// prepareRegPlotData prepares predicted point for plotting.
func prepareRegPlotData(r *regression.Regression) (plotter.XYs, error) {
	pts := make(plotter.XYs, 1652)
	i := 1

	for i <= 1652 {
		pts[i-1].X = float64(i)
		value, err := r.Predict([]float64{float64(i)})
		if err != nil {
			return pts, errors.Wrap(err, "Could not calculate predicted value")
		}
		pts[i-1].Y = value
		i++
	}

	return pts, nil
}
// preformRegression performs a linear regression of create repo counts vs. day.
func performRegression(counts [][]int) *regression.Regression {
	var r regression.Regression
	r.SetObserved("count of created Github repos")
	r.SetVar(0, "days since Jan 1 2013")

	for _, count := range counts {
		r.Train(regression.DataPoint(
			float64(count[1]),
			[]float64{float64(count[0])}))
	}

	r.Run()
	return &r
}
Example #3
0
func main() {
	// Initialise a new regression data set
	var r regression.Regression

	// Load the CSV data as data points
	dataFile, err := os.Open("data-no-zeros.csv")
	if err != nil {
		fmt.Print(err, "\n")
	}
	reader := csv.NewReader(dataFile)
	row, err := reader.Read()
	count := 0
	for err == nil {
		if count == 0 {
			for i := 0; i < len(row); i++ {
				if i == 0 {
					r.SetObservedName(row[i])
				} else {
					r.SetVarName(i-1, row[i])
				}
			}
		} else {
			var d regression.DataPoint
			d.Variables = make([]float64, len(row)-1)
			for i := 0; i < len(row); i++ {
				if i == 0 {
					observed, _ := strconv.ParseFloat(row[i], 64)
					d.Observed = observed
				} else {
					variable, _ := strconv.ParseFloat(row[i], 64)
					d.Variables[i-1] = variable
				}
			}
			r.AddDataPoint(d)
		}
		count++
		row, err = reader.Read()
	}
	dataFile.Close()

	// Run the regression
	r.RunLinearRegression()
	r.Dump(true)
}
Example #4
0
func main() {
	var r regression.Regression
	//r.Debug = true
	r.SetObservedName("Distance")
	r.SetVarName(0, "Weight")
	r.SetVarName(1, "Height")
	r.SetVarName(2, "Blood sugar")
	r.AddDataPoint(regression.DataPoint{Observed: 98, Variables: []float64{483, 343, 0.0386}})
	r.AddDataPoint(regression.DataPoint{Observed: 75, Variables: []float64{227, 419, 0.0705}})
	r.AddDataPoint(regression.DataPoint{Observed: 0, Variables: []float64{380, 666, 0.0245}})
	r.AddDataPoint(regression.DataPoint{Observed: 20, Variables: []float64{85, 833, 0.0567}})
	r.AddDataPoint(regression.DataPoint{Observed: 50, Variables: []float64{188, 182, 0.0143}})
	r.AddDataPoint(regression.DataPoint{Observed: 90, Variables: []float64{783, 1148, 0.0129}})
	r.RunLinearRegression()
	r.Dump(true)
}