user6574498
user6574498

Reputation: 49

How to calculate "terms" from predict-function manually when regression has an interaction term

does anyone know how predict-function calculates terms when there are an interaction term in a regression model? I know how to solve terms when regression has no interaction terms in it but when I add one I cant solve those manually anymore. Here is some example data and I would like to see how to calculate those values manually. Thanks! -Aleksi

set.seed(2)  
a <- c(4,3,2,5,3) # first I make some data  
b <- c(2,1,4,3,5)  
e <- rnorm(5)  
y= 0.6*a+e  
data <- data.frame(a,b,y)  
model1 <- lm(y~a*b,data=data) # regression  
predict(model1,type='terms',data) # terms  
#This gives the result:

       a          b        a:b
1  0.04870807 -0.3649011  0.2049069
2 -0.03247205 -0.7298021  0.7740928
3 -0.11365216  0.3649011  0.2049069
4  0.12988818  0.0000000 -0.5919534
5 -0.03247205  0.7298021 -0.5919534
attr(,"constant")
[1] 1.973031

Upvotes: 0

Views: 344

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

Your model is technically y ~ b0 + b1*a + b2*a*b + e. Calculating a is done by multiplying independent variable by its coefficient and centering the result. So for example, terms for a would be

cf <- coef(model1)

scale(a * cf[2], scale = FALSE)

            [,1]
[1,]  0.04870807
[2,] -0.03247205
[3,] -0.11365216
[4,]  0.12988818
[5,] -0.03247205

which matches your output above.

And since interaction term is nothing else than multiplying independent variables, this translates to

scale(a * b * cf[4], scale = FALSE)
           [,1]
[1,]  0.2049069
[2,]  0.7740928
[3,]  0.2049069
[4,] -0.5919534
[5,] -0.5919534

Upvotes: 2

Related Questions