##### d4: The Practice of Hypothesis Testing ##### ### Preamble source("http://rfs.kvasaheim.com/stat200.R") dt = read.csv("http://rfs.kvasaheim.com/data/crime.csv") attach(dt) ##### ### One-Population mean # The Data weight = c(3,2,1,2,2,4,2,3,5,3) ## Bootstrapping the Mean # Initialize variables B = 10000 ## Number of iterations ts = numeric() ## Tell R to set aside memory # Simple bootstrapping of the mean for (i in 1:B) { x = sample (weight, replace=TRUE) ## Sample from data ts[i] = mean(x) ## Calculate mean } # Analysis Calculations 2*mean(ts > 4) ## p- value quantile(ts, c(0.025,0.975) ) ## confidence interval ## Non-Parametric test # Wilcoxon Test hildebrand.rule(weight) wilcox.test(weight, mu=4, conf.int=TRUE) ## Parametric test # one-sample t-test shapiroTest(weight) t.test(weight, mu=4) ### Example 1a: one-sample t-test rxJ = c(143, 182, 192, 205, 115, 362, 172, 192, 203, 61) shapiroTest(rxJ) t.test(rxJ, mu=0, alternative="greater") ### Example 1b: Wilcoxon test rxP = c (88, 233, 240, 207, 188, 194, 184, 198, 193, 208) shapiroTest(rxP) hildebrand.rule(rxP) wilcox.test(rxP, mu=0, alternative="greater", conf.int=TRUE) ### Example 1c: Mann-Whitney test wilcox.test(rxJ,rxP, conf.int=TRUE) ### Example 2 source("http://rfs.kvasaheim.com/stat200.R") dt = read.csv("http://rfs.kvasaheim.com/data/big12football2015.csv") attach(dt) names(dt) shapiroTest(ptsFor) shapiroTest(ptsAgainst) wilcox.test(ptsFor,ptsAgainst, alternative="greater",conf.int=TRUE) ### Example 3 # Preamble install.packages("quantmod") ## Only once per computer library(quantmod) ## Each time you use it getSymbols("IBM", src="yahoo") stock1 = as.numeric(IBM$IBM.Close) getSymbols("AAPL", src="yahoo") stock2 = as.numeric(AAPL$AAPL.Close) stock1 = stock1 / mean(stock1) stock2 = stock2 / mean(stock2) var.test(stock1,stock2) ### Example 4 # Bootstrapping the proportion genM = rbinom(1e6, size=50, prob=0.105) ## Based on H0 mean(genM >= 24) ## p-value genM = rbinom(1e6, size=50, prob=24/50) ## Based on data quantile(genM, c(0.025,0.975) )/50 ## confidence interval genF = rbinom(1e6, size=25, prob=0.051) ## Based on H0 mean(genF >= 6) genF = rbinom(1e6, size=25, prob=6/25) ## Based on data quantile(genF, c(0.025,0.975) )/25 binom.test(xm,nm, p=0.105) binom.test(xf,nf, p=0.051) prop.test( c(xm,xf*2), c(nm ,nf) ) ### End of file