R语言 在下面的广义回归模型中,如果两列都相似,为什么我在一列中得到错误,而在另一列中没有?

3npbholx  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(76)

我有以下 Dataframe 和拟合模型:

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


这看起来像是与水平的数量有关。然而,这两列看起来几乎相同,我不明白它们之间的区别是什么,使它们给予不同的结果。

ovfsdjhp

ovfsdjhp1#

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

字符串
没有对比度可以应用于等级,这解释了

> 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

相关问题