# A 2SLS when the instrumented variable has two interactions in the model

I am using ivreg and ivmodel in R to apply a 2SLS.

I would like to instrument one variable, namely $$x_1$$, present in two interaction terms. In this example $$x_1$$ is a factor variable. The regression is specified in this manner because the ratio between $$a$$ and $$b$$ is of importance.

$$y = ax_1 x_2 + bx_1x_3 + cx_4 + e$$

For this instrumented variable I have two instruments $$z_1$$ and $$z_2$$. For both the following causal diagram is applicable (Z only has an indirect effect on Y through X).

What is for this problem the correct way to instrument $$x_1$$?

# In the data

Translated to some (fake) sample data the problem looks like:

$$happiness = a(factor:income) + b(factor:sales) + c(educ) + e$$ $$=$$ $$(y = ax_1 x_2 + bx_1x_3 + cx_4 + e)$$

Where the instrument $$z_1$$ is urban and $$z_2$$ is size. Here I however become to get confused about how to write the regression.

# For the first stage:

What is my dependent variable here?

# For the second stage, should I do:

$$happiness = a(urban:income) + b(urban:sales) + c(educ) + e$$ $$happiness = a(size:income) + b(size:sales) + c(educ) + e$$

Or should I just do:

$$happiness = urban*(a:income+b:sales) + c(educ) + e$$ $$happiness = size*(a:income+b:sales) + c(educ) + e$$

Nevertheless, how should I specify this in R ?

library(data.table)
library(ivmodel)
library(AER)
panelID = c(1:50)
year= c(2001:2010)
country = c("NLD", "BEL", "GER")
urban = c("A", "B", "C")
indust = c("D", "E", "F")
sizes = c(1,2,3,4,5)
n <- 2
library(data.table)
set.seed(123)
DT <- data.table(panelID = rep(sample(panelID), each = n),
country = rep(sample(country, length(panelID), replace = T), each = n),
year = c(replicate(length(panelID), sample(year, n))),
some_NA = sample(0:5, 6),
Factor = sample(0:5, 6),
industry = rep(sample(indust, length(panelID), replace = T), each = n),
urbanisation = rep(sample(urban, length(panelID), replace = T), each = n),
size = rep(sample(sizes, length(panelID), replace = T), each = n),
income = round(runif(100)/10,2),
Y_Outcome= round(rnorm(10,100,10),2),
sales= round(rnorm(10,10,10),2),
happiness = sample(10,10),
Sex = round(rnorm(10,0.75,0.3),2),
Age = sample(100,100),
educ = round(rnorm(10,0.75,0.3),2))
DT [, uniqueID := .I]                                                         # Creates a unique ID
DT <- as.data.frame(DT)


To make it slightly easier for someone to help who is not familiar with the packages, I have added how the structure of the two packages I use looks.

The structure of the second stage of ivreg is as follows:

second_stage <- ivreg(Happiness ~ factor:income + factor:sales + educ | urban:income + urban:sales + educ, data=DT)


The structure for ivmodel is:

