R 通过回归输出手动计算交互的拟合值

R 通过回归输出手动计算交互的拟合值,r,interaction,interpretation,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, mo

我正在使用一个类似于下面的交互模型:

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)
我正在学习如何从回归表中计算交互的拟合值。在本例中,基本类别(或省略的类别)是
x=No
moderation=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 #<- ?


在数学中,您的回归如下所示:

hat_y = a + b x + c m + d m x
其中,当“是”时x=1,当“否”时x=0,m由
调节类似地定义

然后
X\u yes.M\u yes
意味着X=1和M=1,因此您的预测是
a+b+c+d.


或者用你的符号
X_yes.M_yes=52.4000+8.4571-11.4435-0.1233
xYes:moderationyes-0.1233
d=(X_yes.M_yes-X_no.M_yes)-(X_yes.M_no-X_no.M_no)
。该回归通常用于“差异中的差异”估计,在某些假设下,该估计可能是对M为“是”的影响的因果估计。
#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
hat_y = a + b x + c m + d m x