dplyr 和 tidyr - 用因子一次计算很多线性模型
dplyr and tidyr - calculating a lot of linear models at once with factors
在深入阅读 tidyverse 之后,我开始一次拟合许多线性模型,如 this 中所述。也就是说,我会按照这些思路做一些事情:
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))
问题在于,当变量的类型不同时,这种方法会失败,例如,一个是数字,一个是因子,因为 gather()
函数将强制整个 value
向量转化为一个因素。例如,
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 message:
attributes are not identical across measure variables; they will be dropped
y covariate value
"numeric" "character" "character"
并且 value
列是一个字符,因此 nest()
的技巧将不再起作用,因为所有协变量都将作为因子输入。
我想知道是否有 tidy 方法可以做到这一点。
您可以在拟合模型时转换类型,但您应该按照评论中指出的那样小心操作,因为这可能会产生意想不到的后果。
如果您仍想转换,可以在整个框架上使用 readr 中的 type_convert
或仅在 "value" 上使用 type.convert
矢量。
使用type_convert
:
mutate(model = map(.x = data , .f = ~lm(y ~ value, data = readr::type_convert(.))))
使用type.convert
:
mutate(model = map(.x = data , .f = ~lm(y ~ type.convert(value), data = .)))
作为链的一部分,这些中的任何一个都会导致这种情况下的预期结果:
df %>%
gather(covariate, value, x1:x3) %>%
group_by(covariate) %>%
nest() %>%
mutate(model = map(.x = data , .f = ~lm(y ~ type.convert(value), data = .))) %>%
mutate(rsquared = map_dbl(.x = model, .f = ~summary(.)$r.squared))
# A tibble: 2 x 4
covariate data model rsquared
<chr> <list> <list> <dbl>
1 x1 <tibble [10 x 2]> <S3: lm> 0.33176960
2 x3 <tibble [10 x 2]> <S3: lm> 0.06150498
在深入阅读 tidyverse 之后,我开始一次拟合许多线性模型,如 this 中所述。也就是说,我会按照这些思路做一些事情:
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))
问题在于,当变量的类型不同时,这种方法会失败,例如,一个是数字,一个是因子,因为 gather()
函数将强制整个 value
向量转化为一个因素。例如,
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 message:
attributes are not identical across measure variables; they will be dropped
y covariate value
"numeric" "character" "character"
并且 value
列是一个字符,因此 nest()
的技巧将不再起作用,因为所有协变量都将作为因子输入。
我想知道是否有 tidy 方法可以做到这一点。
您可以在拟合模型时转换类型,但您应该按照评论中指出的那样小心操作,因为这可能会产生意想不到的后果。
如果您仍想转换,可以在整个框架上使用 readr 中的 type_convert
或仅在 "value" 上使用 type.convert
矢量。
使用type_convert
:
mutate(model = map(.x = data , .f = ~lm(y ~ value, data = readr::type_convert(.))))
使用type.convert
:
mutate(model = map(.x = data , .f = ~lm(y ~ type.convert(value), data = .)))
作为链的一部分,这些中的任何一个都会导致这种情况下的预期结果:
df %>%
gather(covariate, value, x1:x3) %>%
group_by(covariate) %>%
nest() %>%
mutate(model = map(.x = data , .f = ~lm(y ~ type.convert(value), data = .))) %>%
mutate(rsquared = map_dbl(.x = model, .f = ~summary(.)$r.squared))
# A tibble: 2 x 4
covariate data model rsquared
<chr> <list> <list> <dbl>
1 x1 <tibble [10 x 2]> <S3: lm> 0.33176960
2 x3 <tibble [10 x 2]> <S3: lm> 0.06150498