lab10soln

pdf

School

Indiana University, Bloomington *

*We aren’t endorsed by this school

Course

630

Subject

Economics

Date

Jan 9, 2024

Type

pdf

Pages

4

Uploaded by DeanIbisMaster955

Report
Lab 10 Solutions Exercise 1 . Calculate a 95% confidence interval for the population proportion of Ameri- cans that favor the death penalty. library (resampledata) data ( "GSS2002" ) death_penalty <- GSS2002 $ DeathPenalty[ ! is.na (GSS2002 $ DeathPenalty)] n <- length (death_penalty) phat <- sum (death_penalty == 'Favor' ) / n SE <- sqrt (phat * ( 1 - phat) / n) ci_l <- phat - 1.96 * SE ci_u <- phat + 1.96 * SE round ( c (ci_l, ci_u), digits = 3 ) ## [1] 0.662 0.712 We are 95% confident that the population proportion of Americans that favor the death penalty is between 0.662 and 0.712. Exercise 2 . Conduct a hypothesis test to test whether males and females have different views on the death penalty. Specifically, test whether the population proportion of males that favor the death penalty is different than the population proportion of females that favor the death penalty. Report the test statistic and p-value, and your decision based on the p-value. H 0 : p m = p f H A : p m 6 = p f where p m is the proportion of males that favor the death penalty, and p f is the proportion of females that favor the death penalty. # remove missing data index <- which ( is.na (GSS2002 $ Gender) | is.na (GSS2002 $ DeathPenalty)) gender <- GSS2002 $ Gender[ - index] death_penalty <- GSS2002 $ DeathPenalty[ - index] 1
# contingency tables table (gender, death_penalty) ## death_penalty ## gender Favor Oppose ## Female 424 253 ## Male 475 156 prop.table ( table (gender, death_penalty), margin = 1 ) ## death_penalty ## gender Favor Oppose ## Female 0.6262925 0.3737075 ## Male 0.7527734 0.2472266 n <- length (death_penalty) n_f <- sum (gender == 'Female' ) n_m <- sum (gender == 'Male' ) # sample proportion of females that favor the death penalty: phat_f <- sum (gender == 'Female' & death_penalty == 'Favor' ) / n_f # sample proportion of males that favor the death penalty: phat_m <- sum (gender == 'Male' & death_penalty == 'Favor' ) / n_m # pooled sample proportion phat <- sum (death_penalty == 'Favor' ) / n # compute test statisic SE <- sqrt (phat * ( 1 - phat) * ( 1 / n_f + 1 / n_m)) z <- (phat_f - phat_m) / SE; z ## [1] -4.930561 # p-value pvalue = 2 * pnorm (z); pvalue ## [1] 8.199368e-07 Since the p-value < 0 . 05, we reject H 0 . The data suggest that population proportion of males that favor the death penalty is different than the population proportion of females that favor the death penalty. Also, the sample sizes are large enough in each cell of the contingency table (greater than 5), so the conditions for the z -test are satisfied. 2
Exercise 3 . Calculate a 95% confidence interval for the difference between the proportion of males and females that favor the death penalty. Interpret the interval in context. Does the confidence interval agree with the conclusion of the hypothesis test? SE <- sqrt (phat_f * ( 1 - phat_f) / n_f + phat_m * ( 1 - phat_m) / n_m) ci_l <- phat_m - phat_f - 1.96 * SE ci_u <- phat_m - phat_f + 1.96 * SE round ( c (ci_l, ci_u), 3 ) ## [1] 0.077 0.176 We are 95% confident that the population proportion of males that favor the death penalty is between 0.077 and 0.176 higher than the population proportion of females that favor the death penalty. Yes, since 0 is not inside the interval, it agrees with the conclusion of the hypothesis test. Exercise 4 . Consider the categorical variables Education (highest level of education) and SpendSci (position on government spending on science). (a) Make a contingency table between the two variables. table (GSS2002 $ Education, GSS2002 $ SpendSci) ## ## About right Too little Too much ## Left HS 83 54 29 ## HS 345 249 103 ## Jr Col 43 37 14 ## Bachelors 107 84 20 ## Graduate 51 37 10 (b) Make a contingency table of the row proportions (where the rows are the different categories for Education ; hint: use the prop.table() function). Based on the table of row proportions, does there appear to be a relationship between the two categorical variables? prop.table ( table (GSS2002 $ Education, GSS2002 $ SpendSci), margin = 1 ) ## ## About right Too little Too much ## Left HS 0.50000000 0.32530120 0.17469880 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
## HS 0.49497848 0.35724534 0.14777618 ## Jr Col 0.45744681 0.39361702 0.14893617 ## Bachelors 0.50710900 0.39810427 0.09478673 ## Graduate 0.52040816 0.37755102 0.10204082 No, there does not appear to be a relationship between the two categorical variables. For instance, the row proportions for “About right” are close to 0.5 for each education level. (c) Conduct a chi-square test to test whether or not Education and SpendSci are inde- pendent. Write down the null and alternative hypothesis and your conclusion based on the p-value. H 0 : Education level and opinion on government spending are independent. H A : Education level and opinion on government spending are dependent. chisq1 = chisq.test (GSS2002 $ Education, GSS2002 $ SpendSci) chisq1 ## ## Pearson's Chi-squared test ## ## data: GSS2002$Education and GSS2002$SpendSci ## X-squared = 8.0804, df = 8, p-value = 0.4257 Since the p-value > 0 . 05, we reject H 0 . (d) Are the conditions for the chi-square test in part (c) satisfied? Yes, the expected counts (see part e) are all greater than 5. We also assume random sampling. (e) Extract the table of expected counts for the chi-square test you conducted in part (c). chisq1 $ expected ## GSS2002$SpendSci ## GSS2002$Education About right Too little Too much ## Left HS 82.47551 60.44708 23.07741 ## HS 346.29779 253.80490 96.89731 ## Jr Col 46.70300 34.22907 13.06793 ## Bachelors 104.83333 76.83333 29.33333 ## Graduate 48.69036 35.68562 13.62401 4