Search code examples
rtidyverselinear-regression

Extraction LM stats into table


I have made a graph that displays r2, p-value and equation from linear regressions in the top left corner using stat_poly_eq.

Now I wish to have the stats from the linear regression extracted into a table.

For an example, in the mtcars dataset, if I want to do linear regression on plots of hp against disp for each cylinder group (e.g. 4, 6, 8) and then extract the linear regression stats into a table, how could I do that?

Thanks!

Here's the graph I have:

library(ggplot2)
library(ggpmisc)

formula <- y~x

ggplot(mtcars, aes(disp, hp)) +
 geom_point() +
 geom_smooth(method = "lm",formula = formula) +
 theme_bw()+
 facet_wrap(~cyl, scales = "free")+
 stat_poly_eq(
  aes(label = paste(stat(adj.rr.label), stat(eq.label), stat(p.value.label), sep = "*\", \"*")),
  formula = formula, parse = TRUE, size=3)

enter image description here


Solution

  • Do you mean something like this?

    • With nest_by, divide the rest of the columns in separated tibbles by each cyl
    • With summarise, calculate each lm. You need to set it into a list.
    • Operate like a normal list with map and calculate the stuff you need: coefficients (extractable with broom::tidy) and adj.r.squared (with summary(.)$adj.r.squared)
    • unnest the result of broom::tidy to make a unique tibble.
    library(dplyr)
    library(tidyr)
    library(purrr)
    
    mtcars %>%
     nest_by(cyl) %>% 
     summarise(mdl = list(lm(hp ~ disp, data)), .groups = "drop") %>% 
     mutate(adjrsquared = map_dbl(mdl, ~summary(.)$adj.r.squared ),
            mdl = map(mdl, broom::tidy)) %>% 
     unnest(mdl)
    
    #> # A tibble: 6 x 7
    #>     cyl term        estimate std.error statistic p.value adjrsquared
    #>   <dbl> <chr>          <dbl>     <dbl>     <dbl>   <dbl>       <dbl>
    #> 1     4 (Intercept)  47.0       25.3       1.86  0.0960       0.0988
    #> 2     4 disp          0.339      0.234     1.45  0.182        0.0988
    #> 3     6 (Intercept) 177.        42.0       4.22  0.00829      0.117 
    #> 4     6 disp         -0.300      0.224    -1.34  0.238        0.117 
    #> 5     8 (Intercept) 178.        77.4       2.30  0.0405      -0.0682
    #> 6     8 disp          0.0890     0.216     0.413 0.687       -0.0682