Example R programs and commands Student-t test power demo # All lines preceded by the "#" character are my comments. # All other left-justified lines are my input. # All other indented lines are the R program output. # Truth: X is a normal random variable with mean 0.34 # and standard deviation 2 # Hypothesis: X is a normal random variable with mean 0 # and unknown standard deviation # # How likely are we to reject the (false) hypothesis if we # can only afford to take 100 samples of X? ### Preliminary investigation: # Simulate sampling X 100 times: x <- rnorm(100, mean=0.34, sd=2 ) # Student-t test of "H0:mu=0" from first principles: s <- sd(x); # estimated standard deviation of X from samples se <- s/sqrt(100); # estimated standard error for a 100-sample average t <- abs(mean(x)-0)/se; # t-statistic for null hypothesis "H0:mu=0" 2*pt(t,df=99,lower.tail=FALSE) # p-value for the test. # ...so given a significance level alpha (such as alpha=0.05), we # Do not reject H0 if p>alpha ; # Reject H0 if p