如何使用 fitted() 创建模型预测的新数据框到 运行 现有 lm/lmer 具有交互作用的模型?
How create new dataframe of model predictions using fitted() to run existing lm/lmer model with interaction?
由于缺乏更好的措辞:我需要从现有数据帧创建一个新的数据帧到 运行 一个 lm/lmer 模型以获得模型预测。
假设我有:
x <- as.numeric(rep(1:6,5))
y <- as.numeric(rep(1:5,6))
int1 <- factor(rep(c("a","b"),15))
int2 <- factor(rep(c("11","12","13"),10))
g <- rep(c("f","m"),15)
df <- data.frame(x,y,int1,int2,g)
head(df)
# x y int1 int2 g
#1 1 1 a 11 f
#2 2 2 b 12 m
#3 3 3 a 13 f
#4 4 4 b 11 m
#5 5 5 a 12 f
#6 6 1 b 13 m
library(lme4)
mod <- lmer(y ~ x + int1*int2 + (1|g), data=df) #model I need
#OR:
#mod <- lm(y ~ x + int1*int2, data=df) #might be enough, don't need
#random effects in new df.
现在我需要为 运行 拟合模型创建一个新的 df 以获得预测。
我只关心交互项(2x3 交互),但显然,新的 df 需要具有所有固定效果,否则会引发错误。随机效应被排除在外。
新的 df 具有标准误差和置信区间边界。
大致应该是这样的:
> foo
int1/int2fit se lwr upr
11 68.86 2.03 64.91 72.86 #main effect
12 43.44 5.78 32.50 55.10 #main effect
13 38.77 4.14 31.12 47.19 #main effect
a 36.81 5.87 26.05 48.72 #main effect
b 34.58 3.59 27.55 41.71 #main effect
11a 28.04 4.40 19.87 37.31 #interaction term
11b 32.69 3.92 25.28 40.48 #interaction term
11c more numbers … … #interaction term
12a … … … … #interaction term
12b … … … … #interaction term
12c … … … … #interaction term
13a … … … … #interaction term
13b … … … … #interaction term
13c … … … … #interaction term
我使用的代码如下。它不起作用,错误在第一行。
newdata <- data.frame(int1 = levels(df$int1), int2 = levels(df$int2),
x = range(df$x)) #wrong. How change it?
fitmod = fitted(mod, newdata = newdata, re_formula = NA, summary =
T)*100 #convert to %
colnames(fitmod) = c('fit', 'se', 'lwr', 'upr')
foo = cbind(newdata, fitmod)
创建一个额外的列组合 int1 和 int2 也不起作用。
如果模型只有一个作为因素的预测变量,它将是:
newdata <- data.frame(int1 = levels(df$int1))
如何正确设置此 df 以获得所有正确的系数?
非常感谢
最容易使用modelr
:
newdat <- modelr::data_grid(df, .model = mod)
newdat$pr <- predict(mod, newdat)
但听起来您可能正在改用 emmeans
?
emmeans::emmeans(mod, ~int1:int2)
由于缺乏更好的措辞:我需要从现有数据帧创建一个新的数据帧到 运行 一个 lm/lmer 模型以获得模型预测。
假设我有:
x <- as.numeric(rep(1:6,5))
y <- as.numeric(rep(1:5,6))
int1 <- factor(rep(c("a","b"),15))
int2 <- factor(rep(c("11","12","13"),10))
g <- rep(c("f","m"),15)
df <- data.frame(x,y,int1,int2,g)
head(df)
# x y int1 int2 g
#1 1 1 a 11 f
#2 2 2 b 12 m
#3 3 3 a 13 f
#4 4 4 b 11 m
#5 5 5 a 12 f
#6 6 1 b 13 m
library(lme4)
mod <- lmer(y ~ x + int1*int2 + (1|g), data=df) #model I need
#OR:
#mod <- lm(y ~ x + int1*int2, data=df) #might be enough, don't need
#random effects in new df.
现在我需要为 运行 拟合模型创建一个新的 df 以获得预测。 我只关心交互项(2x3 交互),但显然,新的 df 需要具有所有固定效果,否则会引发错误。随机效应被排除在外。 新的 df 具有标准误差和置信区间边界。
大致应该是这样的:
> foo
int1/int2fit se lwr upr
11 68.86 2.03 64.91 72.86 #main effect
12 43.44 5.78 32.50 55.10 #main effect
13 38.77 4.14 31.12 47.19 #main effect
a 36.81 5.87 26.05 48.72 #main effect
b 34.58 3.59 27.55 41.71 #main effect
11a 28.04 4.40 19.87 37.31 #interaction term
11b 32.69 3.92 25.28 40.48 #interaction term
11c more numbers … … #interaction term
12a … … … … #interaction term
12b … … … … #interaction term
12c … … … … #interaction term
13a … … … … #interaction term
13b … … … … #interaction term
13c … … … … #interaction term
我使用的代码如下。它不起作用,错误在第一行。
newdata <- data.frame(int1 = levels(df$int1), int2 = levels(df$int2),
x = range(df$x)) #wrong. How change it?
fitmod = fitted(mod, newdata = newdata, re_formula = NA, summary =
T)*100 #convert to %
colnames(fitmod) = c('fit', 'se', 'lwr', 'upr')
foo = cbind(newdata, fitmod)
创建一个额外的列组合 int1 和 int2 也不起作用。 如果模型只有一个作为因素的预测变量,它将是:
newdata <- data.frame(int1 = levels(df$int1))
如何正确设置此 df 以获得所有正确的系数? 非常感谢
最容易使用modelr
:
newdat <- modelr::data_grid(df, .model = mod)
newdat$pr <- predict(mod, newdat)
但听起来您可能正在改用 emmeans
?
emmeans::emmeans(mod, ~int1:int2)