This question relates to this question here.
I am running a similar model as in that question, but at the last line I would like to have 7 predicted columns (i.e. change the dataset in a way that in the first case the new dataset group=0, in the second group=1, etc.
# 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*group, data=x,
family=binomial(link="probit")))
# create new dataset where group =1
dat_new0 <- data.frame(dta[, c("smoker", "year", female)], group=0)
dat_new1 <- data.frame(dta[, c("smoker", "year", female)], group=1)
dat_new2 <- data.frame(dta[, c("smoker", "year", female)], group=2)
etc.
pred0 <- predict.glm(dat_new0, type = "response")
pred1 <- predict.glm(dat_new1, type = "response")
pred2 <- predict.glm(dat_new2, type = "response")
etc.
Instead of doing this by hand, I would like to use tidymodels somehow.