问题描述
我试图基于嵌套数据框中的数据运行多个简单的线性回归,并使用tidy()将回归拟合系数存储在数据框中.我的代码块如下
I am trying to run multiple simple linear regressions based on data from a nested data frame and store the regression fit coefficients in a dataframe using tidy(). My code block is as follows
library(tidyverse)
library(broom)
library(reshape2)
library(dplyr)
Factors <- as.factor(c("A","B","C","D"))
set.seed(5)
DF <- data.frame(Factors, X = rnorm(4), Y = rnorm(4), Z= rnorm(4))
MDF <- melt(DF, id.vars=c("Factors","X"))
DFF <- MDF %>% nest(-Factors)
如果它是一个包含许多列的数据框,我可以使用以下方法进行多个简单的线性回归
If it is a single dataframe with many columns, I can do multiple simple linear regressions using
MDF %>% group_by(variable) %>% do(tidy(lm(value ~ X, data =.)))
或者如果它是一个嵌套的数据框,并且我必须运行一个简单的线性回归,我可以尝试
or if it is a nested dataframe and I have to run one simple linear regression, I can try
MDF %>% nest(-Factors)
%>% mutate(fit = map(data, ~lm(Y ~ X, data = .)), results = map(fit,tidy))
%>% unnest(results)
但是我需要做的是上述两种情况的结合.我需要从嵌套数据框中的数据运行多个简单的线性回归.
But What I need to do is a combination of both of the above cases. I need to run multiple simple linear regressions from data in nested dataframe.
推荐答案
您可以通过两个分组变量来嵌套
:
You could nest
by both grouping variables:
MDF %>% nest(-Factors, -variable) %>%
mutate(fit = map(data, ~lm(value ~ X, data = .)),
results = map(fit,tidy)) %>%
unnest(results)
您还可以使用 split
并避免嵌套:
You could also use split
and avoid nesting:
split(MDF, list(MDF$Factors, MDF$variable)) %>%
map_df(~ tidy(lm(value ~ X, data=.x)) %>%
mutate(Factors=.x$Factors[1],
variable=.x$variable[1]))
或者,如果您不介意单个列中的组标识符,则:
Or, if you don't mind the group identifiers in a single column:
split(MDF, list(MDF$Factors, MDF$variable), sep="_") %>%
map_df(~ tidy(lm(value ~ X, data=.x)), .id="Factors_variable")
这篇关于从嵌套的数据框/小标题运行多个简单的线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!