After I read more on tidyverse, I immediately began to customize many linear models, as described in this . Namely, I would do something in this direction:
library(dplyr)
library(tidyr)
library(purrr)
df <- data.frame(y = rnorm(10),
x1 = runif(10),
x2 = runif(10))
df %>%
gather(covariate, value, x1:x2) %>%
group_by(covariate) %>%
nest() %>%
mutate(model = map(.x = data , .f = ~lm(y ~ value, data = .))) %>%
mutate(rsquared = map_dbl(.x = model, .f = ~summary(.)$r.squared))
The problem is that this approach fails when the variables are not of the same type, for example, when they are numeric, but one is a factor, since the function gather()will force the entire vector valueto a coefficient. For instance,
df <- data.frame(y = rnorm(10),
x1 = runif(10),
x3 = sample(c("a", "b", "c"), 10, replace = TRUE))
df %>%
gather(covariate, value, x1:x3) %>%
sapply(class)
warning follows
Warning message:
attributes are not identical across measure variables; they will be dropped
y covariate value
"numeric" "character" "character"
and the column valueis a symbol, so trick c nest()will no longer work, since all covariates will be placed as factors.
I am wondering if there is a way to do this.