I have a dataframe on which i run multiple GLMs, using the whole data set.
df <- data.frame(Var1 = sample(as.factor(0:1), replace = TRUE, 1000),
Var2 = runif(100),
Var3 = runif(100),
Var4 = runif(100),
Var5 = sample(as.factor(0:1), replace = TRUE, 1000),
Var6 = sample(as.factor(0:1), replace = TRUE, 1000))
df %>%
pivot_longer(cols = c("Var3","Var4")) %>%
group_by(name) %>% nest() %>%
mutate(model = map(data,~glm(Var1 ~ value, data = .x,family=binomial("logit")))) %>%
mutate(tidy= map(model, tidy)) %>%
unnest(tidy)
now i would like to use Var5 and Var6 to filter through my dataset.
I would like a GLM for each of the 4 possible datasets Var52 * Var62
With cross() i can get all combinations of the values of Var 5 and Var 6.
list <- df %>%
expand(Var5,Var6) %>%
cross()
now i would like to filter through my dataframe using the list.
So i would like to run a GLM for each of the 4 possible dataframes.
eg in manual mode.
df %>%
filter(Var5 == 1 & Var6 == 1) %>%
pivot_longer(cols = c("Var3","Var4")) %>%
group_by(name) %>% nest() %>%
mutate(model = map(data,~glm(Var1 ~ value, data = .x, family=binomial("logit")))) %>%
mutate(tidy= map(model, tidy)) %>%
unnest(tidy)
df %>%
filter(Var5 == 1 & Var6 == 0) %>%
pivot_longer(cols = c("Var3","Var4")) %>%
group_by(name) %>% nest() %>%
mutate(model = map(data,~glm(Var1 ~ value, data = .x, family=binomial("logit")))) %>%
mutate(tidy= map(model, tidy)) %>%
unnest(tidy)
ect...
i appreciate any advice you can give me on achieving this.
question from:
https://stackoverflow.com/questions/65893162/filtering-through-a-list-using-purrr-map