* Section 2: Data
version 11.1
set more off
webuse nhanes2f, clear
keep if !missing(diabetes, black, female, age, age2, agegrp)
label variable age2 "age squared"
* Compute the variables we will need
tab1 agegrp, gen(agegrp)
gen femage = female*age
label variable femage "female * age interaction"
label define black 0 "nonBlack" 1 "black"
label define female 0 "male" 1 "female"
label values female female
label values black black
sum diabetes black female age age2 femage, separator(6)
* Section 3: Adj predictions for a basic model
logit diabetes black female age , nolog
adjust age = 20 black female, pr
adjust age = 70 black female, pr
margins, at(age=(20 70)) atmeans vsquish
* Section 5.1: Squared terms
logit diabetes black female age age2, nolog
adjust age = 70 black female age2, pr
logit diabetes i.black i.female age c.age#c.age, nolog
margins, at(age = 70) atmeans
* Section 5.2: Interaction terms
logit diabetes black female age femage, nolog
adjust female = 0 black age femage, pr
logit diabetes i.black i.female age i.female#c.age, nolog
margins female , atmeans grand
* Section 5.3: Multiple dummies
logit diabetes black female agegrp2 agegrp3 agegrp4 agegrp5 agegrp6, nolog
adjust agegrp6 = 1 black female agegrp2 agegrp3 agegrp4 agegrp5, pr
logit diabetes i.black i.female i.agegrp, nolog
margins agegrp, atmeans
* Section 6: Marginal Effects
* Back to basic model
logit diabetes i.black i.female age, nolog
* Section 6.1: MEMs - Marginal Effects at the Means
* Adjusted Predictions at the Means
margins black female, atmeans
* Marginal Effects at the Means
margins, dydx(black female) atmeans
* Replicate results for black without using margins
scalar female_mean = .5250121
scalar age_mean = 47.56584
scalar wlogodds = _b[1.black]*0 + _b[1.female]*female_mean + _b[age]*age_mean + _b[_cons]
scalar wodds = exp(wlogodds)
scalar wapm = wodds/(1 + wodds)
di "White APM = " wapm
scalar blogodds = _b[1.black]*1 + _b[1.female]*female_mean + _b[age]*age_mean + _b[_cons]
scalar bodds = exp(blogodds)
scalar bapm = bodds/(1 + bodds)
di "Black APM = " bapm
di "MEM for black = " bapm - wapm
* Section 6.2: AMEs - Average Marginal Effects
* Average Adjusted Predictions (AAPs)
margins black female
* Average Marginal Effects (AMEs)
margins, dydx(black female)
* Replicate AME for black without using margins
clonevar xblack = black
quietly logit diabetes i.xblack i.female age, nolog
replace xblack = 0
predict adjpredwhite
replace xblack = 1
predict adjpredblack
gen meblack = adjpredblack - adjpredwhite
sum adjpredwhite adjpredblack meblack
* Section 6.3: MERs - Marginal Effects at Representative Values
quietly logit diabetes i.black i.female age, nolog
* APRs - Adjusted Predictions at Representative Values (Race Only)
margins black, at(age=(20 30 40 50 60 70)) vsquish
* MERs - Marginal Effects at Representative values
margins, dydx(black female) at(age=(20 30 40 50 60 70)) vsquish
* Section 7: marginsplot command
version 12.0
* Plot of marginal effects
quietly logit diabetes i.black i.female age, nolog
quietly margins, dydx(black female) at(age=(20 30 40 50 60 70)) vsquish
marginsplot, noci
* Plot of adjusted predictions
quietly logit diabetes i.black i.female age i.female#c.age, nolog
margins female#black, at(age=(20 30 40 50 60 70))
marginsplot, noci
*Section 8: Marginal effects for interaction terms
quietly logit diabetes i.black i.female age i.female#c.age, nolog
margins, dydx(*)