【问题标题】:Polr function error: "response must be a factor"Polr 函数错误:“响应必须是一个因素”
【发布时间】:2020-09-17 11:34:21
【问题描述】:

我正在使用polr 函数来运行有序逻辑回归。 Reglog 是我的数据框,Reglog$wtp 是响应变量。这是我的模型中所有变量的列表。

  1. wtp
  2. 感知
  3. 收入
  4. 年龄
  5. 教育
  6. 经验
  7. 首页

一开始wtpchr,所以我把它转换成ordered factor

Reglog$WTP <- as.ordered(Reglog$WTP) 

str(Reglog) 
tibble [30 x 7] (S3:
tbl_df/tbl/data.frame)  
$ WTP : Ord.factor w/ 3 levels "1"<"2"<"3": 1 1 3 2 2 2 2 1 2 2 ...

结果显示wtp是一个有序因子,但是当我使用polr运行回归时,它说wtp不是一个因子。

> model <- polr(wtp~persepsi+pendapatan+usia+pendidikan+pengalaman+pendapatanrt, data = Reglog, Hess = TRUE)
Error in polr(wtp ~ persepsi + pendapatan + usia + pendidikan + pengalaman +  : 
  response must be a factor

我该如何解决这个问题?谢谢。

【问题讨论】:

  • 看起来您在一个 WTP 引用中使用所有大写字母,而在另一个引用中使用非大写字母。

标签: r


【解决方案1】:

您可能需要使用method = c("logistic")

polr(wtp ~ persepsi + pendapatan + usia + pendidikan + pengalaman + pendapatanrt, data = Reglog, Hess = TRUE, method = c("logistic"))

如果这不起作用,请尝试

polr(as.factor(wtp) ~ persepsi + pendapatan + usia + pendidikan + pengalaman + pendapatanrt, data = Reglog, Hess = TRUE, method = c("logistic"))

【讨论】:

  • 好的。但我遇到了另一个错误:polr 中出现意外的字符串常量。它的目的是wtp,所以我将其转换为有序因子,但该错误仍然存​​在。我该如何解决?谢谢。
  • 您是否尝试运行我回答中的代码?你有同样的错误吗?
猜你喜欢
  • 2013-07-15
  • 2017-02-01
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 2021-05-27
相关资源
最近更新 更多