从回归输出中手动计算交互的拟合值

时间:2020-08-21 04:35:42

标签: r interaction interpretation

我正在使用一种与以下类似的交互模型:

set.seed(1993)

moderating <- sample(c("Yes", "No"),100, replace = T)
x <- sample(c("Yes", "No"), 100, replace = T)
y <- sample(1:100, 100, replace = T)

df <- data.frame(y, x, moderating)

Results <- lm(y ~ x*moderating)
summary(Results)
Call:
lm(formula = y ~ x * moderating)

Residuals:
    Min      1Q  Median      3Q     Max
-57.857 -29.067   3.043  22.960  59.043

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)         52.4000     6.1639   8.501 2.44e-13 ***
xYes                 8.4571     9.1227   0.927    0.356    
moderatingYes      -11.4435     8.9045  -1.285    0.202    
xYes:moderatingYes  -0.1233    12.4563  -0.010    0.992    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 30.82 on 96 degrees of freedom
Multiple R-squared:  0.04685, Adjusted R-squared:  0.01707
F-statistic: 1.573 on 3 and 96 DF,  p-value: 0.2009

我正在学习如何根据回归表计算互动的拟合值。在示例中,基本类别(或省略的类别)是x= Nomoderating = No

到目前为止,我知道以下拟合值:

#Calulate Fitted Value From a Regression Interaction by hand
#Omitted Variable = X_no.M_no

X_no.M_no <- 52.4000
X_yes.M_no <- 52.4000 + 8.4571
X_no.M_yes <- 52.4000 + -11.4435
X_yes.M_yes #<- ?

我不明白最终类别X_yes.M_yes的计算方式。我最初的想法是X_yes.M_yes <- 52.4000 + -0.1233(截距加上交互项),但这是不正确的。我知道它是错误的,因为使用预测函数,拟合值X_yes.M_yes = 49.29032而不是52.4000 + -0.1233 = 52.2767

如何手动计算X_yes.M_yes类别的预测值?

这是从R中的predict函数生成的预测值

#Validated Here Using the Predict Function:
newdat <- NULL
for(m in na.omit(unique(df$moderating))){
  for(i in na.omit(unique(df$x))){
    moderating <- m
    x <- i
   
    newdat<- rbind(newdat, data.frame(x, moderating))
   
  }
}

Prediction.1 <- cbind(newdat, predict(Results, newdat, se.fit = TRUE))
Prediction.1

1 个答案:

答案 0 :(得分:1)

您的回归在数学上看起来像这样:

hat_y = a + b x + c m + d m x

类似地,moderating定义x,当“是”时x = 1,当“否”和m时x为0。

那么X_yes.M_yes意味着x = 1且m = 1,因此您的预测为a + b + c + d.

或使用您的符号X_yes.M_yes = 52.4000 + 8.4571 - 11.4435 - 0.1233