clear all
version 14.1
set rmsg on
cap log close
log using "Week5_lecture", replace
/* Quiz*/
/* Review: */
* Filling in missing names from raw_names (after using tsfill) :
* bys ename: replace name = name[_N] if name==""
/* Week 5 Lecture */
cd "/users/belenchavez/desktop"
use firm, clear
tab hired rank
/******************************************************************************/
/* LPM model (OLS) */
/******************************************************************************/
reg hired exp gpa i.rank male minority, r
predict yhat_ols
summ yhat_ols
/* A one unit change in experience increases prob of hiring by .177
Going from rank 1 to rank 2 decreases prob of hiring by 0.159 */
/* What does model estimate for Female non-minority from rank 1? */
/* Assume gpa = 3.8 and exp = 2 */
di _b[_cons]+_b[gpa]*3.8+_b[exp]*2
//62.3%
* Could have also used lincom:
lincom gpa*3.8+exp*2 + 1.rank*1+_cons
lincom gpa*3.8+exp*2 +_cons
//62.3%
/* Estimate effect on hired if exp goes up by half a year? (same from 0.5 to 1, or 1 to 1.5, etc. Linear. */
di _b[exp]*0.5
*8.8%
margins, dydx(exp)
/******************************************************************************/
/*Probit regression */
/******************************************************************************/
probit hired exp gpa i.rank male minority
* Rank is individually significant. Is it jointly significant?
test 2.rank 3.rank 4.rank
//Yes. Jointly Significant
* What about male and minority? Not individually sig, are they jointly sig?
test minority male
*Remove male and minority from hired. They are not statistically sig different than 0.
probit hired exp gpa i.rank
//The probit regression coefficients give the change in the z-score or probit index for a one unit change in the predictor.
//For a one unit increase in experience, the z-score increases by 0.55
//For a one unit increase in gpa, the z-score increases by .4777302
/* Now, let's look at a person's chance of being hired if they have 3.8 GPA and went to a top-tier (rank 1 school)
What is the effect of a half-year of experience? */
/*Let's look at non-linearity. Is half year of experience the same over all intervals? */
di normal(_b[_cons]+_b[gpa]*3.8+_b[exp]*2)-normal(_b[_cons]+_b[gpa]*3.8+_b[exp]*1.5)
//10.14%. Higher than the 8.8% the LPM predicted
di normal(_b[_cons]+_b[gpa]*3.8+_b[exp]*.75)-normal(_b[_cons]+_b[gpa]*3.8+_b[exp]*.25)
//Can also do the above by using the command margins. This is on your hw assignment.
//10.47%
/* Predicted probabilities of being hired at each level of rank holding all other
variables in the model at their means. */
margins rank, atmeans
/* Average Predicted probabilities calculated using sample values of other predictor variables */
margins rank
* For margins we could also hold other variables at certain values, for example GPA = 3.5
margins rank, at(gpa = 3.5) atmeans
marginsplot
//Predicted probabilities of being hired holding gpa at 3.5 and all other values at their means
* We can also look at interactions:
probit hired exp gpa i.rank i.male
margins rank##male
marginsplot
* We can look at average marginal effects
margins, dydx(exp)
probit hired exp gpa i.rank
predict yhat_probit
gen yhat_probit_manual = normal(_b[exp]*exp+_b[gpa]*gpa+_b[2.rank]*2.rank+_b[3.rank]*3.rank+_b[4.rank]*4.rank + _b[_cons])
assert yhat_probit == yhat_probit_m
//Can use those predicted values to assess how our model predicted hiring.
/******************************************************************************/
/*Logit regression */
/******************************************************************************/
logistic hired exp gpa i.rank
* Note odds ratios are reported
logit
//The logistic regression coefficients give the change in the log odds of the outcome
//for a one unit increase in the predictor variable.
* For a one unit increase in gpa, the log odds of being hired increases by 0.804.
di exp(.8040377) // To get odds-ratio
* Can get back to odds-ratio by typing:
logit, or
di log(2.234545) //To get what we had in log-odds
* For a one unit increase in gpa, the odds of being hired increase by a factor of 2.23
/*What's the change of being hired for half a year? more experience? */
di exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*2)/(1+exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*2)) - ///
(exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*1.5)/(1+exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*1.5)))
* 10.17357%
di exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*.75)/(1+exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*.75)) - ///
(exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*.25)/(1+exp(_b[_cons]+_b[gpa]*3.8+_b[exp]*.25)))
*10.6%
* Can also use margins:
/* Predicted probabilities of being hired at each level of rank holding all other
variables in the model at their means. */
* Margins for hired
margins rank, atmeans
* Average marginal effects of experience
margins, dydx(exp)
* For margins we could also hold other variables at certain values, for example GPA = 3.5
margins rank, at(gpa = 3.5) atmeans
predict yhat_logit
gen double yhat_logit_manual = exp(_b[exp]*exp+_b[gpa]*gpa+_b[2.rank]*2.rank+_b[3.rank]*3.rank+_b[4.rank]*4.rank + _b[_cons])/(1+exp(_b[exp]*exp+_b[gpa]*gpa+_b[2.rank]*2.rank+_b[3.rank]*3.rank+_b[4.rank]*4.rank + _b[_cons]))
assert round(yhat_logit, .001) == round(yhat_logit_manual, .001)
/* Graph example */
* A very simple example*
use http://www.ats.ucla.edu/stat/stata/webbooks/logistic/apilog, clear
regress hiqual avg_ed
predict yhat
twoway scatter yhat hiqual avg_ed, connect(l .) symbol(i O) sort ylabel(0 1)
logit hiqual avg_ed
predict yhat_l
twoway scatter yhat_l hiqual avg_ed, connect(l i) msymbol(i O) sort ylabel(0 1)
probit hiqual avg_ed
predict yhat_p
twoway scatter yhat_p hiqual avg_ed, connect(l i) msymbol(i O) sort ylabel(0 1)
scatter yhat* avg_ed
log close