问题
Suppose we have a toy data set:
library(tidyverse)
library(purrr)
tbl <- tibble(a = rep(c(0, 1), each = 5),
b = rep(c(0, 1), times = 5),
c = runif(10),
d = rexp(10)) %>%
mutate_at(vars(1,2), as.factor)
where a
is a dependent variable and b:d
are independent variables. The idea is to run glm
model for each independent variable:
glm(a ~ b, data = tbl, family = "binomial")
glm(a ~ c, data = tbl, family = "binomial")
glm(a ~ d, data = tbl, family = "binomial")
My initial attempt goes as follows:
tbl %>%
pivot_longer(2:4, names_to = "key", values_to = "val") %>%
group_split(key) %>%
map(~ glm(a ~ val, data = .x, family = "binomial"))
This resulted in an error because data types of b
and c
(or d
) are not the same.
Error: No common type for `b` <factor<dec08>> and `c` <double>.
I wonder how to address this issue.
回答1:
Without reshaping, we can use map
to apply glm
to various independent variables and use reformulate
to create the formula like :
purrr::map(names(tbl)[-1],~glm(reformulate(.x, 'a'),data = tbl,family = 'binomial'))
#[[1]]
#Call: glm(formula = reformulate(.x, "a"), family = "binomial", data = tbl)
#Coefficients:
#(Intercept) b1
# -0.4055 0.8109
#Degrees of Freedom: 9 Total (i.e. Null); 8 Residual
#Null Deviance: 13.86
#Residual Deviance: 13.46 AIC: 17.46
#...
#...
来源:https://stackoverflow.com/questions/60598466/running-multiple-glm-models-on-mixed-data-with-purrr