Skip to content

Instantly share code, notes, and snippets.

@mfansler
Created January 1, 2021 21:58
Show Gist options
  • Save mfansler/e4c4d76d7e581b1a12af0b558681ea28 to your computer and use it in GitHub Desktop.
Save mfansler/e4c4d76d7e581b1a12af0b558681ea28 to your computer and use it in GitHub Desktop.
Student-t test for correlation
package main
import (
"fmt"
"math"
"gonum.org/v1/gonum/stat/distuv"
)
func twoSidedPValue(r float64, n float64) float64 {
// compute the test stat
testStat := r*math.Sqrt((n-2)/(1-r*r))
fmt.Println("Found T.S. =", testStat)
// make a Student's t with (n-2) d.f.
t := distuv.StudentsT{0, 1, (n-2), nil}
// compute the p-value
pval := 2.0*t.CDF(-math.Abs(testStat))
return pval
}
func main() {
n := 25.
r := -0.4992939
//ts := 11.2
// compute pval for two-sided test
pval := twoSidedPValue(r, n)
fmt.Println("Found pval =", pval)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment