1

I have an issue that might be silly in some ways, but following this question:

Linear Regression and group by in R

I tried to install the broom package in order to "retrieve the coefficients and Rsquared/p.value".

I know that the previous question is 12 years old but this package is still listed in my RStudio for installation, but then I have this error message and I am lost on what to do to make it work properly:

library(broom) Error in value[3L]: Package 'broom' version 0.7.12 cannot be loaded: Error in unloadNamespace(package): namespace 'broom' is imported by 'modelr', 'tidyverse', 'rstatix' and therefore cannot be unloaded

So my question is straightforward: what does it mean? Did broom become a dependancy of the 3 packages cited? How to make it work?

Thank you very much for your help.

EDIT: screenshot of the output to know why some numbers appear in red.

enter image description here

Recology
  • 165
  • 1
  • 10
  • 1
    Have you tried calling broom::tidy() on your model object? e.g. model_data <- lm(data = mtcars, formula = mpg ~ cyl + disp + hp + drat + wt) broom::tidy(model_data) – jpenzer Apr 17 '22 at 07:55
  • > broom::tidy(fitted_models) Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : is.atomic(x) is not TRUE In addition: Warning messages: 1: Data frame tidiers are deprecated and will be removed in an upcoming release of broom. 2: In mean.default(X[[i]], ...): the argument is neither numeric nor logical: return NA – Recology Apr 17 '22 at 08:00
  • What type of object is fitted_models in this case, and what code did you use to generate it? – jpenzer Apr 17 '22 at 08:15
  • > class(fitted_models) [1] "rowwise_df" "tbl_df" "tbl" "data.frame" – Recology Apr 17 '22 at 08:17
  • fitted_models = dataset %>% group_by(condition) %>% do(model = lm(day_number ~ time, data = .)) – Recology Apr 17 '22 at 08:17
  • > fitted_models$model [[1]] Call: lm(formula = day_number ~ time, data = .) Coefficients: (Intercept) time 57.253 -5.884 //and so on for every condition so it seems to work. – Recology Apr 17 '22 at 08:19

1 Answers1

3

Given your comments, you should be able to purrr::map broom::tidy over your list column of models.

fitted_models$model %>%
    purrr::map(broom::tidy)

This returns a list of your models with the coefficients, p-values etc. tidied.

You can also mutate a new column into your fitted_models data frame/tibble to keep your data frame/tibble data type. Note that we include model in the map() call because we are piping from fitted_models, not fitted_models$model:

fitted_models %>%
    mutate(tidied_models = purrr::map(model, broom::tidy)
jpenzer
  • 739
  • 2
  • 8
  • thank you very much for your answer. Just one more precision, why some numbers appear in red in the results? Does it mean something (e.g. significancy)? I will edit my post and put a screenshot of the outpu to be clearer. – Recology Apr 17 '22 at 08:38
  • and maybe one last question, any way to show the Diagnostic Plots for every lm to check the assumptions? – Recology Apr 17 '22 at 08:43
  • 1
    It depends where the red is popping up, but under coefficient it means that the variable has a negative effect. You won't find red values under std.error and p.value as they can't be negative. 'Statistic' - I believe is the t statistic, which is the coefficient divided by the std.error, thus it will be negative when the coefficient is negative and it is essentially an estimate of the coefficient's variance. – jpenzer Apr 17 '22 at 08:43
  • 1
    Yes, you can alsp map over your models to see the diagnostic plots. I'm not sure about storing them in a column, it could be possible. To see the plots call: fitted_models$model %>% map(plot) – jpenzer Apr 17 '22 at 08:46
  • sorry, last error to debug: > fitted_models$model %>% map(plot) Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character' / I should call purrr::map(plot) right? – Recology Apr 17 '22 at 08:51
  • 1
    I don't have access to your models/data so it's hard to say precisely what's happening. Is model still the original model objects, rather than the tidied model? Try taking a look at fitted_models %>% unnest(model) and then another call to fitted_models %>% unnest(tidied_models). See if you have any all NAs or similar, if you can't determine the cause, you'll need to create a new question I feel :) – jpenzer Apr 17 '22 at 08:56
  • these lines actually seem to work: `par(mfrow=c(2,2)) fitted_models$model %>% purrr::map(plot)` / output is all the diagnostic plots and this appears in the console for every lm: `[[1]] NULL [[2]] NULL [[3]] NULL` – Recology Apr 17 '22 at 09:02