0% found this document useful (0 votes)
6 views5 pages

OLS Regression with Interaction Terms in Stata

This document outlines Exercise 7 for a BSc Soc course at Copenhagen Business School, focusing on multivariate linear regressions with interaction terms using Stata. It includes instructions for recoding variables, running regressions, generating interaction terms, and interpreting results, particularly in relation to urbanization and crime, as well as gender and working hours. The document emphasizes the importance of visualizing marginal effects and interaction significance through plots.

Uploaded by

aagnaagn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

OLS Regression with Interaction Terms in Stata

This document outlines Exercise 7 for a BSc Soc course at Copenhagen Business School, focusing on multivariate linear regressions with interaction terms using Stata. It includes instructions for recoding variables, running regressions, generating interaction terms, and interpreting results, particularly in relation to urbanization and crime, as well as gender and working hours. The document emphasizes the importance of visualizing marginal effects and interaction significance through plots.

Uploaded by

aagnaagn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

**** EXERCISE 7 START

* ==========================================================
* BSc Soc - Quantitative Methods II, Exercise class #7 =
* Copenhagen Buiness School, Spring 2017 =
* Week 10, Session 7 =
* OLS regression in Stata, Chapter 7 & 10 =
* ==========================================================

/* Today's session

I. Multivariate linear regressions with interaction terms

Dataset:
European Social Survvey (ESS)
Agresti/Finlay crime dataset
*/

* ==========================================================

*** General settings ***

clear

cd "your file path"

log using Exercise7, text

use [Link]

* =============================================================
* I. Multivariate linear regressions with interaction terms
* =============================================================

*** Exercise#1 ***


*a. Recode the variable "me" - percent in metropolitan areas into a dummy variable.
// States with high urbanization (above 60%) should be coded into 1; states with
low urbanization should be coded into 0.
// call the new variable 'urban'.

* using recode
recode me (0/60=0) (60.1/100=1), gen(urban)

* alternative: using generate replace


gen urban2 = 0
replace urban2 = 1 if me>=60

* b. Do a regression with violent crimes as the dependent variable, and poverty


* as the explanatory variable, using the urban dummy as a control.

reg vi po urban

* Medium-high adj. R2, both independent variables significant and positive sign

***********************************
*** Including interaction terms ***
***********************************
*** Exercise 2 ***

* a. Generate a new variable for the interaction term poverty and urban

gen po_urb = po*urban

*b.1 Run the regression inlcuding the before generated interaction!

reg vi po urban po_urb

*b.2 As an alternative run the same regression but this time using stata's automated
commands for generating multiplicative interactions
// instead of inserting the before generated interaction variable.

// The sign '#' tells Stata that you want to multiply two variables
// The prefix 'c.' entered before the variable you want to multiply tells stata
that you're working with a continuous variable.
// The prefix 'i.' entered before the variable you want to multiply tells stata
that you're working with a categorical variable.

// For instance poverty is continuous and urbanization is a dummy - you can write
the model the following way:

reg vi po urban [Link]#[Link]

// Here, the '#' sign tell Stata to multiply the two continuous variables (c.),
poverty and urbanization.
// The result is exactly the same as before (now you just don't have to create
the interaction term yourself - Stata has done it for you.

*b.3 We can make it even easier without having to type po and urban before the
interaction term. The double '##' tells Stata to include the variables both
separately
// and as a multiplicative term. Rerun the regression accordingly and check whether
it gives you the same result.

reg vi [Link]##[Link]

// This will give you a model with three variables on the right hand-side: seperate
variables and cross-product

*c. Interpret the result!

// Interpretation interaction term:


* Effect of urbanization (x-variable) on crime (y-variable) if poverty
(z-variable) changes by one unit
* or:
* Effect of poverty (x-variable) on crime (y-variable) if urbanization
(z-variable) changes by one unit

*To better understand the effect plug in the values for urban:
* Urban=0: vi=cons+b1*po
* Urban=1: vi=const+b1*po+b2*1+b3*1*po=(const+b2)+(b1+b3)*po
* Two effects: intercept decreases by 57.3 and slope increased to 56.9 (22.5+34.4

* Note: Noth po and urban are not significant anymore when including the interaction
term!

**********************************************
*** Significance of the interaction effect ***
**********************************************

*** Exercise #3 ***

* Creating a plot of marginal effects

// Note: The point of interaction models is that the effect of one variable (e.g.
poverty) may be moderated by values on another variable (e.g. urbanization)
// If so, the effect of poverty varies according to the level of urbanization in
a particular state
// Poverty may affect crime only in states with high levels of urbanization, but
not in state with low levels of urbanization
// This means that it is not only the effect of poverty on crime that may vary
with levels of urbanization. It also means that the statistical significance of
poverty depends on the level of urbanization
// For instance, while the effect of poverty on crime may be positive and
statistically significant in states with high levels of urbanization, the effect
of poverty may be insignificant in states with low urbanization.
// The problem is that we cannot tell the significance of the interaction effect
simply by looking at p-values of the interaction term.
// Therefore, a good advice is to plot the relationship. This will show you at
what level of urbanization the relationship between poverty and crime becomes
significant

