使用 lapply 从列表中预测

Predicting from list using lapply

我正在尝试使用 dplyr 和 lapply 估计一组模型。我估计概率回归,结果存储在列表中。然后我想使用预测函数来预测新数据集的值。我的模型运行了,但我得到的结果是零值。我做错了什么?

# Code from the original question
library(dplyr)

year <- rep(2014:2015, length.out=10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace=T)
female <- sample(c(0,1), replace=TRUE, size=10000)
smoker <- sample(c(0,1), replace=TRUE, size=10000)
dta <- data.frame(year=year, group=group, value=value, female=female, smoker=smoker)

# cut the dataset into list
table_list <- dta %>%
  group_by(year, group) %>%
  group_split()

# fit model per subgroup
model_list <- lapply(table_list, function(x) glm(smoker ~ female, data=x,
                                                 family=binomial(link="probit")))

# create new dataset where female =1
dat_new <- data.frame(dta[, c("smoker", "year", "group")], female=1) 

# cut into list
pred_list <- dat_new %>%
  group_by(year, group) %>%
  group_split()

# do prediction
pred2 <- Map(function(x, y) predict.glm(x, type = "response", newdata = y), 
             model_list, pred_list)

我预测的结果为零。为什么?

您应该 lapply 而不是 model_list

pred1 <- lapply(model_list, function(x) predict.glm(x, type = "response"))

或者如果你想传递数据使用Map

pred2 <- Map(function(x, y) predict.glm(x, type = "response", newdata = y), 
          model_list, pred_list)