Reputation: 564
I am trying to estimate a group of models using dplyr and lapply. I estimate a probit regression, where results are stored in a list. Then I would like to use predict function to predict values on a new dataset. My model runs, but I get zero values as results. What am I doing wrong?
# 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)
I get zero results predicted. Why?
Upvotes: 1
Views: 663
Reputation: 388862
You should lapply
over model_list
instead.
pred1 <- lapply(model_list, function(x) predict.glm(x, type = "response"))
Or if you want to pass data use Map
.
pred2 <- Map(function(x, y) predict.glm(x, type = "response", newdata = y),
model_list, pred_list)
Upvotes: 1