set more off /* Chapter 1 - Simple & Multiple Regression */ use http://www.ats.ucla.edu/stat/stata/webbooks/reg/elemapi2, clear * mkdir regstata * cd regstata save elemapi2, replace describe list in 1/5 list api00 acs_k3 meals full in 1/10 codebook api00 acs_k3 meals full yr_rnd summarize api00 acs_k3 meals full net from http://www.ats.ucla.edu/stat/stata/ado/analysis net install univar univar api00 acs_k3 meals full /* Simple Linear Regression */ corr api00 enroll graph twoway scatter api00 enroll scatter api00 enroll regress api00 enroll predict fv list api00 fv in 1/10 scatter api00 fv enroll, connect(. l) symbol(o i) sort scatter api00 fv enroll, connect(. l) symbol(o i) mlabel(snum) sort twoway (scatter api00 enroll) (lfit api00 enroll) /* Multiple Regression */ corr api00 ell meals yr_rnd mobility acs_k3 acs_46 full emer enroll pcorr api00 ell meals yr_rnd mobility acs_k3 acs_46 full emer enroll regress api00 ell meals yr_rnd mobility acs_k3 acs_46 full emer enroll regress api00 ell meals yr_rnd mobility acs_k3 acs_46 full emer enroll, beta *** findit listcoef listcoef test ell test acs_k3 acs_46 /* Transforming Variables */ graph twoway histogram enroll histogram enroll, normal bin(20) histogram enroll, normal bin(20) xlabel(0(200)1600) kdensity enroll, normal symplot enroll qnorm api00 pnorm api00 ladder enroll gladder enroll generate lenroll = log(enroll) histogram lenroll, normal bin(20) /* Chapter 2 - Regression diagnostics */ use http://www.ats.ucla.edu/stat/stata/webbooks/reg/crime, clear describe summarize crime murder pctmetro pctwhite pcths poverty single graph matrix pctmetro poverty single crime scatter crime pctmetro, mlabel(state) scatter crime poverty, mlabel(state) scatter crime single, mlabel(state) regress crime pctmetro poverty single *** findit rvfplot2 rvfplot2, rstu * get index plot from ssc ssc install indexplot, replace indexplot rstudent, scatter yline(0) indexplot rstudent, hig(3) low(3) scatter yline(-2.5 0 2.5) mlabel(state) indexplot leverage, hig(3) scatter mlabel(state) indexplot cooksd, hig(3) scatter yline(1) mlabel(state) indexplot dfbeta(poverty), hig(3) scatter mlabel(state) indexplot dfbeta(single), hig(3) scatter mlabel(state) lvr2plot, mlabel(state) regress regress crime pctmetro poverty single if state!="dc" /* Checking for Normality of Residuals */ use http://www.ats.ucla.edu/stat/stata/webbooks/reg/elemapi2, clear regress api00 meals ell emer predict r, resid kdensity r, normal pnorm r qnorm r swilk r /* Checking for Heteroscedasticity */ rvfplot, yline(0) hettest * install whitetst from ssc ssc install whitetst whitetst regress api00 enroll rvfplot generate lenroll = log(enroll) regress api00 lenroll rvfplot regress api00 meals ell emer enroll rvfplot /* Checking for Collinearity */ regress api00 acs_k3 avg_ed grad_sch col_grad some_col vif regress api00 acs_k3 grad_sch col_grad some_col vif *** findit collin collin acs_k3 avg_ed grad_sch col_grad some_col collin acs_k3 grad_sch col_grad some_col /* Checking Model Specification */ regress api00 acs_k3 linktest ovtest regress api00 acs_k3 full meals linktest ovtest /* Chapter 4 - Robust Regression Methods */ /* Robust Standard Errors */ regress api00 acs_k3 acs_46 full enroll lvr2plot regress api00 acs_k3 acs_46 full enroll, robust tabulate dnum regress api00 acs_k3 acs_46 full enroll, cluster(dnum) /* Robust Regression */ use http://www.ats.ucla.edu/stat/stata/webbooks/reg/crime, clear regress crime pctmetro poverty single rvfplot2, yline(0) rreg crime pctmetro poverty single, gen(wt) rvfplot2, resid yline(0) sort wt list wt state crime pctmetro poverty single /* Chapter 3 - Regression with categorical predictors */ /* Chapter 5 - Additional coding systems for categorical variables */ use http://www.ats.ucla.edu/stat/stata/webbooks/reg/elemapi2, clear describe api00 some_col yr_rnd mealcat codebook api00 some_col yr_rnd mealcat /* 3.1 Regression with a 0/1 variable */ regress api00 yr_rnd twoway (scatter api00 yr_rnd) (lfit api00 yr_rnd) tabulate yr_rnd, sum(api00) ttest api00, by(yr_rnd) di "square t-test = " r(t)^2 anova api00 yr_rnd /* Regression with a 1/2/3 variable Manually Creating Dummy Variables */ codebook mealcat regress api00 mealcat tabulate mealcat, gen(mealcat) list mealcat mealcat1 mealcat2 mealcat3 in 1/10, nolabel regress api00 mealcat2 mealcat3 test mealcat2 mealcat3 tabulate mealcat, summarize(api00) /* Using the xi & xi3 commands */ xi: regress api00 i.mealcat test _Imealcat_2 _Imealcat_3 regress api00 mealcat1 mealcat2 char mealcat[omit] 3 xi: regress api00 i.mealcat test _Imealcat_1 _Imealcat_2 anova api00 mealcat anova, regress *** findit xi3 /* simple coding */ xi3: regress api00 g.mealcat test _Imealcat_1 _Imealcat_2 describe _Imealcat_1 _Imealcat_2 /* effect coding */ xi3: regress api00 e.mealcat test _Imealcat_1 _Imealcat_2 describe _Imealcat_1 _Imealcat_2 /* orthogonal coding */ xi3: regress api00 o.mealcat test _Imealcat_1 _Imealcat_2 describe _Imealcat_1 _Imealcat_2 /* Categorical predictor with interactions */ xi: regress api00 i.mealcat*yr_rnd test _ImeaXyr_rn_1 _ImeaXyr_rn_2 test _Imealcat_1 _Imealcat_2 anova api00 yr_rnd mealcat yr_rnd*mealcat xi3: regress api00 g.mealcat*g.yr_rnd test _Ime1Xyr1 _Ime2Xyr1 test _Imealcat_1 _Imealcat_2 *** findit postgr3 postgr3 mealcat, by(yr_rnd) /* Continuous and Categorical variables */ xi3: regress api00 g.yr_rnd some_col predict yhat scatter yhat some_col postgr3 some_col, by(yr_rnd) anova api00 yr_rnd some_col, cont(some_col) anova, regress xi3: regress api00 g.yr_rnd*some_col postgr3 some_col, by(yr_rnd)