在以下广义回归模型中,如果两列相似,为什么一列会出现错误,而另一列却没有出现错误?

In the following generalized regression model, why am I getting an error with one column and not with the other column if both are similar?

提问人:Ba Lalo 提问时间:11/10/2023 最后编辑:jay.sfBa Lalo 更新时间:11/10/2023 访问量:28

问:

我有以下数据框和拟合模型:

goyo <- data.frame(
  Grade <- c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8),
  a <- c(NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, 1, NA, 1, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
  b <- c(0, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, 0, NA, NA, NA, 1, NA, NA, 1, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)
goyo$Grade <- factor(goyo$Grade)
glm(reformulate(c("Grade"), "a"), family=binomial, data = goyo)
glm(reformulate(c("Grade"), "b"), family=binomial, data = goyo)

当我运行列为“b”的拟合模型时,出现错误:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

看起来这与级别数量有关。但是,两列看起来几乎相同,我不明白它们之间有什么区别,使它们给出不同的结果。

R 调试 回归 GLM

评论


答:

0赞 jay.sf 11/10/2023 #1

glm(b ~ Grade)根据

> goyo[complete.cases(goyo[c('Grade', 'b')]), ]
   Grade  a b
1      8 NA 0
9      8 NA 0
14     8  1 0
18     8  1 1
21     8 NA 1
23     8 NA 0

并且不能对 Grade 应用对比,这解释了

> glm(reformulate(c("Grade"), "b"), family=binomial, data = goyo)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

检查一下,这与 .goyo[complete.cases(goyo[c('Grade', 'a')]), ]a ~ Grade

顺便说一句,在 b 上回归等级不是更有意义吗?

> reformulate(termlabels="b", response="Grade")
Grade ~ b