* USP Prof. Lorena G Barberia * 2019 * Interaction Lab - Answer Key version 15 clear set more off use "C:\Users\Lorena\Dropbox\2019 USP Methods II\Labs\Lab Class 7 - Interactions\Hillary example\nes1996_modif.dta", clear * Part 1: X and Z are dummy variables; * As we continously emphasize in this class, you must always know your data. *Women's Movement sum wmnmvt, detail kdensity wmnmvt tab wmnmvt *Income sum faminc, detail kdensity faminc tab faminc * In the first exercise, it is required to create dummy variable from the varibles already available in the data set. * Hint: To create variables you can use de "generate" command. "Replace" will help you while creating a variable from one already * present in the data set. * In this data set, there are many missing values for variables. * We need to make sure to keep missing values as missing in creating dummy variables. *B - Estimate the additive model * Run the additive model using the dummy variables created above. *C- Estimate the interative model * There is more than one way to estimate interactive models. Here, we will describe some of them: * Creating an interactive variable manually gen wmn_hiincome_dummy = wmnmvtdummy*hiincomedummy * After creating the interactive variable, you may run your regression. * Using "margins" command findit margins * Look for stata documentation of "margins" command before using it. regress hillary i.wmnmvtdummy##i.incomedummy * E - Calculating Predicted Values * In this exercise, there are a few command that you may use to calculate the predicted values. * Using Stata's margins command *Always before runnig margins command, you must run your regression regress hillary i.wmnmvtdummy##i.incomedummy * Non-Feminist and Low Income margins, at( wmnmvtdummy = (0) incomedummy = (0) ) * Feminist and Low Income margins, at( wmnmvtdummy = (1) incomedummy = (0) ) * Non-Feminist and High Income margins, at( wmnmvtdummy = (0) incomedummy = (1) ) * Feminist and High Income margins, at( wmnmvtdummy = (1) incomedummy = (1) ) * A more succint version of the command margins wmnmvtdummy incomedummy wmnmvtdummy#incomedummy, atmeans * It is also possible to use "lincom" command * Non-Feminist and Low Income lincom 1*_cons * Feminist and Low income lincom 1*_cons + 1*wmnmvtdummy * Non-Feminist and High Income lincom 1*_cons + 1* hiincomedummy * Feminist and High Income lincom 1*_cons + 1* wmnmvtdummy +1*hiincomedummy + 1*wmn_hiincome_dummy ******************************************************************************************** * Part 2: X is a dummy and Z is a categorical variable; * A * With Stata commands treating income as categorical gen wmnmvtdummy_faminc=wmnmvtdummy*faminc regress hillary wmnmvtdummy faminc wmnmvtdummy_faminc regress hillary i.wmnmvtdummy##i.faminc * B * Margins plot command allows you to draw a graph to analyze the average predicted effect of income influences * how feminist and non-feminists rate Hillary regress hillary i.wmnmvtdummy##c.faminc margins, at(faminc=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) wmnmvtdummy=(0 1)) marginsplot, xdimension(faminc) * Or, equivalently margins, at(faminc=(1(1)24) wmnmvtdummy= (0 1)) marginsplot * C * using command for adjacent values to assess if differences are statistically different from 0. regress hillary i.wmnmvtdummy##i.faminc margins ar.faminc, dydx(wmnmvtdummy) contrast(nowald) marginsplot, yline(0) * With interflex ssc install interflex, replace all interflex hillary wmnmvtdummy faminc, vce(r) ylab(hillary) dlab(wmnmvtdummy) xlab(faminc) ******************************************************************************************** * Part 3: X is a continuous variable and Z is a categorical variable * A: * Creating interactive term manually: gen wmnmvt_faminc=wmnmvt *faminc regress hillary wmnmvt faminc wmnmvt_faminc * Using margins command regress hillary c.wmnmvt##i.faminc * B * In Stata margins code regress hillary c.wmnmvt##i.faminc margins, dydx(wmnmvt) at(faminc=(1(7)24) wmnmvt=(0(20)100)) * Simulating a one unit change in wmnmvt regress hillary c.wmnmvt##i.faminc quietly predict double wmnmvt0 quietly replace wmnmvt=wmnmvt+1 quietly predict double wmnmvt1 gen double diff = wmnmvt1-wmnmvt0 sum diff ******************************************************************************************** * Part 4: X and Z are continuous variables * A * Additive Model regress hillary wmnmvt faminc * Interaction Model regress hillary c.wmnmvt##c.faminc *B - Calculating Interaction Effects using Margins regress hillary c.wmnmvt##c.faminc margins, dydx(wmnmvt) at(faminc=(1(1)24)) // dydx is the partial derivative regress hillary c.wmnmvt##c.faminc margins, dydx(faminc) at(wmnmvt=(0(10)100)) // dydx is the partial derivative // Using margins and marginsplot regress hillary c.faminc##c.wmnmvt * (Case 1) margins, dydx( faminc ) continuous at( wmnmvt = (0(10)100) ) marginsplot, xdimension(at(wmnmvt)) recast(line) recastci(rline) /// ciopts(lpattern(dash)) ytitle(Marginal Effect of Income) /// xtitle(Women’s Movement) /// title(Marginal Effect of Income on Hillary) /// subtitle((Given variation in Women’s Movement Score)) * Using interflex interflex hillary wmnmvt faminc , type(linear) xlab(faminc) ylabel(me of wmnmvt on hillary) interflex hillary faminc wmnmvt, type(linear) xlab(wmnmvt) ylabel(me of income on hillary)