######################################################################### ########### Hypotesis test ############ ######################################################################### ################### examples on the slides ############################# # 1 variance in the pop is known xbar = 2.84 # sample mean mu0 = 3 # hypothesized value sigma = 0.8 # population standard deviation n = 100 # sample size z = (xbar-mu0)/(sigma/sqrt(n)) #Z-stat z #Z-stat alpha = 0.05 # critical values z (two tails test) # qnorm: quantile function for the normal distribution qnorm(alpha/2) # negative value (only 0.025 of the distrib) qnorm(1-alpha/2) # positive value (0.925 of the distrib) # conclusion: Z-stat is bigger than 1.96=> reject null hypothesis # p-value computation pval = 2 * pnorm(z) pval lower_bound<-xbar-(1.959964*(sigma/sqrt(n))) upper_bound<-xbar+(1.959964*(sigma/sqrt(n))) ci<-c(lower_bound, upper_bound) ci ################################################################################ # 2 - # 1 variance in the pop is unknown - Use S (st.dev in the sample) xbar = 172.5 # sample mean mu0 = 168 # hypothesized value s = 15.4 # sample standard deviation n = 25 # sample size t = (xbar-mu0)/(s/sqrt(n)) t # test statistic #t-stat - 1.46 df=n-1 alpha = 0.05 t.alpha = qt(1-alpha/2, df=n-1) t.alpha # critical value 2.06 # Conclusion: do not reject Ho (Accept Ho) pt(t,df) ############# proportion case ################################################# pbar = 0.05 # sample proportion 25/500 p0 = 0.08 # hypothesized value in the Pop n = 500 # sample size z = (pbar-p0)/sqrt(p0*(1-p0)/n) z # test statistic #[1] 0.89443 #We then compute the critical values at .05 significance level. alpha = .05 z.half.alpha = qnorm(1-alpha/2) c(-z.half.alpha, z.half.alpha) #[1] ???1.9600 1.9600 # conclusion: reject Ho pval = 2 * pnorm(z) pval ############################################################################################# ########################### Hypotesis test with dataset ###################################### data<-mtcars summary(data$mpg) mu=22 # valore di Ho #t.test(x, y = NULL, # alternative = c("two.sided", "less", "greater"), # choose an altern. hyp on the basis of the test # conf.level = 0.95 ) mpg<-data$mpg t.test(data$mpg, alternative = "two.sided", mu=22, conf.level = 0.95) t.alpha = qt(0.975, df=31) # crit.val. t.alpha # accept ho ##### VARIANCE KNOWN # es. mean mpg # sigma=5 (conf.int 0.95) --> variance in the pop is known (mu=22 is the mean in the popolation) # Ho: mu=22 # H1: mu != 22 library("BSDA") z.test(mtcars$mpg, alternative = "two.sided", mu=22, sigma.x = 5, conf.level = 0.95) ########## proportion. test # variable am, con p_0=0.45 # Ho: p0=0.45 # H1: p0 != 0.45 # test bilaterale --> two.sided table(data$am) #13 units with value equal to 1 prop.test(x = 13, n = 32, p=0.45, alternative = "two.sided", correct = FALSE) ########### exercise to do at home #################### # load dataset marketing campagin # A) compute an hypotesis test in which the food exp is equal to 25 # against the hypotesis that the mean is greater by considering a conf. level equal to 90% # B) compute an hypotesis test in which the percentage of complaint is equal to 3% #against the hypotesis that the prop is lower than those hypotized in the null hyp # consider a conf. level equal to 90%