// StdOutput is responsible for doing the actual printing to STDOUT, based on certain // arguments which it receives. func StdOutput(mode string, r reg.New, xquery, yquery float64, plot bool) { fmt.Println("*********************************************************") switch mode { case "all": fmt.Printf("Query(YonX for %f):\t\t\t%f\n", yquery, r.ModelYX(yquery)) fmt.Printf("Query(XonY for %f):\t\t\t%f\n", xquery, r.ModelXY(xquery)) fmt.Printf("Equation(YonX):\t\t\t\t%s\n", r.Equation("YonX")) fmt.Printf("Equation(XonY):\t\t\t\t%s\n", r.Equation("XonY")) fmt.Printf("Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // ?? x, y := r.Intersect() fmt.Printf("Intersect:\t\t\t\t(%f,%f)\n", x, y) fmt.Printf("Regression Coeff(YonX):\t\t\t\t%f\n", r.Byx()) fmt.Printf("Regression Coeff(XonY):\t\t\t\t%f\n", r.Bxy()) fmt.Printf("Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Printf("StdDev(Y):\t\t\t\t\t%f\n", r.SdY()) fmt.Printf("StdDev(X):\t\t\t\t\t%f\n", r.SdX()) case "YX": fmt.Printf("Query(YonX for %f):\t\t\t%f\n", yquery, r.ModelYX(yquery)) fmt.Printf("Equation(YonX):\t\t\t\t%s\n", r.Equation("YonX")) fmt.Printf("Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // ?? fmt.Printf("Regression Coeff(YonX):\t\t\t\t%f\n", r.Byx()) fmt.Printf("Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Printf("StdDev(Y):\t\t\t\t\t%f\n", r.SdY()) case "XY": fmt.Printf("Query(XonY for %f):\t\t\t%f\n", xquery, r.ModelXY(xquery)) fmt.Printf("Equation(XonY):\t\t\t%s\n", r.Equation("XonY")) fmt.Printf("Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // <-- not matching fmt.Printf("Regression Coeff(XonY):\t\t\t\t%f\n", r.Bxy()) fmt.Printf("Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Printf("StdDev(X):\t\t\t\t\t%f\n", r.SdX()) default: break } fmt.Println("*********************************************************") if plot { fmt.Printf("%sOpen the .html file in your browser to render the plot%s", yellow, reset) } }
// Standard prints out statistics to a file on disk func Standard(mode string, r reg.New, file string, writeAddr io.Writer, xquery, yquery float64) { if len(file) == 0 { fmt.Fprintf(writeAddr, "Results for: Unknown file\n") } else { fmt.Fprintf(writeAddr, "Results for: %s\n", file) } fmt.Fprintf(writeAddr, "*********************************************************\n") switch mode { case "all": fmt.Fprintf(writeAddr, "Query(YonX for %f):\t\t\t%f\n", yquery, r.ModelYX(yquery)) fmt.Fprintf(writeAddr, "Query(XonY for %f):\t\t\t%f\n", xquery, r.ModelXY(xquery)) fmt.Fprintf(writeAddr, "Equation(YonX):\t\t\t\t%s\n", r.Equation("YonX")) fmt.Fprintf(writeAddr, "Equation(XonY):\t\t\t\t%s\n", r.Equation("XonY")) fmt.Fprintf(writeAddr, "Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // <-- not matching x, y := r.Intersect() fmt.Fprintf(writeAddr, "Intersect:\t\t\t\t\t(%.1f,%.1f)\n", x, y) fmt.Fprintf(writeAddr, "Regression Coeff(YonX):\t\t\t\t%f\n", r.Byx()) fmt.Fprintf(writeAddr, "Regression Coeff(XonY):\t\t\t\t%f\n", r.Bxy()) fmt.Fprintf(writeAddr, "Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Fprintf(writeAddr, "StdDev(Y):\t\t\t\t\t%f\n", r.SdY()) fmt.Fprintf(writeAddr, "StdDev(X):\t\t\t\t\t%f\n", r.SdX()) case "YX": fmt.Fprintf(writeAddr, "Query(YonX for %f):\t\t\t%f\n", yquery, r.ModelYX(yquery)) fmt.Fprintf(writeAddr, "Equation(YonX):\t\t\t\t%s\n", r.Equation("YonX")) fmt.Fprintf(writeAddr, "Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // <-- not matching fmt.Fprintf(writeAddr, "Regression Coeff(YonX):\t\t\t\t%f\n", r.Byx()) fmt.Fprintf(writeAddr, "Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Fprintf(writeAddr, "StdDev(Y):\t\t\t\t\t%f\n", r.SdY()) case "XY": fmt.Fprintf(writeAddr, "Query(XonY for %f):\t\t\t%f\n", xquery, r.ModelXY(xquery)) fmt.Fprintf(writeAddr, "Equation(XonY):\t\t\t\t%s\n", r.Equation("XonY")) fmt.Fprintf(writeAddr, "Confidence:\t\t\t\t\t%.2f%%\n", r.Conf()) // <-- not matching fmt.Fprintf(writeAddr, "Regression Coeff(XonY):\t\t\t\t%f\n", r.Bxy()) fmt.Fprintf(writeAddr, "Covariance:\t\t\t\t\t%f\n", r.Covariance()) fmt.Fprintf(writeAddr, "StdDev(X):\t\t\t\t\t%f\n", r.SdX()) default: break } fmt.Fprintf(writeAddr, "*********************************************************\n") }
// Plot uses HTML5/SVG to create a graph which you can view in your browser func Plot(location string, r reg.New, yx, xy bool) { if location == "stdin" { // We don't have a filename or location. Using the current // UNIX timestamp in nanoseconds instead. File is created // in the working directory. cwd, _ := os.Getwd() name := fmt.Sprintf("/plot-%d.html", time.Now().UnixNano()) absPath := cwd + name loc, err = os.Create(absPath) if err != nil { fault.Trap(err, "errFsperm") } defer loc.Close() } else { // We have both the filename and the location // where to create it. Proceed normally. cwd := filepath.Dir(location) name := fmt.Sprintf("/plot-for-%s.html", filepath.Base(location)) absPath := cwd + name loc, err = os.Create(absPath) if err != nil { fault.Trap(err, "errFsperm") } defer loc.Close() } // Draw the data points // Initialize the graph object with 33x22 grids gp := r.GraphInit(xgcount, ygcount) fmt.Fprintf(loc, "<!DOCTYPE html>\n\n") fmt.Fprintf(loc, "<!-- ratChart -->\n") fmt.Fprintf(loc, "<!-- This plot was generated by %s-->\n\n", doc.MiniVer()) fmt.Fprintf(loc, "<!-- This file is best rendered in modern versions of -->\n") fmt.Fprintf(loc, "<!-- Mozilla Firefox/Google Chrome. To render the plot, -->\n") fmt.Fprintf(loc, "<!-- open this file with your browser. -->\n\n") fmt.Fprintf(loc, "<head>\n") fmt.Fprintf(loc, "\t<meta charset=\"%s\">\n", charset) fmt.Fprintf(loc, "\t<title>ratchart</title>\n") fmt.Fprintf(loc, "\t<style type=\"text/css\">\n") fmt.Fprintf(loc, "\t\tbody {background-color: %s; background-attachment: fixed; font-family: \"%s\"}\n", bgcolor, bodyfnt) fmt.Fprintf(loc, "\t\t.headerbar {background-color: #116A95; color: %s; font-family: \"%s\";\n\t\t font-size: 20px;font-weight: bold; padding: 2px; text-align: center;}\n", bgcolor, plotfnt) fmt.Fprintf(loc, "\t\t.plotarea {background-color: %s; font-family: \"%s\"; font-size: 12px; float: left;}\n", bgcolor, plotfnt) fmt.Fprintf(loc, "\t\t.legend {background-color: %s; float: right; color: %s; padding: 15px;}\n", legendcl, bgcolor) fmt.Fprintf(loc, "\t</style>\n") fmt.Fprintf(loc, "</head>\n") fmt.Fprintf(loc, "<body>\n") fmt.Fprintf(loc, "\t<div class=\"headerbar\">\n") fmt.Fprintf(loc, "\t\t<p>\n") if yx && xy || !yx && !xy { fmt.Fprintf(loc, "\t\t\tPlot for: %s and %s\n", r.Equation("YonX"), r.Equation("XonY")) } if yx && !xy { fmt.Fprintf(loc, "\t\t\tPlot for: %s\n", r.Equation("YonX")) } if !yx && xy { fmt.Fprintf(loc, "\t\t\tPlot for: %s\n", r.Equation("XonY")) } fmt.Fprintf(loc, "\t\t</p>\n") fmt.Fprintf(loc, "\t</div>\n") fmt.Fprintf(loc, "\t<div class=\"plotarea\">\n") fmt.Fprintf(loc, "\t\t<svg width=\"1000\" height=\"600\">\n") // Tag to show which line is which if yx && xy || !yx && !xy { fmt.Fprintf(loc, "\t\t\t<line x1=\"40\" y1=\"0\" x2=\"40\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") fmt.Fprintf(loc, "\t\t\t<line x1=\"190\" y1=\"0\" x2=\"190\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") fmt.Fprintf(loc, "\t\t\t<line x1=\"40\" y1=\"50\" x2=\"190\" y2=\"50\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") fmt.Fprintf(loc, "\t\t\t<text x=\"50\" y =\"20\" fill=\"black\"> Y on X </text>\n") fmt.Fprintf(loc, "\t\t\t<text x=\"50\" y =\"40\" fill=\"black\"> X on Y </text>\n") fmt.Fprintf(loc, "\t\t\t<line x1=\"100\" y1=\"16\" x2=\"180\" y2=\"16\" style=\"stroke:rgb(248,32,32);stroke-width:3\"/>\n") fmt.Fprintf(loc, "\t\t\t<line x1=\"100\" y1=\"36\" x2=\"180\" y2=\"36\" style=\"stroke:rgb(11,92,20);stroke-width:3\"/>\n") } // Line definition // fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(32,42,198);stroke-width:3\"/>\n", 40,20,40,10) // Origin tag // On the graph, (0,0) starts from (310,500) fmt.Fprintf(loc, "\t\t\t<text x=\"260\" y =\"520\" fill=\"black\"> (0,0) </text>\n") // // X coordinate tag // xtag := fmt.Fprintf("\t\t\t<text x=\"910\" y =\"530\" fill=\"black\"> (%0.f,0) </text>\n", gp.Xgrids[len(gp.Xgrids)-1]) // // Y coordinate tag // ytag := fmt.Fprintf("\t\t\t<text x=\"260\" y =\"25\" fill=\"black\"> (0,%0.f) </text>\n", gp.Ygrids[len(gp.Ygrids)-1]) // Y- axis fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"35\" x2=\"305\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") // Y -axis label fmt.Fprintf(loc, "\t\t\t<text x=\"200\" y=\"285\" fill=\"black\"> (Y) </text>\n") // Mirror X fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"35\" x2=\"995\" y2=\"35\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") // X axis fmt.Fprintf(loc, "\t\t\t<line x1=\"305\" y1=\"505\" x2=\"995\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") // X axis label fmt.Fprintf(loc, "\t\t\t<text x=\"620\" y=\"580\" fill=\"black\"> (X) </text>\n") // Mirror Y fmt.Fprintf(loc, "\t\t\t<line x1=\"995\" y1=\"35\" x2=\"995\" y2=\"505\" style=\"stroke:rgb(15,25,32);stroke-width:3\"/>\n") // Draw framework // Draw grids // Vertical grids, 33 of them, spacing 20 for I := 0; ; I++ { J := 20.0 * float64(I) X, _, term := reg.Translate(J, 0) if term { break } fmt.Fprintf(loc, "%s%.2f%s%s%.2f%s%s", x1var, X, y1const, x2var, X, y2const, grstyle) if I > 0 && I < (len(gp.Xgrids)-1) { fmt.Fprintf(loc, "\t\t\t<text x=\"%.0f\" y=\"520\" fill=\"black\" transform=\"rotate(60 %.0f,520)\">%.2f</text>\n", gp.Xcoord[I], gp.Xcoord[I], gp.Xgrids[I]) } } // Horizontal grids, 22 of them, spacing 20 for I := 0; ; I++ { J := 20.0 * float64(I) _, Y, term := reg.Translate(0, J) if term { break } fmt.Fprintf(loc, "%s%s%.2f%s%s%.2f%s", x1const, y1var, Y, x2const, y2var, Y, grstyle) if I > 0 && I < (len(gp.Ygrids)-1) { fmt.Fprintf(loc, "\t\t\t<text x=\"240\" y=\"%.0f\" fill=\"black\" transform=\"rotate(0 260,%.0f)\">%.2f</text>\n", gp.Ycoord[I], gp.Ycoord[I], gp.Ygrids[I]) } } // Initialize plotter pl := gp.PlotInit(r) for index, element := range pl.X { fmt.Fprintf(loc, "%s%f%s%f%s\n", circstr, element, circmid, pl.Y[index], circsty) } // Draw regression lines rg := r.ReglineInit(gp) xxy1, xxy2 := rg.XonXY[0], rg.XonXY[1] // X on Y yxy1, yxy2 := rg.YonXY[0], rg.YonXY[1] xyx1, xyx2 := rg.XonYX[0], rg.XonYX[1] // Y on X yyx1, yyx2 := rg.YonYX[0], rg.YonYX[1] if !yx && xy || !yx && !xy { // XonY fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(11,92,20);stroke-width:3\"/>\n", xxy1, yxy1, xxy2, yxy2) } if yx && !xy || !yx && !xy { // YonX fmt.Fprintf(loc, "\t\t\t<line x1=\"%.2f\" y1=\"%.2f\" x2=\"%.2f\" y2=\"%.2f\" style=\"stroke:rgb(248,32,32);stroke-width:3\"/>\n", xyx1, yyx1, xyx2, yyx2) } fmt.Fprintf(loc, "\t\t</svg>\n") fmt.Fprintf(loc, "\t</div>\n") fmt.Fprintf(loc, "\t<div class=\"legend\">\n") fmt.Fprintf(loc, "\t\t<p> <u> AXIS SCALE: </u> </p>\n") fmt.Fprintf(loc, "\t\t<p> * X-axis: %.3f units </p>\n", r.GridScale("X", xgcount)) fmt.Fprintf(loc, "\t\t<p> * Y-axis: %.3f units </p>\n", r.GridScale("Y", ygcount)) fmt.Fprintf(loc, "\t\t<p> <u> AXIS LABLE: </u> </p>\n") fmt.Fprintf(loc, "\t\t<p> * X-axis: %s </p>\n", r.Xlabel) fmt.Fprintf(loc, "\t\t<p> * Y-axis: %s </P>\n", r.Ylabel) fmt.Fprintf(loc, "\t\t<p> <u> STATS: </u> </p>\n") fmt.Fprintf(loc, "\t\t<p> * R value: %.2f </p>\n", r.Rval()) fmt.Fprintf(loc, "\t\t<p> * RegCoeff (Y on X): %.2f </p>\n", r.Byx()) fmt.Fprintf(loc, "\t\t<p> * RegCoeff (X on Y): %.2f </p>\n", r.Bxy()) fmt.Fprintf(loc, "\t\t<p> * Confidence: %.2f%% </p>\n", r.Conf()) fmt.Fprintf(loc, "\t\t<p> * Covariance: %.2f </p>\n", r.Covariance()) fmt.Fprintf(loc, "\t\t<p> * StdDev (X): %.1f </p>\n", r.SdX()) fmt.Fprintf(loc, "\t\t<p> * StdDev (Y): %.1f </p>\n", r.SdY()) fmt.Fprintf(loc, "\t</div>\n") fmt.Fprintf(loc, "</body>\n") fmt.Fprintf(loc, "</html>") }
// func Draw(data reg.New) { func Draw(model reg.New, datasource string) { if datasource == "stdin" { statheader = fmt.Sprintf("Statistics (File: Command line entry)") } else { statheader = fmt.Sprintf("Statistics (File: %s)", datasource) } if err := ui.Init(); err != nil { panic(err) } defer ui.Close() //Main data plot lc := ui.NewLineChart() lc.BorderLabel = "Main plot. Hit 'q' to exit. Drag to resize." lc.Data = []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.2, 0.1, 0.4, 0.33, 12.23} lc.Mode = "dot" lc.Height = 15 lc.AxesColor = ui.ColorWhite lc.LineColor = ui.ColorRed | ui.AttrBold //Residual plots rp0 := ui.NewLineChart() rp0.BorderLabel = "Residual 1" rp0.Data = []float64{1, 2, 3, 4, 5, 6, 7, 8} rp0.Mode = "dot" rp0.Height = 15 rp0.AxesColor = ui.ColorWhite rp0.LineColor = ui.ColorRed | ui.AttrBold //Display statistics // eq, conf, sd, rgc, intcp // axis lable and axis scale xlab := str("X axis: %s", model.Xlabel) ylab := str("Y axis: %s", model.Ylabel) eqxy := str("Equation(XonY): %s", model.Equation("XonY")) eqyx := str("Equation(YonX): %s", model.Equation("YonX")) conf := str("Confidence: %0.2f %%", model.Conf()) sdx := str("StdDev(X): %0.2f", model.SdX()) sdy := str("StdDev(Y): %0.2f", model.SdY()) bxy := str("RegCof(xy): %0.2f", model.Bxy()) byx := str("RegCof(yx): %0.2f", model.Byx()) cov := str("Covariance: %0.2f", model.Covariance()) ix, iy := model.Intersect() icept := fmt.Sprintf("Intercept: %0.2f, %0.2f", ix, iy) stats := []string{ xlab, ylab, eqxy, eqyx, conf, sdx, sdy, bxy, byx, cov, icept, } lst := ui.NewList() lst.Items = stats // slice lst.Height = 13 lst.Width = 30 lst.Y = 0 lst.BorderLabel = statheader lst.ItemFgColor = ui.ColorWhite ui.Body.AddRows(ui.NewRow(ui.NewCol(8, 0, lc), ui.NewCol(4, 0, rp0)), ui.NewRow(ui.NewCol(12, 0, lst))) // ui.Body.AddRows(ui.NewRow(ui.NewCol(8,0,rp0))) ui.Body.Align() ui.Render(ui.Body) ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() }) ui.Handle("sys/wnd/resize", func(e ui.Event) { ui.Body.Width = ui.TermWidth() ui.Body.Align() ui.Render(ui.Body) }) ui.Loop() }