load("/Users/arbaazsandhu/Desktop/RCODE/cps_data.Rdata")
ls()
## [1] "cps_data"
#frequency table
table(cps_data$race)
##
## 1 2 3 4 5
## 623220 94102 117397 55117 13875
#table of proportions which is more informative
prop.table(table(cps_data$race))
##
## 1 2 3 4 5
## 0.68962312 0.10412842 0.12990547 0.06098963 0.01535336
#without memorizing what the numeric values mean, those are difficult to interpret. Let's label the values here.
val_lab(cps_data$race) = num_lab("
1 White
2 Black
3 Hispanic
4 Asian
5 Other
")
#much better
prop.table(table(cps_data$race))
##
## White Black Hispanic Asian Other
## 0.68962312 0.10412842 0.12990547 0.06098963 0.01535336
#same for education
prop.table(table(cps_data$educ))
##
## 1 2 3 4 5
## 0.1127097 0.2995736 0.2722242 0.2007356 0.1147568
val_lab(cps_data$educ) = num_lab("
1 LTHS
2 HS
3 Some College
4 College
5 Advanced
")
prop.table(table(cps_data$educ))
##
## LTHS HS Some College College Advanced
## 0.1127097 0.2995736 0.2722242 0.2007356 0.1147568
#same for labor force status
#NILF = not in labor force
prop.table(table(cps_data$lfstat))
##
## 1 2 3
## 0.59465250 0.02282501 0.38252250
val_lab(cps_data$lfstat) = num_lab("
1 Employed
2 Unemployed
3 NILF
")
prop.table(table(cps_data$lfstat))
##
## Employed Unemployed NILF
## 0.59465250 0.02282501 0.38252250
prop.table(table(cps_data$female))
##
## 0 1
## 0.478829 0.521171
val_lab(cps_data$lfstat) = num_lab("
0 Male
1 Female
")
prop.table(table(cps_data$female))
##
## 0 1
## 0.478829 0.521171
prop.table(table(cps_data$metro))
##
## 0 1
## 0.1923391 0.8076609
val_lab(cps_data$metro) = num_lab("
0 Rural
1 Metropolitan
")
prop.table(table(cps_data$metro))
##
## Rural Metropolitan
## 0.1923391 0.8076609
Let’s check our work
cps_data <- transform(cps_data,
white = ifelse(cps_data$race == 1, 1, 0),
black = ifelse(cps_data$race == 2, 1, 0),
hispanic = ifelse(cps_data$race == 3, 1, 0),
asian = ifelse(cps_data$race == 4, 1, 0),
other = ifelse(cps_data$race == 5, 1, 0)
)
cps_data <- transform(cps_data,
lths = ifelse(cps_data$educ == 1, 1, 0),
hs = ifelse(cps_data$educ == 2, 1, 0),
somecol = ifelse(cps_data$educ == 3, 1, 0),
college = ifelse(cps_data$educ == 4, 1, 0),
advanced = ifelse(cps_data$educ == 5, 1, 0)
)
summary(cps_data$lths)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.1127 0.0000 1.0000
summary(cps_data$hs)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.2996 1.0000 1.0000
summary(cps_data$somecol)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.2722 1.0000 1.0000
summary(cps_data$college)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.2007 0.0000 1.0000
summary(cps_data$advanced)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 0.0000 0.1148 0.0000 1.0000
prop.table(table(cps_data$educ))
##
## LTHS HS Some College College Advanced
## 0.1127097 0.2995736 0.2722242 0.2007356 0.1147568
total2 = mean(cps_data$lths) + mean(cps_data$hs) + mean(cps_data$somecol) + mean(cps_data$college) + mean(cps_data$advanced)
total2
## [1] 1
#race variable first. One dummy variable for each category
#Employment Variable
cps_data <- transform(cps_data,
empl_test = ifelse(cps_data$lfstat == 1, 1, 0),
unem_test = ifelse(cps_data$lfstat == 2, 1, 0),
nilf_test = ifelse(cps_data$lfstat == 3, 1, 0)
)
#Variable Mean
summary(cps_data$empl_test)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 0.000 1.000 0.595 1.000 1.000 3777
summary(cps_data$unem_test)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 0.000 0.000 0.023 0.000 1.000 3777
summary(cps_data$nilf_test)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 0.000 0.000 0.383 1.000 1.000 3777
prop.table(table(cps_data$lfstat))
##
## Female 2 3
## 0.59465250 0.02282501 0.38252250
#Variable_Mean
total3 = mean(cps_data$empl_test, na.rm = TRUE) + mean(cps_data$unem_test, na.rm = TRUE) + mean(cps_data$nilf_test, na.rm = TRUE)
total3
## [1] 1
#Variable for Metropolitan
cps_data <- transform(cps_data,
Rural = ifelse(cps_data$metro == 0, 1, 0),
Metropolitan = ifelse(cps_data$metro == 1, 1, 0)
)
#Lets Check our work
summary(cps_data$metropolitan)
## Length Class Mode
## 0 NULL NULL
summary(cps_data$rural)
## Length Class Mode
## 0 NULL NULL
prop.table(table(cps_data$metro))
##
## Rural Metropolitan
## 0.1923391 0.8076609
total4 = mean(cps_data$Metropolitan, na.rm = TRUE) + mean(cps_data$Rural, na.rm = TRUE)
total4
## [1] 1
#Variable for Male
cps_data <- transform(cps_data,
Male = ifelse(cps_data$female == 0, 1, 0),
Female = ifelse(cps_data$female == 1, 1, 0)
)
#Lets Check our work
summary(cps_data$male)
## Length Class Mode
## 0 NULL NULL
summary(cps_data$female)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 1.0000 0.5212 1.0000 1.0000
prop.table(table(cps_data$female))
##
## 0 1
## 0.478829 0.521171
total4 = mean(cps_data$male, na.rm = TRUE) + mean(cps_data$female, na.rm = TRUE)
## Warning in mean.default(cps_data$male, na.rm = TRUE): argument is not numeric
## or logical: returning NA
total4
## [1] NA
#Solutions for Question #5
wage_f <- mean(cps_data[cps_data$female == "1",]$rw, na.rm = TRUE)
wage_m <- mean(cps_data[cps_data$female == "0",]$rw, na.rm = TRUE)
wage_all <- mean(cps_data$rw, na.rm = TRUE)
prop_f <- mean(cps_data$female, na.rm = TRUE)
prop_m <- 1-prop_f
wage_f
## [1] 22.76413
wage_m
## [1] 28.04034
wage_all
## [1] 25.4453
prop_f
## [1] 0.521171
wage_test <- wage_f*prop_f + wage_m *prop_m
wage_all
## [1] 25.4453
wage_test
## [1] 25.29053
cor.test_race <- cor.test(cps_data$rw, cps_data$race)
cor.test_educ <- cor.test(cps_data$rw, cps_data$educ)
cor.test_married <- cor.test(cps_data$rw, cps_data$married)
cor.test_female <- cor.test(cps_data$rw, cps_data$female)
cor.test_metro <- cor.test(cps_data$rw, cps_data$metro)
print(cor.test_race)
##
## Pearson's product-moment correlation
##
## data: cps_data$rw and cps_data$race
## t = -49.607, df = 476913, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.07447100 -0.06882393
## sample estimates:
## cor
## -0.07164804
print(cor.test_educ)
##
## Pearson's product-moment correlation
##
## data: cps_data$rw and cps_data$educ
## t = 328, df = 476913, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4267077 0.4313391
## sample estimates:
## cor
## 0.4290262
print(cor.test_married)
##
## Pearson's product-moment correlation
##
## data: cps_data$rw and cps_data$married
## t = 142.79, df = 476913, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1997607 0.2052042
## sample estimates:
## cor
## 0.202484
print(cor.test_female)
##
## Pearson's product-moment correlation
##
## data: cps_data$rw and cps_data$female
## t = -94.479, df = 476913, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.1383309 -0.1327590
## sample estimates:
## cor
## -0.135546
print(cor.test_metro)
##
## Pearson's product-moment correlation
##
## data: cps_data$rw and cps_data$metro
## t = 64.62, df = 472747, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.09074520 0.09639644
## sample estimates:
## cor
## 0.09357157
summary(cps_data$race)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.000 1.000 1.608 2.000 5.000
summary(cps_data$educ)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 2.000 3.000 2.905 4.000 5.000
summary(cps_data$married)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 1.0000 0.5277 1.0000 1.0000
summary(cps_data$female)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.0000 1.0000 0.5212 1.0000 1.0000
summary(cps_data$metro)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.000 1.000 1.000 0.808 1.000 1.000 8698
summary(cps_data$rw)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.0 13.0 19.3 25.4 30.5 393.1 426796
Regression model
\(realwage_i = \beta_0+\beta_1Asian_i+\beta_2male_i+\beta_3married_i+\beta_4metro_i+\beta_5college+u_i\)
wage <- lm(rw ~ asian + college + Metropolitan + married + Male, data=cps_data)
summary(wage)
##
## Call:
## lm(formula = rw ~ asian + college + Metropolitan + married +
## Male, data = cps_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -40.76 -10.55 -4.67 4.63 368.51
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.15295 0.07649 171.97 <2e-16 ***
## asian 3.18837 0.10882 29.30 <2e-16 ***
## college 8.86917 0.06322 140.29 <2e-16 ***
## Metropolitan 4.21230 0.07126 59.11 <2e-16 ***
## married 7.22108 0.05406 133.58 <2e-16 ***
## Male 5.12613 0.05372 95.42 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 18.43 on 472743 degrees of freedom
## (430962 observations deleted due to missingness)
## Multiple R-squared: 0.106, Adjusted R-squared: 0.106
## F-statistic: 1.122e+04 on 5 and 472743 DF, p-value: < 2.2e-16