r - How to calculate "terms" from predict-function manually when regression has an interaction term -
does know how predict-function calculates terms when there interaction term in regression model? know how solve terms when regression has no interaction terms in when add 1 cant solve manually anymore. here example data , see how calculate values manually. thanks! -aleksi
set.seed(2) <- c(4,3,2,5,3) # first make 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 result: 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
your model technically y ~ b0 + b1*a + b2*a*b + e
. calculating a
done multiplying independent variable coefficient , centering result. example, terms a
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 output above.
and since interaction term nothing else multiplying independent variables, 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
Comments
Post a Comment