*a. Show the marginal effects of poverty at the two different levels of the
moderating z-variable - urbanization.
// Use the command margins, dydx [x-var] at(z-var=(levels). Check help for more
information.
* First, you run the regression using Stata's automated commands for interaction
terms (you must use this command).
reg vi [Link]##[Link]

* Second, you ask Stata to show the marginal effect of poverty at different levels
of the moderating variabel - urbanization.
// Marginal effects (by conditioning variable, me=urbanization)

margins, dydx(po) at(urban=(0 1))

// The margins command and expression dydx(po) tell stata to calculate the marginal
effect of poverty.
// The option 'at(urban=(0 1))' tells stata that you want to evaluate the effect
of poverty at the two values of urbanization defined by your dummy (0=rural states;
1=urban states).

// Note: You could also do an interaction with the original, continuous


urbanization variabel. In this case, you would run the following

reg vi [Link]##[Link]
margins, dydx(po) at(me=(0(10)100))
// The option 'at(me=(0(10)100))' tells stata that you want to evaluate the effect
of poverty at different levels of urbanization (the 'me' variabel).
// the expression '(0(10)100))' tells stata that you want to evaluate the effect
of poverty for values of urbanization between 0 and 100, and at 10-point intervals.

*b. Depict the interaction (command: marginsplot). Add titles to the graphs and
axes and include the source.

marginsplot, recast(line) recastci(rline) ciopts(lpattern(dash)) yline(0)


marginsplot, recast(line) recastci(rline) yline(0)

/* explanation of options:
recast(plottype) specifies that margins be plotted using a specified plottype -
in our case a line (could alsoo be scatter, connected bar, area etc). plottype
recastci(plottype) specifies that confidence intervals be plotted using a
specified plottype - here also a line (rline)
ciopts(rcap_options) affects the rendition of all confidence-interval plots - here
that it is depicted as a dashed line. You could e.g. also change the color to green
with lcolor(green)
*/

* You can also add titles to the plot and the axes, as well as notes to the plot.

marginsplot, recast(line) recastci(rline) ciopts(lpattern(dash)) yline(0) ///


legend(on label(1 "95% confidence interval") label(2 "Marginal effect of
poverty")) ///
xtitle(Urbanization) ytitle (Marginal effect af poverty) title(Marginal effect
of poverty in rural vs. urban states) ///
note("Note: Data from Agresti/Finlay")

*c Interpret the plot!


* the interaction term is statistically significant whenever upper and lower bounds
of confidence interval are both above (or below) the zero line

*here: not siginificant for urban=0

*** loading ESS dataset ***

save crimeEX7, replace

clear
use [Link]

*** Exercise#4 ***

* Conditional hypothesis:
* Is there a different effect of being married on working hours among men and women?
* Use interaction term to analyse that.

* a. Compute dummy variables based on the variable *Legal marital status* (marit)
tab marit
tab marit, nolabel
gen marriage=0
replace marriage=1 if marit==1
* b. Compute a dummy-coded gender variable (women = 1, men = 0) and create
interaction terms
// by multiplying the gender dummy by the *Legal marital status* dummy.
tab gndr
tab gndr, nolabel
recode gndr (1=0) (2=1), gen(gender_dum)
*Alternatively, use gen and replace... if as we did in 4a)

gen marr_gndr=marriage*gender_dum

* c. Run a multiple regression analysis with gender, the marital status dummies
and the interaction terms on the independents list and
// Total hours normally worked per week in main job, overtime included* as the
dependent variable (wkhtot).
// Use also the in-build stata command for interaction terms. Interpret the
resulting coefficients and statistical tests.
reg wkhtot marriage gender_dum marr_gndr

*alternative:
reg wkhtot [Link]##i.gender_dum

* Result: You should observe that with marital status there are different
associations with working hours among men than among women.
* Thus, the additional COMBINED effect of change in marriage (being married) as
well as gender (females) in comparison to the independent effect
* Interaction coefficient: Effect of women (x) on working hours (y) if married
(z)

/*
Unmarried: wkhtot=const+b2*gender
for men: wktot=35,1; for women=35,1 � 3,3=31,8; Diffference: -3.3

Married: wkhtot=const+b1*1+b2*gender+b3*1*gender=(const+b1)+(b2+b3)*gender
for men: 35,1+7,5=42,6; for women: 42,6 � (3,3-5,0)=34,3; Difference: -8.3

=> If women marry, they gap to the working hours of men increases even more!
*/

*d. Show the marginal effect of gender at the two different levels of the moderating
z-variable marriage and create a marginsplot.
reg wkhtot [Link]##i.gender_dum
margins, dydx(gender_dum) at(marriage=(0 1))
marginsplot, recast(line) recastci(rline) ciopts(lpattern(dash)) yline(0) ///
ylabel (-10(1)5) ///
legend(on label(1 "95% confidence interval") label(2 "Marginal effect of gender"))
///
xtitle(Marriage) ytitle (Gender) title(Marginal effect of gender being married
vs. non-married) ///
note("Note: Data from ESS")

You might also like