abc
abc

Reputation: 27

adding predictions for logistic regression (grouped variable)

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

Upvotes: 0

Views: 417

Answers (1)

David Robinson
David Robinson

Reputation: 78590

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.

Upvotes: 0

Related Questions