我正在从Stata切换到R,当我使用预测来计算边际pred并且Stata命令边缘的结果将变量的值固定为x时,我发现结果不一致.这是一个例子:
library(dplyr)
library(prediction)
d <- data.frame(x1 = factor(c(1,1,1,2,2,2), levels = c(1, 2)),
x2 = factor(c(1,2,3,1,2,3), levels = c(1, 2, 3)),
x3 = factor(c(1,2,1,2,1,2), levels = c(1, 2)),
y = c(3.1, 2.8, 2.5, 4.3, 4.0, 3.5))
m2 <- lm(y ~ x1 + x2 + x3, d)
summary(m2)
marg2a <- prediction(m2, at = list(x2 = "1"))
marg2b <- prediction(m2, at = list(x1 = "1"))
marg2a %>%
select(x1, fitted) %>%
group_by(x1) %>%
summarise(error = mean(fitted))
marg2b %>%
select(x2, fitted) …Run Code Online (Sandbox Code Playgroud)