我正在尝试使用包中的sjp.glm
命令来sjPlot
绘制 glm 模型的结果。一切正常,直到我将 y 结果指定为成功和失败的向量,这是可以调用glm 的方式之一。
为了说明问题
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)`
有什么解决方案吗?