* Encoding: UTF-8.
* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
This macro can be used to quickly run Generalized LInear Models when you want
to run many modesl while keeping the same covariates, only changing the key
independent variable and the dependent variable. The syntax will run the model
unadjusted first (with no control variables) then adjusted for confounders
Notice that you should change the confounders (sex, age), according to your research
question and dataset.
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
* Model parameters
* model - model type: normal, binomial, poisson
*scl - Estimation method for estimating the scele paramter (mle, deviance, pearson, or a fixed value.
* dep - This is the dependent variable .
* indep - Independent variable. In this case we assume there is only one. Control variables are already stated in the macro.
* link - The link function which will depend on the model (log, inverse, identity).
* rob - If robust sandwich estimator is to be used (model, robust).
define !runmod (model = !default(normal) !tokens(1)
/scl = !default(mle) !tokens(1)
/dep = !tokes(1)
/indep = !tokens(1)
/link = !default(log) !tokens(1),
/rob = !default(robust) !tokens(1) )
* Unadjusted MOdel
GENLIN !dep WITH !indep
/MODEL !indep Distribution = !model LINK= !link
/CRITERIA METHOD=FISHER(1) SCALE=!scl COVB= !rob MAXITERATIONS=1000 MAXSTEPHALVING=25
/PRINT CPS DESCRIPTIVES MODELINFO FIT SUMMARY SOLUTION (EXPONENTIATED) .
* Adjusted Model - Here we save Cook Distance, leverage and residuals for model deagnostic.
GENLIN !dep BY Sex treat.aap treat.li treat.ms marital
WITH !indep Age DASI BMI Educationyears
/MODEL !indep Sex treat.aap treat.li treat.ms marital Age DASI BMI Educationyears
Distribution = !model LINK= !link
/CRITERIA METHOD=FISHER(1) SCALE=!scl COVB= !rob MAXITERATIONS=1000 MAXSTEPHALVING=25
/PRINT CPS DESCRIPTIVES MODELINFO FIT SUMMARY SOLUTION (EXPONENTIATED)
/SAVE meanpred xbpred DEVIANCERESID cook leverage .
* R2 for model - Association between predicted with observed.
REG /STAT R /DEP !dep /METHOD=ENTER MeanPredicted.
* Distribution check for the dependent variable.
PPLOT /var= !dep /TYPE=p-p /DIST= !model.
PPLOT /var= !dep /TYPE=q-q /DIST= !model.
* Check normality of residuals.
GRAPH /HISTOGRAM=DevianceResidual.
EXAMINE VAR=DevianceResidual /PLOT NPPLOT.
* check influence by using histographs for leverage and cook's distance.
GRAPH /HISTOGRAM=leverage.
GRAPH /HISTOGRAM=cooksdistance.
* Check homogeneous variance, outlier.
GRAPH /SCATTERPLOT(BIVAR)=XBPredicted WITH DevianceResidual.
!enddefine.
* Here we run the macro, specifying its parameters.
* yvar and xvar are the independent and dependent variables.
!runmod dep = yvar indep = xvar model = normal link = log rob = robust.
* We found that ID = 25 has very high cook's distance, so we run again after removing ID = 25.
compute x = id ne 25.
filter by x.
exe.
!runmod dep = yvar indep = xvar model = normal link = log rob = robust.
* turn filter off.
use all.
* the way the macro is useful is that it lets you run the same model again, with otehr outcome or main predictor.
* Now we use a binary outcome, so that hte model has to change to binomial (logistic regression).
!runmod dep = yvar_binary indep = xvar model = binomial link = logit rob = robust.