###################################################################################### ################## solutions of the exercises included in Lesson 7 ################### ###################################################################################### #Ex 1 # Lower Tail Test of Population Mean with Known Variance #Suppose the manufacturer claims that the mean lifetime of a light bulb is more than 10,000 hours. #In a sample of 30 light bulbs, it was found that they only last 9,900 hours on average. #Assume the population standard deviation is 120 hours. #At .05 significance level, can we reject the claim by the manufacturer against the alternative that the mean is lower? xbar = 9900 # sample mean mu0 = 10000 # hypothesized value sigma = 120 # population standard deviation n = 30 # sample size z = (xbar-mu0)/(sigma/sqrt(n)) z # test statistic #[1] -4.5644 #We then compute the critical value at .05 significance level. alpha = 0.05 #for negative critical value: 2 options: z.alpha = -qnorm(1-alpha) z.alpha = qnorm(alpha) # command for positive crit. val: z.alpha = qnorm(alpha) z.alpha # critical value #[1] 1.6449 #Answer #The test statistic -4.5644 is less than the critical value of -1.6449. #Hence, at .05 significance level, we reject the claim that mean lifetime of a light bulb is above 10,000 hours. #Alternative Solution - pvalue #Instead of using the critical value, we apply the pnorm function to compute the lower tail p-value of the test statistic. #As it turns out to be less than the .05 significance level, we reject the null hypothesis that mu=10000. pval = pnorm(z) pval # lower tail p-value #[1] 2.5052e-06 #Alternative Solution - confidence interval xbar = 9900 # sample mean sigma = 120 # population standard deviation n = 30 alpha = 0.05 quantz<-qnorm(1-alpha/2) quantz low<-xbar-quantz*(sigma/sqrt(n)) upp<-xbar+quantz*(sigma/sqrt(n)) IC<-c(low,upp) IC # Ex 2 # Upper Tail Test of Population Mean with Unknown Variance (S) # Suppose the food label on a cookie bag states that there is at most 2 grams of saturated fat in a single cookie. #In a sample of 35 cookies, it is found that the mean amount of saturated fat per cookie is 2.1 grams. #Assume that the sample standard deviation is 0.3 gram. #At .05 significance level, can we reject the claim on food label against the alternative in which the mean of saturated fat is higher? #The null hypothesis is that ?? ??? 2. We begin with computing the test statistic. xbar = 2.1 # sample mean mu0 = 2 # hypothesized value s = 0.3 # sample standard deviation n = 35 # sample size t = (xbar-mu0)/(s/sqrt(n)) t # test statistic #[1] 1.9720 #We then compute the critical value at .05 significance level. alpha = .05 t.alpha = qt(1-alpha, df=n-1) t.alpha # critical value #[1] 1.6909 #Answer #The test statistic 1.9720 is greater than the critical value of 1.6991. #Hence, at .05 significance level, we can reject the claim that there is at most 2 grams of saturated fat in a cookie. #Alternative Solution #Instead of using the critical value, we apply the pt function to compute the upper tail p-value of the test statistic. As it turns out to be less than the .05 significance level, we reject the null hypothesis that ?? ??? 2. pval = pt(t, df=n-1, lower.tail=FALSE) pval # upper tail p???value #[1] 0.028393 #EX 3 # Two-Tailed Test of Population Proportion #Suppose a coin toss turns up 12 heads out of 20 trials. #At .05 significance level, can one reject the null hypothesis that the coin toss is fair? # Two-tail test #Solution #The null hypothesis is that p = 0.5. We begin with computing the test statistic. pbar = 12/20 # sample proportion p0 = .5 # hypothesized value n = 20 # 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 #Answer #The test statistic 0.89443 lies between the critical values -1.9600 and 1.9600. #Hence, at .05 significance level, we do not reject the null hypothesis that the coin toss is fair. #Alternative Solution 1 #Instead of using the critical value, we apply the pnorm function to compute the two-tailed p-value of the test statistic. #It doubles the upper tail p-value as the sample proportion is greater than the hypothesized value. #Since it turns out to be greater than the .05 significance level, we do not reject the null hypothesis that p = 0.5. pval = 2 * pnorm(z, lower.tail=FALSE) # upper tail pval # two???tailed p???value #[1] 0.37109 #Alternative Solution 2 #We apply the prop.test function to compute the p-value directly. #The Yates continuity correction is disabled for pedagogical reasons. prop.test(12, 20, p=0.5, correct=FALSE) #we can observe the confidence interval. # rule of thumb: if p (observed in the sample is included in the ic, we accept Ho) data: 12 out of 20, null probability 0.5 X???squared = 0.8, df = 1, p???value = 0.3711 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.38658 0.78119 sample estimates: p 0.6