Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于lappy的列表预测_R_Dplyr_Lapply_Glm_Predict - Fatal编程技术网

R 基于lappy的列表预测

R 基于lappy的列表预测,r,dplyr,lapply,glm,predict,R,Dplyr,Lapply,Glm,Predict,我试图使用dplyr和lapply来估计一组模型。我估计一个概率回归,结果存储在一个列表中。然后我想使用predict函数来预测新数据集上的值。我的模型运行,但结果为零值。我做错了什么 # 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)

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

# 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)
#原始问题的代码
图书馆(dplyr)

年份您应该将
lappy
置于
model\u列表
之上

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