How to use this document. Run the code along with me. Comments start with
#. Each section mirrors the live lab; code chunks show the command and its output together.
Today’s plan
prop.testThe dataset includes information on high school graduates’ test scores, family background, and distance to college. It ships with the AER package, so no external file is needed.
Key variables
gender — student gender (Factor: “male”, “female”)ethnicity — student ethnicity (Factor: “other”, “afam”,
“hispanic”)score — student test score (Numeric)fcollege — father attended college (Factor: “no”,
“yes”)mcollege — mother attended college (Factor: “no”,
“yes”)home — student lives at home (Factor: “no”, “yes”)urban — lives in an urban area (Factor: “no”,
“yes”)unemp — county unemployment rate (%) (Numeric)wage — average wage in county (Numeric)distance — distance (in miles) to nearest 4-year
college (Numeric)tuition — relative tuition cost at 4-year colleges
(Numeric)education — years of schooling completed (Numeric)income — family income level (Factor: “low”,
“high”)region — region of the U.S. (Factor: “other”,
“west”)# Load the dataset from the AER package
data("CollegeDistance")
college <- CollegeDistance
remove(CollegeDistance) # remove duplicate#> 'data.frame': 4739 obs. of 14 variables:
#> $ gender : Factor w/ 2 levels "male","female": 1 2 1 1 2 1 2 2 1 2 ...
#> $ ethnicity: Factor w/ 3 levels "other","afam",..: 1 1 1 2 1 1 1 1 1 1 ...
#> $ score : num 39.2 48.9 48.7 40.4 40.5 ...
#> $ fcollege : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 2 1 ...
#> $ mcollege : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
#> $ home : Factor w/ 2 levels "no","yes": 2 2 2 2 1 2 2 2 2 2 ...
#> $ urban : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 1 1 2 2 ...
#> $ unemp : num 6.2 6.2 6.2 6.2 5.6 ...
#> $ wage : num 8.09 8.09 8.09 8.09 8.09 ...
#> $ distance : num 0.2 0.2 0.2 0.2 0.4 ...
#> $ tuition : num 0.889 0.889 0.889 0.889 0.889 ...
#> $ education: num 12 12 12 12 13 12 13 15 13 15 ...
#> $ income : Factor w/ 2 levels "low","high": 2 1 1 1 1 1 1 1 1 1 ...
#> $ region : Factor w/ 2 levels "other","west": 1 1 1 1 1 1 1 1 1 1 ...
#> - attr(*, "datalabel")= chr ""
#> - attr(*, "time.stamp")= chr "25 Oct 2002 16:44"
#> - attr(*, "formats")= chr [1:14] "%9.0g" "%9.0g" "%9.0g" "%9.0g" ...
#> - attr(*, "types")= int [1:14] 102 102 102 102 102 102 102 102 102 102 ...
#> - attr(*, "val.labels")= chr [1:14] "" "" "" "" ...
#> - attr(*, "var.labels")= chr [1:14] "" "" "" "" ...
#> - attr(*, "version")= int 6
#> - attr(*, "label.table")=List of 14
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
#> ..$ : NULL
# In RStudio you would run View(college); here we preview the first rows.
knitr::kable(head(college), caption = "First rows of the CollegeDistance data")| gender | ethnicity | score | fcollege | mcollege | home | urban | unemp | wage | distance | tuition | education | income | region |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| male | other | 39.15 | yes | no | yes | yes | 6.2 | 8.09 | 0.2 | 0.88915 | 12 | high | other |
| female | other | 48.87 | no | no | yes | yes | 6.2 | 8.09 | 0.2 | 0.88915 | 12 | low | other |
| male | other | 48.74 | no | no | yes | yes | 6.2 | 8.09 | 0.2 | 0.88915 | 12 | low | other |
| male | afam | 40.40 | no | no | yes | yes | 6.2 | 8.09 | 0.2 | 0.88915 | 12 | low | other |
| female | other | 40.48 | no | no | no | yes | 5.6 | 8.09 | 0.4 | 0.88915 | 13 | low | other |
| male | other | 54.71 | no | no | yes | yes | 5.6 | 8.09 | 0.4 | 0.88915 | 12 | low | other |
prop.testResearch question. Is the proportion of high-income students different from 0.5?
# Count number of 'high' income students
x_high <- sum(college$income == "high")
# Total number of students
n_total <- nrow(college)Two-sided test (\(H_0\): true proportion of high-income students \(= 0.5\); \(H_1\): true proportion \(\neq 0.5\)). The test statistic (\(X^2\)) and p-value tell us whether the observed proportion is significantly different from 0.5.
#>
#> 1-sample proportions test with continuity correction
#>
#> data: x_high out of n_total, null probability 0.5
#> X-squared = 850.83, df = 1, p-value < 2.2e-16
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.2752141 0.3012030
#> sample estimates:
#> p
#> 0.2880355
Check the 95% confidence interval.
#> [1] 0.2752141 0.3012030
#> attr(,"conf.level")
#> [1] 0.95
One-sided test — test whether the proportion of high-income students \(> 0.5\) (\(H_0\): \(p \leq 0.5\); \(H_1\): \(p > 0.5\)). If the p-value \(< 0.05\), we reject \(H_0\) and conclude the proportion is significantly greater than 0.5. The one-sided test is used when we have a directional hypothesis.
prop_test_one_sided <- prop.test(x = x_high, n = n_total, p = 0.5, alternative = "greater")
prop_test_one_sided#>
#> 1-sample proportions test with continuity correction
#>
#> data: x_high out of n_total, null probability 0.5
#> X-squared = 850.83, df = 1, p-value = 1
#> alternative hypothesis: true p is greater than 0.5
#> 95 percent confidence interval:
#> 0.2772343 1.0000000
#> sample estimates:
#> p
#> 0.2880355
Research question. Does distance to college affect test scores?
#>
#> Call:
#> lm(formula = score ~ distance, data = college)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -22.352 -6.924 0.203 6.813 22.101
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 51.35330 0.16034 320.27 < 2e-16 ***
#> distance -0.25752 0.05491 -4.69 2.81e-06 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 8.683 on 4737 degrees of freedom
#> Multiple R-squared: 0.004621, Adjusted R-squared: 0.004411
#> F-statistic: 21.99 on 1 and 4737 DF, p-value: 2.815e-06
Confidence interval for the coefficients.
#> 2.5 % 97.5 %
#> (Intercept) 51.0389556 51.6676422
#> distance -0.3651712 -0.1498628
Interpretation.
distance = 0.distance): expected change in score
for each additional mile.Research question. Does distance still matter after controlling for family background, income, and region?
model_2 <- lm(score ~ distance + fcollege + mcollege +
home + urban + unemp + wage + tuition +
education + income + region,
data = college)
summary(model_2)#>
#> Call:
#> lm(formula = score ~ distance + fcollege + mcollege + home +
#> urban + unemp + wage + tuition + education + income + region,
#> data = college)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -21.5003 -5.6451 0.0033 5.4572 22.7546
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 15.49630 1.19365 12.982 < 2e-16 ***
#> distance -0.05115 0.05291 -0.967 0.333764
#> fcollegeyes 1.92447 0.31524 6.105 1.11e-09 ***
#> mcollegeyes 0.94899 0.35420 2.679 0.007404 **
#> homeyes 1.43657 0.28806 4.987 6.35e-07 ***
#> urbanyes -1.33062 0.27229 -4.887 1.06e-06 ***
#> unemp -0.14297 0.04373 -3.269 0.001086 **
#> wage 0.47496 0.08860 5.361 8.68e-08 ***
#> tuition 3.16110 0.42604 7.420 1.38e-13 ***
#> education 2.01015 0.06457 31.133 < 2e-16 ***
#> incomehigh 0.31580 0.26308 1.200 0.230042
#> regionwest 1.19619 0.33984 3.520 0.000436 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 7.477 on 4727 degrees of freedom
#> Multiple R-squared: 0.2635, Adjusted R-squared: 0.2618
#> F-statistic: 153.8 on 11 and 4727 DF, p-value: < 2.2e-16
Interpretation.
score for
a one-unit change in that variable, holding the others constant.model_1 vs. model_2: does the effect get
smaller (suggesting confounding)?Q1) Proportion test. Is the proportion of high-income students different from 0.5? (\(H_0\): \(p = 0.5\); \(H_1\): \(p \neq 0.5\).) If p-value \(< 0.05\), conclude the proportion of high-income students is significantly different from 0.5.
x_high <- sum(college$income == "high") # number of high-income students
n_total <- nrow(college) # total number of students
prop_test_q1 <- prop.test(x = x_high, n = n_total, p = 0.5)
prop_test_q1#>
#> 1-sample proportions test with continuity correction
#>
#> data: x_high out of n_total, null probability 0.5
#> X-squared = 850.83, df = 1, p-value < 2.2e-16
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.2752141 0.3012030
#> sample estimates:
#> p
#> 0.2880355
Q2) Proportion test. Is the proportion of students living at home greater than 70%? (\(H_0\): \(p \leq 0.7\); \(H_1\): \(p > 0.7\).) If p-value \(< 0.05\), conclude more than 70% of students live at home.
x_home <- sum(college$home == "yes") # number of students living at home
prop_test_q2 <- prop.test(x = x_home, n = n_total, p = 0.7, alternative = "greater")
prop_test_q2#>
#> 1-sample proportions test with continuity correction
#>
#> data: x_home out of n_total, null probability 0.7
#> X-squared = 325.55, df = 1, p-value < 2.2e-16
#> alternative hypothesis: true p is greater than 0.7
#> 95 percent confidence interval:
#> 0.8107504 1.0000000
#> sample estimates:
#> p
#> 0.8202152
Q3) Multiple regression. Does family income predict students’ test scores after controlling for parents’ education, distance, home, and region? Which variables are statistically significant (\(p < 0.05\))? Are the effects of income and distance positive or negative?
model_q3 <- lm(score ~ income + fcollege + mcollege + distance + home + region, data = college)
summary(model_q3)#>
#> Call:
#> lm(formula = score ~ income + fcollege + mcollege + distance +
#> home + region, data = college)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -22.6793 -6.4663 0.0954 6.3711 21.4955
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 47.94593 0.30943 154.950 < 2e-16 ***
#> incomehigh 1.52070 0.28892 5.263 1.48e-07 ***
#> fcollegeyes 3.77278 0.34438 10.955 < 2e-16 ***
#> mcollegeyes 2.14938 0.39110 5.496 4.10e-08 ***
#> distance -0.13655 0.05309 -2.572 0.0101 *
#> homeyes 2.19001 0.31774 6.893 6.20e-12 ***
#> regionwest -0.62941 0.30327 -2.075 0.0380 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 8.307 on 4732 degrees of freedom
#> Multiple R-squared: 0.08978, Adjusted R-squared: 0.08862
#> F-statistic: 77.79 on 6 and 4732 DF, p-value: < 2.2e-16
Key takeaways