首页 > 解决方案 > 缺少 newdata 参数时 predict() 如何工作?

问题描述

我最近在学习跨栏模型。我找到了一个包含这些代码的博客。

library(AER)
data("NMES1988")
nmes <- NMES1988[, c(1, 6:8, 13, 15, 18)]
plot(table(nmes$visits))
mod1 <- glm(visits ~. , data = nmes, family = "poisson")

mu <- predict(mod1, type = "response")

exp <- sum(dpois(x=0, lambda = mu))

predict() 函数中缺少 newdata 参数。这个函数会根据旧数据 nmes 进行预测吗?

标签: rmodel

解决方案


是的,它使用原始数据,即产生fitted.values(yhat)。查看:

mu <- predict(mod1, type = "response")
mu2 <- predict(mod1, type = "response", newdata=nmes)
mu3 <- mod1$fitted.values

identical(mu, mu2)
# [1] TRUE
identical(mu, mu3)
# [1] TRUE

推荐阅读