second_stage<- ivmodel(Y=DT$$Happiness,D=DT$$factor,Z=DT[,c("urban","size")],X=DT$educ, na.action = na.omit)  Any help with figuring out how to do this properly would be greatly appreciated! • Can you add a causal diagram to your question, please? Jun 15 '20 at 14:25 • @AdrianKeister I am not very familiar with using causal diagrams, but I noticed that my example was very unclear. Did I by improving the example perhaps take away your confusion? If not, could you tell me what exactly is unclear? I will then try to make that clear using a causal diagram. – Tom Jun 16 '20 at 6:30 • @AdrianKeister I have added a diagram:) – Tom Jun 16 '20 at 7:00 • Causal diagrams can make a LOT of causal concepts much clearer - I would always start with them. Your diagram still leaves me with a couple questions: 1. Is$Y$the effect variable? 2. Is$X$the causal variable? 3. What is the big red X in your diagram? One comment: your diagram is not the usual setup for 2SLS. Instrumental variables can be useful for certain diagrams, but other techniques, such as the backdoor adjustment or the frontdoor adjustment formulas, are oftentimes more useful. Jun 16 '20 at 13:58 • Y is the dependent/effect variable. The main point is that the z, provide an exogenous shock to x. Furthermore, the z do not affect y other than through x (hence the red cross). The correlation between the z and x should therefore lead to an exogenous change in y. Which I assume answer the question what the causal effect of x on y is. Does that clear anything up? – Tom Jun 16 '20 at 14:06 ## 1 Answer You have an endogenous variable $$x_1$$ which is a factor, let's say that it takes 6 values ($$x_1 = a",...f")$$. In your model it enters with an interaction with variables $$x_2$$ and $$x_3$$. Therefore your model is equivalent to $$y = a_11_{(x_1=a")}x_2+...a_51_{(x_1=f")}x_2+b_11_{(x_1=a")}x_3+...b_51_{(x_1=f")}x_3+cx_4+e$$ where $$1_{(x_1=a")} = 1$$ if $$x_1=a"$$ and zero otherwise. So it has $$2\times6$$ endogenous variables (the 12 interactions) and you need at least 12 instruments. In your example you have an endogenous variable $$factor$$ and two good instrumental variables $$urban$$ and $$size$$. If they are good instuments for $$factor$$ then their interaction with $$income$$ and $$sales$$ is good for the same interaction with the instrumented variable. But still you need at least 12 instruments. The first step of the 2SLS will be a OLS regression where the dependent variables are the 12 endogenous interactions $$1_{(x_1=a")}x_2,...1_{(x_1=f")}x_3$$ and the independent variables are all the instruments and the rest of the exogenous variables. The second step is another OLS regression where the dependent variable is the original one and the independent variables are the exogenous variables + the fitted values of the first step. Now, following your code example, suppose your instrument is also a factor, let's say it has 6 levels, you'll have enough variables to estimate it. In your code the variable Factor is numeric, so let's make it a factor first. DT$$Factor = as.factor(DT$$Factor)  Based on your code, without instruments: lm(happiness ~ Factor:income + Factor:sales + educ,data=DT) Coefficients: (Intercept) educ Factor0:income Factor1:income Factor2:income Factor3:income Factor4:income Factor5:income Factor0:sales 4.46555 3.25149 18.18265 -19.68570 -16.71225 -38.04578 -19.68150 2.98939 -0.10307 Factor1:sales Factor2:sales Factor3:sales Factor4:sales Factor5:sales -0.16310 -0.03131 -0.12445 -0.14503 -0.08158  Now, in a 2SLS model using ivreg with the two instruments you provided: ivreg(happiness ~ Factor:income + Factor:sales + educ | urbanisation:income + urbanisation:sales + size:income + size:sales + educ, data=DT) Coefficients: (Intercept) educ Factor0:income Factor1:income Factor2:income Factor3:income Factor4:income Factor5:income Factor0:sales -6.690 8.736 -296.010 292.081 36.372 -183.167 -231.191 712.253 -1.522 Factor1:sales Factor2:sales Factor3:sales Factor4:sales Factor5:sales 2.428 NA NA NA NA Warning message: In ivreg.fit(X, Y, Z, weights, offset, ...) : more regressors than instruments  You see that it can only estimate 8 endogenous variables, this is because we provided 8 instruments: urbanisation has 3 levels (hence 6 interactions) + 2 interactions for size. Now consider using the variable urbanisation2 with 6 levels: DT$urbanisation2 = rep(sample(c("A", "B", "C","D","E","F"), length(panelID), replace = T), each = n)

ivreg(happiness ~ Factor:income + Factor:sales + educ | urbanisation2:income + urbanisation2:sales + size:income + size:sales + educ, data=DT)

Coefficients:
(Intercept)            educ  Factor0:income  Factor1:income  Factor2:income  Factor3:income  Factor4:income  Factor5:income   Factor0:sales
4.513e+00       3.321e+00       6.329e+00      -1.802e+01       1.045e+01      -1.154e+02       6.208e+01      -3.196e+01      -8.905e-03
Factor1:sales   Factor2:sales   Factor3:sales   Factor4:sales   Factor5:sales
-1.706e-01      -1.730e-01       2.692e-02      -3.410e-01      -3.671e-02


Now it works because you have enough instruments.

• Hey @Ale, thank you for your answer! I'm going to try to run this on my actual data. What does it mean for the dependent variable in my first stage (which would normally be the instrument) in this case? I assume that ivreg will just treat the factor interaction as continuous in the first stage?
– Tom
Jun 25 '20 at 12:22
• Wait, in your example DT$Factor was a numeric variable. If the endogenous variable is a factor with 5 levels then is like having 5 variables of interaction for each level, ex:$a_1(x_1="a")*x_2+...+a_5(x_1="e")*x_2$, if all these are endogenous you need at least as many instruments. In the 1st step all endogenous interactions are the dependent variables on the exogenous + instruments. – Ale Jun 25 '20 at 14:05 • I edited the answer to take into account botha theory and practice – Ale Jun 25 '20 at 17:42 • Thank you very much, I'm going to take a look first thing in the morning! – Tom Jun 25 '20 at 18:03 • Hi, I guess yes. In the first step each$1_{(x_1=a")}x_2$is actually$x_2$with zeros where$x_1 \neq a\$. You get the same coefficients of ivreg if you do it manually with the two steps lm()
– Ale
Jun 26 '20 at 12:06