0

I am trying to perform a logistic regression by grouping I tried all the methods , that have been listed, yet i keep getting the following error

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"

 # analysis ----------------------------------------------------------------
    models<-  df %>% 
    group_by(organ) %>% 
    do(model = glm(IHC ~ Dose,
                     data = .,
                     family = binomial(logit))) %>%
    ungroup

    new<-predict(models, newdata = data.frame(dose=1:10))

    Error in UseMethod("predict") : 
      no applicable method for 'predict' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"


Organ_factor     organ                        Dose IHC
1             1   Diaphragm                   0.00   0
2             2   Diaphragm inflammation      0.00   0
3             3   Heart cell                  0.00   0
4             5   Heart  mixed cell           0.00   0
camille
  • 16,432
  • 18
  • 38
  • 60
abc
  • 27
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Right now the problem is that all your models are in a tibble. You would need to map predict over that model column. – MrFlick Apr 16 '20 at 21:36
  • I can't test possible solutions without something I can copy/paste into R. Maybe edit your question just to use a built-in data set for demonstration purposes. Or maybe these questions do what you need: https://stackoverflow.com/questions/52168341/make-prediction-for-each-group-differently or https://stackoverflow.com/questions/49924571/add-predictions-for-models-by-group (`glm` and `lm` should behave the same here.) – MrFlick Apr 16 '20 at 21:45

1 Answers1

0

You can use augment() from the broom package, combined with nest/map/unnest.

library(purrr)
library(broom)

models <- df %>%
  group_by(organ) %>%
  nest() %>%
  mutate(model = map(data, ~ glm(IHC ~ Dose, data = ., family = binomial(logit)),
         augmented = map2(model, data, augment))

models %>%
  unnest(augmented)

The predicted values will be in the column .fitted.

Note that they'll be on a logit scale: you may want to add type.predict = "response" as an argument to the augment function if you want them to be probabilities.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • I suspect there are values of `organ` for which there are no non-NA values of `IHC`, `Dose`, or both. Try putting `filter(!is.na(IHC), !is.na(Dose))` before the `group_by()`. – David Robinson Apr 16 '20 at 22:16
  • you were correct there were NAs, now i have the model run, however i am still unable to use augment , in the way the documentation shows. the error i get is : models %>% unnest(augmented) Error: Can't subset columns that don't exist. x The column `augmented` doesn't exist. – abc Apr 16 '20 at 22:20
  • Did you include the line `augmented = map2(model, data, augment)` as shown above? – David Robinson Apr 17 '20 at 02:16
  • hey @david - yeap i did – abc Apr 17 '20 at 15:55