r语言 - Ranef_pred无法计算预测值



我正在尝试将所有数据绘制到图形上。我正在研究失业如何影响野生动物贸易量,而国家是一个随机效应。我已经运行这个确切的代码10次其他类似的数据集,它的工作完美,但在其中2它不会运行。这是我的数据集中唯一显著的效果,所以我真的想要一个图表来可视化模型。

代码在Ranef_pred部分停止运行,在那里我得到这个错误:

Error: Unable to compute predicted values with this model. You can try to
supply a different dataset to the `newdata` argument. This error
was also raised:

new levels detected in newdata: Greenland, Saint Kitts and Nevis

Bug Tracker:
https://github.com/vincentarelbundock/marginaleffects/issues"

我不知道是什么错了,因为它和其他运行良好的代码完全一样。

我的代码是:
mammals <- read_csv("export csv data by genera/Mammalexportnopoverty.csv")
mammals <- mutate(mammals,
Logvol = log10(Vol + 0.00001))
unemploymod <- lmer(Logvol ~ Unemployment + (Unemployment | Country), mammals)
summary(unemploymod)
anova(unemploymod)
fixef(unemploymod)

Ranef_pred <- predictions(model = unemploymod, 
newdata = mammals,  
conf_level = .95,
re.form = ~(Unemployment | Country)
) %>% 
mutate(Log_preds = 10^estimate,
Log_low = 10^conf.low,
Log_high = 10^conf.high)

Average_dat <- mammals %>% summarise(Forest = seq(from = min(Forest), to = max(Forest), length.out = 100))
Average_pred <- predictions(unemploymod, 
newdata = Average_dat, 
conf_level = .95, 
re.form = NA 
) %>% 
mutate(Log_preds = 10^estimate,
Log_low = 10^conf.low,
Log_high = 10^conf.high)
ggplot(mammals, aes(Unemployment, Vol)) +
facet_wrap(~Country) +
geom_point() +
geom_ribbon(data = Ranef_pred, aes(Unemployment, ymin = Log_low, ymax = Log_high), fill = "cyan4", colour = "cyan4", alpha = .25) +
geom_line(data = Ranef_pred, aes(Unemployment, Log_preds)) +
theme_minimal()

ggplot(mammals, aes(Unemployment, Vol)) +
geom_point() +
geom_line(data = Ranef_pred, aes(Unemployment, Log_preds, group = Country), alpha = .40) +
geom_ribbon(data = Average_pred, aes(Unemployment, y=Log_preds,  ymin = Log_low, ymax = Log_high), fill = "cyan4", colour = "cyan4", alpha = .5) +
geom_line(data = Average_pred, aes(Unemployment, Log_preds), colour = "darkblue", size = 1) +
theme_minimal()+
labs(x = "Unemployment (% population)", y = "Volume traded")

我试着查找这个,但找不到确切的问题。我试过重新加载数据,删除日志(但我需要日志),并在一个新的脚本中运行它,但它没有任何区别。我看过bug追踪器链接,但我对混合模型很陌生,不明白为什么它不运行

错误消息非常清楚:预测数据中的某些级别不存在于拟合数据中。也许格陵兰岛的观测结果因为缺失而被按列表删除了?

可以将allow.new.levels参数传递给predictions。然后marginaleffects将其转发给lme4包的predict()方法。这在?predictions中有记录。

library(lme4)
library(marginaleffects)
mod <- lmer(mpg ~ hp + (1 | cyl), data = mtcars)
nd <- transform(head(mtcars, 2), cyl = 9)
predictions(mod, newdata = nd)
# Error: Unable to compute predicted values with this model. You can try to
#   supply a different dataset to the `newdata` argument. This error was
#   also raised:
#   
#   new levels detected in newdata: 9
#   
#   Bug Tracker:
#   https://github.com/vincentarelbundock/marginaleffects/issues
predictions(mod, newdata = nd, allow.new.levels = TRUE)
# 
#  Estimate Std. Error    z Pr(>|z|) 2.5 % 97.5 % cyl disp  hp drat   wt qsec vs
#      21.4       2.43 8.79   <0.001  16.6   26.1   9  160 110  3.9 2.62 16.5  0
#      21.4       2.43 8.79   <0.001  16.6   26.1   9  160 110  3.9 2.88 17.0  0
#  am gear carb
#   1    4    4
#   1    4    4
# 
# Columns: rowid, estimate, std.error, statistic, p.value, conf.low, conf.high, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb

最新更新