Skip to content

Instantly share code, notes, and snippets.

@GuangchuangYu
Created May 29, 2012 07:03
Show Gist options
  • Save GuangchuangYu/2823041 to your computer and use it in GitHub Desktop.
Save GuangchuangYu/2823041 to your computer and use it in GitHub Desktop.
T test
## http://ygc.name/2012/05/29/t-test/
p1 <- rnorm(1000)
p2 <- rnorm(1000, sd=3)
hist(p2, col="green", xlab="", main="")
hist(p1, col="red", add=T)
legend(legend=c("sd=1", "sd=3"), fill=c("green", "red"), x="topright")
getm <- function(size, mean=0, sd=1, n=100) {
sapply(1:n, function(i) {
x <- rnorm(size, mean, sd)
mean(x)
})
}
m1 <- getm(100)
m2 <- getm(100,sd=3)
hist(m2, col="green", main="Histogram of sample means", xlab="Means")
hist(m1, col="red", add=T)
legend(legend=c("sd=1", "sd=3"),
fill=c("red", "green"), "topright")
m1 <- getm(50)
m2 <- getm(200)
hist(m1,col="green", main="Histogram of sample means", xlab="Means")
hist(m2,add=T, col="red")
legend(legend=c("sample size=50", "sample size=200"),
fill=c("green", "red"), "topright")
gett <- function(size, mean=0, sd=1, n=100) {
sapply(1:n, function(i) {
x <- rnorm(size, mean, sd)
mean(x)/(sd(x)/sqrt(size))
})
}
t1 <- gett(100)
hist(t1, main="Histogram of t statistics", xlab="t")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment