Created
June 19, 2016 12:41
-
-
Save wulingyun/3c26b11d2276196b87267edcca7a9e5e to your computer and use it in GitHub Desktop.
Calculate student t-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# one sample t-test | |
t_test1 <- function(x.n, x.mu, x.var, mu, side=0) | |
{ | |
nu <- x.n - 1 | |
t <- (x.mu - mu) / sqrt(x.var / x.n) | |
p <- p.value(pt, t, params=nu, side=side) | |
data.frame(mean=x.mu, df=nu, statistic=t, p.value=p) | |
} | |
# two samples t-test | |
t_test2 <- function(x.n, x.mu, x.var, y.n, y.mu, y.var, var.equal=F, side=0) | |
{ | |
mu <- x.mu - y.mu | |
if (var.equal) { | |
nu <- x.n + y.n - 2 | |
s <- sqrt(((x.n-1)*x.var + (y.n-1)*y.var) / nu) | |
t <- mu / (s * sqrt(1/x.n + 1/y.n)) | |
} | |
else { | |
nu <- (x.var/x.n + y.var/y.n)^2 / (x.var^2/x.n^2/(x.n-1) + y.var^2/y.n^2/(y.n-1)) | |
t <- mu / sqrt(x.var/x.n + y.var/y.n) | |
} | |
p <- p.value(pt, t, params=nu, side=side) | |
data.frame(mean=mu, df=nu, statistic=t, p.value=p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment