首页 > 解决方案 > 当响应已经是有序因子时,R 显示“getY(fullmf) 中的错误:响应需要成为一个因子”

问题描述

我正在使用 ordinal 包并在 R 上使用 clmm 函数,但是尽管确保我的响应变量是序数(又名有序因子),但仍然出现以下错误:

getY(fullmf) 中的错误:响应需要成为一个因素

这是有错误的代码,还显示了 R 如何将变量“帮助”理解为有序因子。

> library(ordinal)
> hyp1.model1<-clmm(helpfulness~reflectiontype+session+(1+reflectiontype|participant),data=hyp1data)
Error in getY(fullmf):response needs to be a factor
> unique(helpfulness)
[1] 4 2 1 3 0 
Levels: 0 < 1 < 2 < 3 < 4
> class(helpfulness)
[1] "ordered" "factor"

标签: rordinal

解决方案


对于 Roland 和 Ryan J Field 所说的话,我没有太多要补充的。我只是想解释一下。如果不查看您的数据,我可能完全错了。我已经使用示例数据进行了测试wine。该模型仅在 ~ 的左侧为数字时才给出这样的错误消息。当我用数值替换其他变量时,它没有给人们任何错误消息。

您的数据中可能有两个helpfulness:一个向量helpfulness和一个变量。helpfulnesshyp1data

> unique(helpfulness)
[1] 4 2 1 3 0 
Levels: 0 < 1 < 2 < 3 < 4
> class(helpfulness)
[1] "ordered" "factor"

只告诉向量helpfulness是一个因子。您的变量hyp1data$helpfulness可能仍然是数字。您可能想尝试class(hyp1data$helpfulness)检查一下。如果不是这种情况,请告诉我。我会删除这个答案。


推荐阅读