在R中,我如何使用sjPlot和glm模型的cbind()结果?

时间:2017-12-20 04:26:59

标签: r glm sjplot

我正在尝试使用sjp.glm包中的sjPlot command来绘制glm模型的结果。一切正常,直到我将y结果指定为成功和失败的向量,这是glm can be called的方式之一。

说明问题

library(sjPlot)
library(sjmisc)
data(efc)

# example from docs
y <- ifelse(efc$neg_c_7 < median(na.omit(efc$neg_c_7)), 0, 1)
# create data frame for fitting model
df <- data.frame(y = to_factor(y),
                 sex = to_factor(efc$c161sex),
                 dep = to_factor(efc$e42dep),
                 barthel = efc$barthtot,
                 education = to_factor(efc$c172code))

fit1 <- glm(y ~., data = df, family = binomial(link = "logit"))

# plot works
sjp.glm(fit1)

# similar as above but with aggregates
y0 <- ifelse(efc$neg_c_7 < median(na.omit(efc$neg_c_7)), 0, 1)
y1 <- ifelse(efc$neg_c_7 >= median(na.omit(efc$neg_c_7)), 0, 1)
df <- data.frame(y0 = y0, 
                 y1 = y1,
                 sex = to_factor(efc$c161sex),
                 dep = to_factor(efc$e42dep),
                 barthel = efc$barthtot,
                 education = to_factor(efc$c172code))

df_agg <- df %>% 
  dplyr::group_by(sex, dep, barthel, education) %>%                            
  dplyr::summarise(c0=sum(y0),
                   c1=sum(y1))

fit2 <- glm(cbind(c0, c1) ~., data = df_agg, family = binomial(link = "logit"))

# plot works not
sjp.glm(fit2)

引发错误消息:

Adding missing grouping variables: `sex`, `dep`, `barthel`
Error in cbind_all(x) : Argument 2 must be length 275, not 213
In addition: Warning message:
Unknown columns: `cbind(c0, c1)` 

任何解决方案?

0 个答案:

没有答案