Balamurali N.R
Balamurali N.R

Reputation: 353

Running multiple simple linear regressions from a nested dataframe/tibble

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.

Upvotes: 2

Views: 1154

Answers (1)

eipi10
eipi10

Reputation: 93821

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)

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")

Upvotes: 2

Related Questions