Advertisement
Guest User

Single-motherhood vs percent black

a guest
Oct 31st, 2015
211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # install.packages("readstata13")
  2. library(readstata13)
  3. # download tables 1 and 3 from the 2015 paper section
  4. tbl1 = read.dta13("nbhds_online_data_table1.dta")
  5. tbl3 = read.dta13("nbhds_online_data_table3.dta")
  6. cz = merge(tbl1,tbl3,by="cz")
  7.  
  8. # install.packages("psych")
  9. library(psych)
  10.  
  11. cz$p25_mobility_scaled = scale(cz$pct_causal_p25_kr26)
  12. cz$p75_mobility_scaled = scale(cz$pct_causal_p75_kr26)
  13. cz$pct_black_scaled = scale(cz$cs_race_bla)
  14. cz$poverty_rate_scaled = scale(cz$poor_share)
  15. cz$seg_index_scaled = scale(cz$cs_race_theil_2000)
  16. cz$single_mom_rate_scaled = scale(cz$cs_fam_wkidsinglemom)
  17. cz$adj_test_scores_scaled = scale(cz$score_r)
  18.  
  19. cz$pct_black_log10 = log(cz$cs_race_bla,10)
  20.  
  21. selcols = c('p25_mobility_scaled','p75_mobility_scaled',
  22.             'poverty_rate_scaled','seg_index_scaled',
  23.             'adj_test_scores_scaled',
  24.             'pct_black_scaled','single_mom_rate_scaled')
  25. pairs.panels(subset(cz,select=selcols),main="Chetty 2015 CZ-level 'causal' mobility & covariates")
  26.  
  27. czlm1 = lm( p25_mobility_scaled ~ single_mom_rate_scaled,cz)
  28. czlm2 = lm( p25_mobility_scaled ~ pct_black_scaled,cz)
  29. czlm3 = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,cz)
  30.  
  31. czlm3_w = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,cz,weights=pop2000)
  32.  
  33.  
  34. czlm4 = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled,cz)
  35. czlm4_w = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled,cz,weights=pop2000)
  36.  
  37. cz$lm4_pred = predict(czlm4)
  38. cz$lm4_w_pred = predict(czlm4_w)
  39.  
  40. ggplot(cz,aes(x=lm4_pred,y=p25_mobility_scaled)) + geom_point() + geom_smooth()
  41.  
  42.  
  43. ggplot(cz,aes(x=single_mom_rate_scaled,y=p25_mobility_scaled)) + geom_point(aes(size=pop2000),colour="blue") + geom_smooth() + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty CZ-level 25th percentile mobility by percent single-mother")
  44.  
  45. ggplot(cz,aes(x=pct_black_scaled,y=p25_mobility_scaled)) + geom_point(aes(size=pop2000),colour="blue") + geom_smooth() + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty CZ-level 25th percentile mobility by percent black")
  46.  
  47. ggplot(cz,aes(x=pct_black_scaled,y=single_mom_rate_scaled)) + geom_point(aes(size=pop2000),colour="blue") + geom_smooth() + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty CZ-level percent single-mother by percent black")
  48.  
  49.  
  50.  
  51. lmloess_sm = loess(p25_mobility_scaled ~ single_mom_rate_scaled, cz)
  52. cz$loess_sm_pred = predict(lmloess_sm)
  53. cz$loess_sm_overpred = cz$loess_sm_pred - cz$p25_mobility_scaled
  54.  
  55. ggplot(cz,aes(x=pct_black_scaled,y=loess_sm_overpred)) + geom_point(aes(size=pop2000),colour="blue") + geom_smooth(method='lm',color="red") + geom_smooth(color="green") + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty CZ-level: single-mother/loess model residuals by percent black")
  56.  
  57. lmloess_black = loess(p25_mobility_scaled ~ pct_black_scaled, cz)
  58. cz$loess_black_pred = predict(lmloess_black)
  59. cz$loess_black_overpred = cz$loess_black_pred - cz$p25_mobility_scaled
  60.  
  61. ggplot(cz,aes(x=single_mom_rate_scaled,y=loess_black_overpred)) + geom_point(aes(size=pop2000),colour="blue") + geom_smooth(method='lm',color="red") + geom_smooth(color="green") + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty CZ-level: black/loess model residuals by percent single-mother")
  62.  
  63.  
  64. library(data.table)
  65. # take top 100 largest CZ
  66. cztop = data.table(cz)[order(-pop2000)][1:100]
  67.  
  68. cztop$p25_mobility_scaled = scale(cztop$pct_causal_p25_kr26)
  69. cztop$p75_mobility_scaled = scale(cztop$pct_causal_p75_kr26)
  70. cztop$pct_black_scaled = scale(cztop$cs_race_bla)
  71. cztop$poverty_rate_scaled = scale(cztop$poor_share)
  72. cztop$seg_index_scaled = scale(cztop$cs_race_theil_2000)
  73. cztop$single_mom_rate_scaled = scale(cztop$cs_fam_wkidsinglemom)
  74. cztop$adj_test_scores_scaled = scale(cztop$score_r)
  75. cztop$gini_scaled = scale(cztop$gini)
  76. selcols = c('p25_mobility_scaled','p75_mobility_scaled',
  77.             'poverty_rate_scaled','seg_index_scaled',
  78.             'adj_test_scores_scaled','gini_scaled',
  79.             'pct_black_scaled','single_mom_rate_scaled')
  80. pairs.panels(subset(cztop,select=selcols),main="Chetty 2015 top-100 CZ: 'causal' mobility & covariates")
  81.  
  82. cztoplm3 = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,cztop)
  83. cztoplm3_w = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,cztop, weights=pop2000)
  84. cztoplm4 = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled,cztop)
  85.  
  86. library(gtools)
  87. cz$percent_black_quintile = quantcut(cz$cs_race_bla,seq(0,1,by=0.20))
  88. ggplot(cz,aes(x=loess_sm_pred,y=p25_mobility_scaled,color=black_quintile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 CZ-level: p25 mobility by single-motherhood/loess model estimate\ngrouped by quintiles of CZ percent black")
  89. #### county level
  90.  
  91.  
  92. tbl2 = read.dta13("nbhds_online_data_table2.dta")
  93. tbl4 = read.dta13("nbhds_online_data_table4.dta")
  94. ct = merge(tbl2,tbl4,by="cty2000")
  95. ct$p25_mobility_scaled = scale(ct$pct_causal_p25_kr26)
  96. ct$p75_mobility_scaled = scale(ct$pct_causal_p75_kr26)
  97. ct$pct_black_scaled = scale(ct$cs_race_bla)
  98. ct$poverty_rate_scaled = scale(ct$poor_share)
  99. ct$seg_index_scaled = scale(ct$cs_race_theil_2000)
  100. ct$single_mom_rate_scaled = scale(ct$cs_fam_wkidsinglemom)
  101. ct$adj_test_scores_scaled = scale(ct$score_r)
  102.  
  103. selcols = c('p25_mobility_scaled','p75_mobility_scaled',
  104.             'poverty_rate_scaled','seg_index_scaled',
  105.             'adj_test_scores_scaled',
  106.             'pct_black_scaled','single_mom_rate_scaled')
  107. pairs.panels(subset(ct,select=selcols),main="Chetty 2015 county-level 'causal' mobility & covariates")
  108.  
  109.  
  110. ctlm3 = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,ct)
  111.  
  112. ctlm3_w = lm( p25_mobility_scaled ~ single_mom_rate_scaled + pct_black_scaled,ct,weights=cty_pop2000)
  113.  
  114.  
  115. ctlm4 = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled,ct)
  116. ctlm4_w = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled,ct,weights=cty_pop2000)
  117.  
  118.  
  119. ctlmloess_sm = loess(p25_mobility_scaled ~ single_mom_rate_scaled, ct)
  120. ct$loess_sm_pred = predict(ctlmloess_sm)
  121. ct$loess_sm_overpred = ct$loess_sm_pred - ct$p25_mobility_scaled
  122.  
  123. ggplot(ct,aes(x=pct_black_scaled,y=loess_sm_overpred)) + geom_point(aes(size=cty_pop2000),colour="blue") + geom_smooth(method='lm',color="red") + geom_smooth(color="green") + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty County-level: single-mother/loess model residuals by percent black")
  124.  
  125. ctlmloess_black = loess(p25_mobility_scaled ~ pct_black_scaled, ct)
  126. ct$loess_black_pred = predict(ctlmloess_black)
  127. ct$loess_black_overpred = ct$loess_black_pred - ct$p25_mobility_scaled
  128.  
  129. ggplot(ct,aes(x=single_mom_rate_scaled,y=loess_black_overpred)) + geom_point(aes(size=cty_pop2000),colour="blue") + geom_smooth(method='lm',color="red") + geom_smooth(color="green") + scale_x_continuous(breaks=seq(-3,3)) + scale_y_continuous(breaks=seq(-3,3)) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + ggtitle("Chetty County-level: black/loess model residuals by percent single mother")
  130.  
  131.  
  132. ctloess_exp= loess(p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled, ct)
  133. ct$exp_pred = predict(ctloess_exp)
  134. ct$exp_overpred = ct$exp_pred - ct$p25_mobility_scaled
  135.  
  136. library(gtools)
  137. ct$black_quintile = quantcut(ct$cs_race_bla,seq(0,1,by=0.20))
  138. ct$black_decile = quantcut(ct$cs_race_bla,seq(0,1,by=0.1))
  139.  
  140. ct$single_mother_quintile = quantcut(ct$cs_fam_wkidsinglemom,seq(0,1,by=0.20))
  141. ct$single_mother_decile = quantcut(ct$cs_fam_wkidsinglemom,seq(0,1,by=0.1))
  142.  
  143.  
  144. ggplot(ct,aes(x=loess_sm_pred,y=p25_mobility_scaled,color=black_quintile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by single-motherhood/loess model estimate\ngrouped by quintiles of county percent black")
  145.  
  146. ggplot(ct,aes(x=loess_sm_pred,y=p25_mobility_scaled,color=black_decile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by single-motherhood/loess model estimate\ngrouped by deciles of county percent black")
  147.  
  148. ct$ctlm4_pred = predict(ctlm4)
  149. ct$ctlm4_overpred = ct$ctlm4_pred - ct$p25_mobility_scaled
  150.  
  151. ctlm_6 = lm( p25_mobility_scaled ~ single_mom_rate_scaled * pct_black_scaled + I(single_mom_rate_scaled^2),ct)
  152.  
  153.  
  154. ggplot(ct,aes(x=loess_black_pred,y=p25_mobility_scaled,color=single_mother_quintile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by loess/black-model prediction\ngrouped by quintiles of county percent single mother")
  155. ggplot(ct,aes(x=loess_black_pred,y=p25_mobility_scaled,color=single_mother_decile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by loess/black-model prediction\ngrouped by deciles of county percent single mother")
  156.  
  157. ggplot(ct,aes(x=ctlm4_pred,y=p25_mobility_scaled,color=single_mother_decile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by interaction model estimate\ngrouped by deciles of county percent single-mother")
  158.  
  159. ggplot(ct,aes(x=ctlm4_pred,y=p25_mobility_scaled,color=black_decile)) + geom_point() + geom_smooth(method=lm,size=3) + ggtitle("Chetty 2015 county-level: p25 mobility by interaction model estimate\ngrouped by deciles of county percent black")
Advertisement
RAW Paste Data Copied
Advertisement