Read “card” dataset into R program. It contains data on 3,010 observations from the National Longitudinal Study (NLS) of young men. Like other longitudinal surveys initiated in the mid 1960s, the NLSYM was not a random sample of US population, rather men from neighborhood with high concentartion of non-white residents were over sampled. And this data contains a relatively high fraction of men form the southern region and high fraction of Black people. Also, there was this Standard Metropolitan Statistical Area (smsa) variable that they used as a proxy for urban and rurality
# let's check the missing in each variablescolSums(is.na(nls_clean))
id nearc2 nearc4 educ age fatheduc motheduc weight
0 0 0 0 0 690 353 0
momdad14 sinmom14 step14 reg661 reg662 reg663 reg664 reg665
0 0 0 0 0 0 0 0
reg666 reg667 reg668 reg669 south66 black smsa south
0 0 0 0 0 0 0 0
smsa66 wage enroll kww iq married libcrd14 exper
0 0 0 47 949 7 13 0
lwage expersq region
0 0 0
Most variables have no missing data, which is excellent for regression. Overall, the dataset is mostly complete but has moderate missingness in parental education and substantial missingness in IQ.
Q1: Run some descriptive statistics in order to get a general idea of the data
Plotting the distribution of raw and log hourly wages
hist(nls_clean$wage, main ="Wage Distribution", xlab ="Hourly Wage")
hist(nls_clean$lwage, main ="Log Wage Distribution", xlab ="Log Hourly Wage")
Raw hourly wage: The distribution is right-skewed, with a long tail of high earners. Log wage distribution appears approximately normal, supporting the use of OLS regression with log wages as the outcome.
Scatterplots showing the relationship between key predictors and log wages.
library(ggplot2)# Education vs. log wageggplot(nls_clean, aes(x = educ, y = lwage)) +geom_point(alpha =0.3) +geom_smooth(method ="lm", col ="blue") +labs(title ="Education vs. Log Wage")
`geom_smooth()` using formula = 'y ~ x'
# Experience vs. log wageggplot(nls_clean, aes(x = exper, y = lwage)) +geom_point(alpha =0.3) +geom_smooth(method ="lm", col ="darkgreen") +labs(title ="Experience vs. Log Wage")
`geom_smooth()` using formula = 'y ~ x'
# Experience Squared vs. log wageggplot(nls_clean, aes(x = expersq, y = lwage)) +geom_point(alpha =0.3) +geom_smooth(method ="lm", col ="darkgreen") +labs(title ="Experience vs. Log Wage")
`geom_smooth()` using formula = 'y ~ x'
Years of schooling vs. Log Wage shows a positive linear trend. Experience vs. Log Wage is expected to show a concave (nonlinear) pattern Experience Squared vs. Log Wage helps illustrate the diminishing returns of experience on wages.
# Categorize the treatment variable (education) to group individuals based on thatnls_clean$educ_level <-cut(nls_clean$educ,breaks =c(0, 11, 15, 18),labels =c("Less than HS", "HS Grad", "College or More"),right =TRUE,include.lowest =TRUE)table(nls_clean$educ_level)
Less than HS HS Grad College or More
497 1696 817
Generate a descriptive summary table grouped by education level (educ_level).
This table excludes ID, raw education values, and regional dummy variables.
Characteristic
Less than HS
N = 4971
HS Grad
N = 1,6961
College or More
N = 8171
Near 2-year college
214 (43%)
733 (43%)
380 (47%)
Near 4_year college
279 (56%)
1,172 (69%)
602 (74%)
Age (years)
29.0 (26.0, 31.0)
27.0 (25.0, 30.0)
28.0 (26.0, 31.0)
Father's Schooling
7.0 (4.0, 9.0)
10.0 (8.0, 12.0)
12.0 (10.0, 14.0)
Missing
187
401
102
Mother's Schooling
8.0 (5.0, 10.0)
11.0 (8.0, 12.0)
12.0 (11.0, 13.0)
Missing
108
200
45
Weight (lb)
140,524 (99,110, 378,045)
364,640 (127,041, 397,706)
373,482 (352,463, 426,142)
momdad14
334 (67%)
1,312 (77%)
730 (89%)
sinmom14
80 (16%)
176 (10%)
47 (5.8%)
step14
17 (3.4%)
82 (4.8%)
18 (2.2%)
south66
325 (65%)
666 (39%)
256 (31%)
Race
Non-Black
264 (53%)
1,308 (77%)
735 (90%)
Black
233 (47%)
388 (23%)
82 (10%)
Standard Metropolitan Statistical Area(1976)
Non-Urban
223 (45%)
465 (27%)
176 (22%)
Urban
274 (55%)
1,231 (73%)
641 (78%)
Live in South(1976)
Non-South
185 (37%)
1,053 (62%)
557 (68%)
South
312 (63%)
643 (38%)
260 (32%)
Standard Metropolitan Statistical Area(1966)
256 (52%)
1,133 (67%)
566 (69%)
wage
400 (300, 549)
535 (400, 700)
635 (466, 817)
enroll
6 (1.2%)
159 (9.4%)
113 (14%)
Knowledge of the World of Work
26 (20, 32)
34 (28, 39)
39 (34, 43)
Missing
15
25
7
IQ Score
86 (75, 94)
100 (91, 108)
113 (104, 120)
Missing
344
475
130
Marital Status
1
358 (72%)
1,234 (73%)
552 (68%)
2
7 (1.4%)
7 (0.4%)
0 (0%)
3
1 (0.2%)
1 (<0.1%)
1 (0.1%)
4
26 (5.3%)
95 (5.6%)
34 (4.2%)
5
23 (4.7%)
60 (3.5%)
19 (2.3%)
6
79 (16%)
296 (17%)
210 (26%)
Missing
3
3
1
libcrd14
178 (36%)
1,147 (68%)
696 (85%)
Missing
5
6
2
Experience (years)
13.0 (10.0, 16.0)
8.0 (7.0, 11.0)
5.0 (3.0, 8.0)
Log Hourly Wage
5.99 (5.70, 6.31)
6.28 (5.99, 6.55)
6.45 (6.14, 6.71)
expersq
169 (100, 256)
64 (49, 121)
25 (9, 64)
Region (1966)
EN Central
14 (2.8%)
118 (7.0%)
61 (7.5%)
Mountain
81 (16%)
178 (10%)
72 (8.8%)
NC
51 (10%)
265 (16%)
168 (21%)
NE
16 (3.2%)
86 (5.1%)
38 (4.7%)
Other
22 (4.4%)
163 (9.6%)
87 (11%)
Pacific
5 (1.0%)
50 (2.9%)
30 (3.7%)
South Atlantic
64 (13%)
348 (21%)
177 (22%)
South Central
77 (15%)
156 (9.2%)
56 (6.9%)
WN Central
167 (34%)
332 (20%)
128 (16%)
1n (%); Median (Q1, Q3)
Q2:Estimate an OLS regression with the log of wage as the dependent variable and education as the independent variable. In another regression model, estimate the impact of education on log wage adjusting for experience, black race, living in the south, and living in a standard metropolitan statistical area.
OLS regression model (unadjusted)
# Model 1: Simple Linear Regression# Estimate the effect of education on log wage (lwage)model1 <-lm(lwage ~ educ, data = nls_clean)summary(model1)
Call:
lm(formula = lwage ~ educ, data = nls_clean)
Residuals:
Min 1Q Median 3Q Max
-1.73799 -0.27764 0.02373 0.28839 1.46080
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.57088 0.03883 143.47 <2e-16 ***
educ 0.05209 0.00287 18.15 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4214 on 3008 degrees of freedom
Multiple R-squared: 0.09874, Adjusted R-squared: 0.09844
F-statistic: 329.5 on 1 and 3008 DF, p-value: < 2.2e-16
exp(coef(model1))
(Intercept) educ
262.665780 1.053475
Adjusted OLS (add covariates)
# Model 2: Multiple Linear Regression# Estimate the effect of education on lwage adjusting for experience, race, and geography:# Model 2: Add experience, race, region, and urbanicity as controlsmodel2 <-lm(lwage ~ educ + exper + expersq + black + south + smsa, data = nls_clean)summary(model2)
Call:
lm(formula = lwage ~ educ + exper + expersq + black + south +
smsa, data = nls_clean)
Residuals:
Min 1Q Median 3Q Max
-1.59297 -0.22315 0.01893 0.24223 1.33190
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.7336643 0.0676026 70.022 < 2e-16 ***
educ 0.0740090 0.0035054 21.113 < 2e-16 ***
exper 0.0835958 0.0066478 12.575 < 2e-16 ***
expersq -0.0022409 0.0003178 -7.050 2.21e-12 ***
blackBlack -0.1896315 0.0176266 -10.758 < 2e-16 ***
southSouth -0.1248615 0.0151182 -8.259 < 2e-16 ***
smsaUrban 0.1614230 0.0155733 10.365 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.3742 on 3003 degrees of freedom
Multiple R-squared: 0.2905, Adjusted R-squared: 0.2891
F-statistic: 204.9 on 6 and 3003 DF, p-value: < 2.2e-16
In this adjusted OLS model, we estimated the effect of education on log hourly wages while controlling for cognitive ability (IQ), experience squared, race, region, and urban location. The coefficient for education is 0.074, indicating that each additional year of schooling is associated with approximately a 7.4% increase in log wages, holding other variables constant.
We can argue that people with more education also have other advantages, like higher motivation, better family support, or stronger abilities, that help them earn more. These things (which we often can’t measure) can confuse the results. So, part of what OLS captures may not be the effect of education itself, but the effect of these unseen advantages.
We therefore use “geographical proximity to a college when growing up” as an exogenous instrument for education. Although it’s a useful strategy to select an effective instrument or instruments for each endogenous explanatory variable, in 2SLS regression all of the instrumental variables are used to estimate all of the regression coefficients in the model.
Q5: Estimate the effect of education on the log wage using instrumental variables. Compare the results from this exercise to those from question 2.
library(AER)
Warning: package 'AER' was built under R version 4.3.3
Loading required package: car
Warning: package 'car' was built under R version 4.3.3
Loading required package: carData
Warning: package 'carData' was built under R version 4.3.3
Attaching package: 'car'
The following object is masked from 'package:dplyr':
recode
The following object is masked from 'package:purrr':
some
Loading required package: lmtest
Warning: package 'lmtest' was built under R version 4.3.3
Loading required package: zoo
Warning: package 'zoo' was built under R version 4.3.3
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
Loading required package: sandwich
Warning: package 'sandwich' was built under R version 4.3.3
Loading required package: survival
Warning: package 'survival' was built under R version 4.3.3
# IV estimation: using nearc4 as an instrument for educ# Other covariates (exper, expersq, black, south, smsa) are assumed to be exogenous and are included on both sides of the formulaiv_model <-ivreg(lwage ~ educ + exper + expersq + black + south + smsa | nearc4 + exper + expersq + black + south + smsa, data = nls_clean)summary(iv_model)
Call:
ivreg(formula = lwage ~ educ + exper + expersq + black + south +
smsa | nearc4 + exper + expersq + black + south + smsa, data = nls_clean)
Residuals:
Min 1Q Median 3Q Max
-1.82125 -0.24065 0.02368 0.25469 1.43205
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.7527813 0.8293409 4.525 6.27e-06 ***
educ 0.1322888 0.0492332 2.687 0.00725 **
exper 0.1074980 0.0213006 5.047 4.76e-07 ***
expersq -0.0022841 0.0003341 -6.836 9.84e-12 ***
blackBlack -0.1308019 0.0528723 -2.474 0.01342 *
southSouth -0.1049005 0.0230731 -4.546 5.67e-06 ***
smsaUrban 0.1313237 0.0301298 4.359 1.35e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.391 on 3003 degrees of freedom
Multiple R-Squared: 0.2252, Adjusted R-squared: 0.2237
Wald test: 120.8 on 6 and 3003 DF, p-value: < 2.2e-16
modsum <-summary(iv_model, diagnostics =TRUE)
In this model, proximity to a 4-year college (nearc4) was used as an instrument for education, under the assumption that it affects wages only through its influence on educational attainment. The IV estimation shows that each additional year of education increases log wages by approximately 0.132, or about 13.2%, which is substantially higher than the estimate from the adjusted OLS model (7.4% per year). This suggests that the OLS model may underestimate the true causal return to education due to unobserved confounding factors like ability or family background. The IV model also confirms that wage outcomes are shaped by geography and race: being Black is associated with a 12% wage penalty, living in the South corresponds to an 11% reduction, and residing in an urban area is linked to a 13% wage premium. Interestingly, the squared experience term is positively associated with wages here, possibly reflecting model differences due to instrument usage. Overall, the IV results support the idea that accounting for endogeneity reveals a stronger causal effect of education on earnings than initially observed with OLS.
Q6: Use 2-stage least squares to estimate the effect of education on log wage doing each stage separately. What is the F-statistic from the 1st stage? How do your results from the 2nd stage compare to those from question 5?
# First-Stage Regression:# Regress years of education on the instrument (nearc4) and control variables.first_stage <-lm(educ ~ nearc4 + exper + expersq + black + south + smsa, data = nls_clean)summary(first_stage)
Call:
lm(formula = educ ~ nearc4 + exper + expersq + black + south +
smsa, data = nls_clean)
Residuals:
Min 1Q Median 3Q Max
-7.6419 -1.3928 -0.0953 1.2638 6.2692
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 16.6591746 0.1763889 94.446 < 2e-16 ***
nearc4Yes 0.3373208 0.0825004 4.089 4.45e-05 ***
exper -0.4100081 0.0336939 -12.169 < 2e-16 ***
expersq 0.0007323 0.0016499 0.444 0.657201
blackBlack -1.0061383 0.0896454 -11.224 < 2e-16 ***
southSouth -0.2914640 0.0792247 -3.679 0.000238 ***
smsaUrban 0.4038769 0.0848872 4.758 2.05e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.943 on 3003 degrees of freedom
Multiple R-squared: 0.4745, Adjusted R-squared: 0.4734
F-statistic: 451.9 on 6 and 3003 DF, p-value: < 2.2e-16
# Generate predicted values of education from the first-stage regression.# These fitted values (educ_hat) represent the variation in education explained by the instrument (nearc4) and controls.# We'll use educ_hat as a proxy for actual education in the second-stage regression to estimate the causal effect on log wages.nls_clean$educ_hat <-predict(first_stage)
# Second-stage regression of 2SLS:# Regress log wages (lwage) on the predicted education values (educ_hat) from the first stage,# along with experience, experience squared, race, region, and urbanicity as control variables.# This estimates the causal effect of education on wages, correcting for endogeneity using nearc4 as an instrument.second_stage <-lm(lwage ~ educ_hat + exper + expersq + black + south + smsa, data = nls_clean)summary(second_stage)
Call:
lm(formula = lwage ~ educ_hat + exper + expersq + black + south +
smsa, data = nls_clean)
Residuals:
Min 1Q Median 3Q Max
-1.56525 -0.24771 0.01465 0.27091 1.38743
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.7527813 0.8495104 4.418 1.03e-05 ***
educ_hat 0.1322888 0.0504306 2.623 0.00876 **
exper 0.1074980 0.0218186 4.927 8.81e-07 ***
expersq -0.0022841 0.0003423 -6.674 2.96e-11 ***
blackBlack -0.1308019 0.0541582 -2.415 0.01579 *
southSouth -0.1049005 0.0236342 -4.438 9.39e-06 ***
smsaUrban 0.1313237 0.0308626 4.255 2.15e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4005 on 3003 degrees of freedom
Multiple R-squared: 0.1871, Adjusted R-squared: 0.1854
F-statistic: 115.2 on 6 and 3003 DF, p-value: < 2.2e-16
We found that a one-unit increase in the predicted years of schooling is associated with a 13.2% increase in log wages (coefficient = 0.132)
compares the coefficients and diagnostics (e.g., R², AIC, BIC) of the adjusted OLS model and the IV model side-by-side, making it easy to interpret differences in estimated effects of education when accounting for potential endogeneity.
library(modelsummary)
Warning: package 'modelsummary' was built under R version 4.3.3
#The change in coefficients and associated standard errors can also be brought out graphically using the modelplot() function from modelsummary which shows the coefficient estimates along with their 95% confidence intervals.
library(modelsummary)# Create the model listm_list <-list("OLS (Adjusted)"= model2,"IV (2SLS)"= iv_model)# Plot excluding intercept and experience termsmodelplot(m_list, coef_omit ="Intercept|experience")