From 95e9db9a70fd50632d7723641395d92a1f134d06 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 2 Aug 2018 14:59:54 +0200 Subject: [PATCH] stat: add example for LinearRegression --- stat/stat_example_test.go | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 stat/stat_example_test.go diff --git a/stat/stat_example_test.go b/stat/stat_example_test.go new file mode 100644 index 00000000..d7b0d465 --- /dev/null +++ b/stat/stat_example_test.go @@ -0,0 +1,45 @@ +// Copyright ©2018 The Gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stat_test + +import ( + "fmt" + + "golang.org/x/exp/rand" + + "gonum.org/v1/gonum/stat" +) + +func ExampleLinearRegression() { + var ( + xs = make([]float64, 100) + ys = make([]float64, 100) + weights []float64 + ) + + line := func(x float64) float64 { + return 1 + 3*x + } + + for i := range xs { + xs[i] = float64(i) + ys[i] = line(xs[i]) + 0.1*rand.NormFloat64() + } + + // Do not force the regression line to pass through the origin. + origin := false + + alpha, beta := stat.LinearRegression(xs, ys, weights, origin) + r2 := stat.RSquared(xs, ys, weights, alpha, beta) + + fmt.Printf("Estimated slope is: %v\n", alpha) + fmt.Printf("Estimated offset is: %v\n", beta) + fmt.Printf("R^2: %v\n", r2) + + // Output: + // Estimated slope is: 0.988572424633503 + // Estimated offset is: 3.0001541344029676 + // R^2: 0.9999991095061128 +}