为逻辑回归添加预测(分组变量)

问题描述 投票:0回答:1

我正在尝试通过分组进行逻辑回归我尝试了已列出的所有方法,但仍然出现以下错误UseMethod(“ predict”)中的错误:没有适用于“预测”的适用方法,适用于类“ 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')"

器官因子剂量IHC1 1隔膜0.00 02 2 Dia肌发炎0.00 03 3心脏细胞0.00 04 5心脏混合细胞0.00 0

r dplyr logistic-regression
1个回答
0
投票

您可以使用扫帚软件包中的augment(),并结合使用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)

预测值将在.fitted列中。

请注意,它们将成对数级:如果希望它们成为概率,则可能需要将type.predict = "response"添加为扩充函数的参数。

© www.soinside.com 2019 - 2024. All rights reserved.