Skip to content

Instantly share code, notes, and snippets.

@gregglind
Created April 16, 2012 20:52
Show Gist options
  • Save gregglind/2401433 to your computer and use it in GitHub Desktop.
Save gregglind/2401433 to your computer and use it in GitHub Desktop.
Some Demonstrations of Classical Testing.
## some r code for showing of z scores
# made up data about finger counts
fingers <- rep(c(12,11,10,9,8,7,6,5,4,3,2,1,0),c(1,0,10000,9,2,0,0,2,1,0,0,0,1))
table(fingers)
stem(fingers)
summary(fingers)
mean(fingers)
var(fingers)
box(fingers)
stem(fingers)
# simulation of normals drawn from a distribution matching our sample
# see `help(sd)` that sd is the sample sd (with denominator n-1)
plot(density(rnorm(100000, mean(fingers),sd(fingers))),xlim=c(5,15))
# plot the density and cumualative probability
plot(seq(9,11,.01), dnorm(seq(9,11,.01),mean=mean(fingers),sd=sd(fingers)))
plot(seq(9,11,.01), pnorm(seq(9,11,.01),mean=mean(fingers),sd=sd(fingers)))
# we can also plot in functions... first arg will be supplied by plot.
plot(dnorm,xlim=c(-3,-3)
# or for ours...
plot(function(x){dnorm(x,sd=sd(fingers),mean=mean(fingers))},xlim=c(5,15))
plot(function(x){dnorm(x,sd=sd(fingers),mean=mean(fingers))},xlim=c(9.5,10.5))
## onto the meat!
# SUPPORT OR REFUTE!
# what if someone though the mean number of fingers really was exactly 9.8? 10? 11?
pnorm(c(9.8,10,11),mean(fingers),sd(fingers))
# what is the centered 95% ci for our mean estimate?
qnorm(c(.025,.975),mean(fingers),sd(fingers))
# or!
t.test(fingers,mu=9.8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment