R - Interaction Analysis

> Procedural Languages > R

1 - About

interaction with R .

3 - Prerequisites

4 - Steps

4.1 - Linear Model

4.1.1 - Star

An interaction term between a numeric x and z is just the product of x and z.

lm processes the “*” operator between variables and automatically:

myFit=lm(response~predictor1*predictor2,data=data.frame)
myFit

where the star in R means:

  • interaction
  • that we're going to have main effects for each and the interaction. The pure interaction is indicated by a colon (predictor1:predictor2)
Call:
lm(formula = response ~ predictor1 * predictor2, data = data.frame)

Coefficients:
(Intercept)   predictor1   predictor3   predictor1:predictor2
 36.0885359   -1.3921168   -0.0007209               0.0041560  

If you don't want the main effects in the regression, use the I() function. The content will then not parsed as a part of the formula, but rather simply evaluated.

myFit=lm(response~I(predictor1*predictor2),data=data.frame)
myFit

The regression ln will contains then only the interaction term and not the main effects.

Advertising

4.1.2 - Dot

fit1=lm(outcome~.+Predictor1:Predictor2+Predictor3:Predictor4,data.frame)

where:

  • the dot means all variables of the data.frame
  • + Predictor1:Predictor2 as the first interaction
  • + Predictor3:Predictor4 as the second interaction

4.2 - Summary

Summary Statistics

summary(myFit)
Call:
lm(formula = outcome ~ predictor1 * predictor2 , data = Boston)

Residuals:
    Min      1Q  Median      3Q     Max 
-15.806  -4.045  -1.333   2.085  27.552 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) 36.0885359  1.4698355  24.553  < 2e-16 ***
predictor1  -1.3921168  0.1674555  -8.313 8.78e-16 ***
predictor2  -0.0007209  0.0198792  -0.036   0.9711    
interaction  0.0041560  0.0018518   2.244   0.0252 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6.149 on 502 degrees of freedom
Multiple R-squared:  0.5557,	Adjusted R-squared:  0.5531 
F-statistic: 209.3 on 3 and 502 DF,  p-value: < 2.2e-16

where:

4.3 - Confidence Interval

Confidence Interval

confint(myFit)
                    2.5 %       97.5 %
(Intercept) 33.2007489579 38.976322911
lstat       -1.7211168654 -1.063116816
age         -0.0397774826  0.038335764
lstat:age    0.0005177284  0.007794175
Advertising