How to remove interaction from regression model in stargazer in R?


To remove interaction from regression model in stargazer in R, we can follow the below steps −

  • First of all, create a data frame.
  • Then, create a regression model using stargazer.
  • After that, create the regression model with stargazer without interaction terms.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

x1<-sample(1:50,25)
x2<-sample(1:50,25)
x3<-sample(1:50,25)
x4<-sample(1:50,25)
y<-sample(1:100,25)
df<-data.frame(x1,x2,x3,x4,y)
df

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    x1 x2  x3  x4  y
1  40  17  49  12  29
2   3   3  31  5   44
3  16  18  34  32  91
4  15  49  30  15  98
5  14  13  50  23  34
6   7  32  16  13  79
7  44  39  26  26  68
8  45  10  19  31  96
9  38  34  45  20  40
10 36  31  2   17  1
11  8  11  20  48  60
12  6  23  4   43  75
13 35  5   15  9   49
14  4  20  35  45  46
15 32  26  18  49  66
16 42  38  25  11  36
17 46  24  14  21  94
18 30  19  29  50  64
19 13  1    5  18  97
20 21  21  17  27  25
21  1  40   41 10  89
22 41  25   8  29 63
23 22  29   38 47  24
24  9  14   13 28  85
25 29  2    37 34  93

Create the regression model using stargazer

Using stargazer to create the regression model in text format −

x1<-sample(1:50,25)
x2<-sample(1:50,25)
x3<-sample(1:50,25)
x4<-sample(1:50,25)
y<-sample(1:100,25)
df<-data.frame(x1,x2,x3,x4,y)
library(stargazer)
Model<-lm(y~x1+x2+x3+x4+x1*x2+x1*x3+x1*x4+x2*x3+x2*x4+x3*x4)
stargazer(Model,type="text")

Output

================================================
      Dependent variable:
   ---------------------------
            y
-----------------------------------------------
x1       -1.177
        (1.440)

x2       0.485
        (1.449)

x3      -1.502
        (1.463)

x4       1.087
        (1.439)

x1:x2   -0.023
        (0.032)

x1:x3    0.012
        (0.031)

x1:x4   0.055*
       (0.030)

x2:x3   0.054
       (0.037)

x2:x4  -0.081*
      (0.044)

x3:x4   -0.016
        (0.037)

Constant 84.551*
        (46.877)

-----------------------------------------------
Observations       25
R2                0.483
Adjusted R2       0.114
Residual Std. Error 26.194 (df = 14)
F Statistic   1.310 (df = 10; 14)
===============================================
Note:       *p<0.1; **p<0.05; ***p<0.01

Create the regression model using stargazer without interaction terms

Using stargazer to create the regression model in text format by ignoring interaction terms with omit function −

 Live Demo

x1<-sample(1:50,25)
x2<-sample(1:50,25)
x3<-sample(1:50,25)
x4<-sample(1:50,25)
y<-sample(1:100,25)
df<-data.frame(x1,x2,x3,x4,y)
library(stargazer)
Model<-lm(y~x1+x2+x3+x4+x1*x2+x1*x3+x1*x4+x2*x3+x2*x4+x3*x4)
stargazer(Model,type="text",omit=":")

Output

===============================================
         Dependent variable:
      ---------------------------
               y
-----------------------------------------------
x1          -1.177
            (1.440)

x2          0.485
           (1.449)

x3         -1.502
           (1.463)

x4         1.087
          (1.439)

Constant  84.551*
         (46.877)

-----------------------------------------------
Observations    25
R2             0.483
Adjusted R2    0.114
Residual Std. Error 26.194 (df = 14)
F Statistic    1.310 (df = 10; 14)
===============================================
Note:       *p<0.1; **p<0.05; ***p<0.01

Updated on: 14-Aug-2021

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements