Skip to content

Instantly share code, notes, and snippets.

@renjiege
Created November 25, 2017 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renjiege/2d38884bdaaa8ef4d0672ee21b2a0352 to your computer and use it in GitHub Desktop.
Save renjiege/2d38884bdaaa8ef4d0672ee21b2a0352 to your computer and use it in GitHub Desktop.
Conducting predictive analysis with complete or quasi-complete separation data.
* modelprediction.do
* Predict Y2 *
clear
set more off
/*
cd /Users/renjiege/Documents/taskdata/
import delimited "/Users/renjiege/Documents/taskdata/Modeling_C.csv", encoding(ISO-8859-1)
save Modeling_C.dta, replace
xx
*/
use "/Users/renjiege/Documents/taskdata/Modeling_C.dta", clear
drop y1
/* OLS */
reg y2 x*, ro
predict ols
/* Logit with x2 and x4 */
logit y2 x2 x4
predict logit
/* Probit with x2 and x4 */
probit y2 x2 x4
predict probit
/* Firth Logit (Penalized MLE) */
firthlogit y2 x*
predict double xb
gen double firthlogit1 = invlogit(xb)
drop xb
gen x1_sq = x1*x1
gen x2_sq = x2*x2
gen x3_sq = x3*x3
gen x4_sq = x4*x4
firthlogit y2 x*
predict double xb
gen double firthlogit2 = invlogit(xb)
drop xb
/* Firth Logit (Penalized MLE) with interactions */
gen inter12 = x1*x2
gen inter13 = x1*x3
gen inter14 = x1*x4
gen inter23 = x2*x3
gen inter24 = x2*x4
gen inter34 = x3*x4
gen inter123 = x1*x2*x3
gen inter124 = x1*x2*x4
gen inter234 = x2*x3*x4
gen inter134 = x1*x3*x4
gen inter1234 = x1*x2*x3*x4
firthlogit y2 x*
predict double xb
gen double firthlogit3 = invlogit(xb)
drop xb
drop x* inter*
/* Compute Percent Correct */
gen ols_correct = 1 if (ols>=0.5 & y2==1) | (ols<0.5 & y2==0)
replace ols_correct = 0 if ols_correct==.
gen logit_correct = 1 if (logit>=0.5 & y2==1) | (logit<0.5 & y2==0)
replace logit_correct = 0 if logit_correct==.
gen probit_correct = 1 if (probit>=0.5 & y2==1) | (probit<0.5 & y2==0)
replace probit_correct = 0 if probit_correct==.
gen firthlogit_correct1 = 1 if (firthlogit1>=0.5 & y2==1) | (firthlogit1<0.5 & y2==0)
replace firthlogit_correct1 = 0 if firthlogit_correct1==.
gen firthlogit_correct2 = 1 if (firthlogit2>=0.5 & y2==1) | (firthlogit2<0.5 & y2==0)
replace firthlogit_correct2 = 0 if firthlogit_correct2==.
gen firthlogit_correct3 = 1 if (firthlogit3>=0.5 & y2==1) | (firthlogit3<0.5 & y2==0)
replace firthlogit_correct3 = 0 if firthlogit_correct3==.
sum ols_correct logit_correct probit_correct firthlogit_correct1 firthlogit_correct2 firthlogit_correct3
/* Compute Percent Correct */
roccomp y2 probit logit firthlogit1 firthlogit2 firthlogit3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment