UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Code Fragment: Graphing predicted probabilities with an interaction of a binary variable and a continuous variable

This example uses the hsb2 data file to illustrate how to graph predicted probabilities based on a logistic regression model with an interaction of a binary variable with a continuous variable. .
use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear
gen hiwrite = (write>60)
xi: logit hiwrite i.female*math read

mat c=e(b) /*storing the regresison coefficients*/
sum read

preserve
clear
set obs 3
gen read = r(mean) in 1
replace read = r(mean) + r(sd) in 2
replace read = r(mean) - r(sd) in 3
gen group = _n
expand 2 /*for two values of gender*/
sort read
by read: gen female = _n -1
sort read female
expand 20                    /*for 20 values of math going from 32 all the way to 70*/
sort read female
by read female: gen math = 2*_n + 30
gen fmath = female*math     /*interaction term*/
gen one = 1

mkmat female math fmath read one, mat(X)
mat pred = X*c' /*linear prediction*/
svmat pred

gen p = exp(pred)/(1+exp(pred)) /*probability*/
twoway (line p math if female==1 & group==1) (line p math if female==0 &  group==1)

restore

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.