# Bret Larget # Fat mice # Exercise 4.127 # Enter the data for the light/light group # and the light/dark group ll = c(10,10,11,9,12,9,11,9,17) ld = c(5,6,7,8,3,8,6,6,4) # Make one variable for the combined data ex4.127 = c(ll,ld) # function to take difference in means from first 9 and second 9 f = function(x) { return( mean(x[1:9]) - mean(x[10:18]) ) } # variable to store randomization distribution out = numeric(10000) # one at a time, put the data into a random order # and take differences of sample means for ( i in 1:10000 ) { out[i] = f(sample(ex4.127)) } # Make a plot to see if it is roughly bell-shaped and symmetric. require(ggplot2) plot4.127 = ggplot(data.frame(out),aes(x=out)) + geom_density() + xlab('Null Difference in Weight Gain (grams)') plot(plot4.127) # Estimate p-value # H0: mu1 = mu2 # Ha: mu1 > mu 2 pval4.127 = sum(out > f(ex4.127)) / length(out) print(paste("p-value (right tail) =",pval4.127))