I'm trying to run simple linear regressions on a grouped data frame and create a summarised data frame containing the intercept, gradient and R^2 value for each regression. I know I can collect the intercept and gradient for a single regression using lm(formula = var1 ~ var2, data = df)$coefficients[["(Intercept)"]] and lm(formula = var1 ~ var2, data = df)$coefficients[["y"]] respectively, however when I try to combine this with summarise I get the following error:
Error in lm(formula = var1 ~ var2)$coefficients[["y"]] : subscript out of bounds
(The R^2 value calculates just fine). Here is a small reproducible example:
library(dplyr)
## Create dummy data frame
df <- tibble(treatment = factor(c(rep("A", 5), rep("B", 5))),
var1 = c(1, 4, 5, 7, 2, 8, 9, 1, 4, 7),
var2 = c(2, 8, 11, 13, 4, 10, 11, 2, 6, 10)) %>%
group_by(treatment)
reg <- df %>%
## Intercept of linear model
summarise(intercept = lm(formula = var1 ~ var2)$coefficients[["(Intercept)"]],
## Gradient of linear model
gradient = lm(formula = var1 ~ var2)$coefficients[["y"]],
## R^2 value of linear model
r2 = cor(x = var1, y = var2, use = "complete.obs"))
How do I need to change my code to extract these values successfully for each linear model? Do I need to try a different approach entirely than using summarise?
coefficients[["var2"]and it should work. - Dave2e