【问题标题】:Error when running poisson regression with a binary outcome使用二元结果运行泊松回归时出错
【发布时间】:2017-07-13 03:58:27
【问题描述】:

我正在尝试运行泊松回归来预测常见二元结果。

这是我第一次尝试使用dput - 如果我使用不当,请告诉我,以便我更正。

示例数据:

df <- structure(list(id = 1:30, sex = structure(c(1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L), .Label = c("Female", "Male"
), class = "factor"), migStat = structure(c(1L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("Australian-born", 
"Migrant"), class = "factor"), mhAreaBi = structure(c(1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("Metropolitan", 
"Regional"), class = "factor"), empStatBi = structure(c(2L, 2L, 
1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Student / employed", 
"Unemployed"), class = "factor"), pensBenBi = structure(c(1L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("No benefit", 
"In receipt of pension benefit"), class = "factor"), maritStatBi = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("Married (including de facto)", 
"Not married"), class = "factor"), cto = structure(c(1L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("No", 
"Yes"), class = "factor")), .Names = c("id", "sex", "migStat", 
"mhAreaBi", "empStatBi", "pensBenBi", "maritStatBi", "cto"), row.names = c(NA, 
-30L), class = "data.frame")

在 R 中使用 glm 运行回归时,我收到一个错误:

fit <- glm(cto ~ sex + migStat + mhAreaBi + empStatBi + pensBenBi + maritStatBi, df, family = poisson)

Error in if (any(y < 0)) stop("negative values not allowed for the 'Poisson' family") : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(y, 0) : ‘<’ not meaningful for factors

同样的错误已经简单解释了in this thread:

因为没有为因子定义“if 的长度为 0。在 RHS 上设置因子变量 并在 hte LHS 上使用整数值成功。

将结果转换为整数时不会出现错误;然而,这个:

  1. 似乎违背了预测二元结果的目的(除非范围为 0-1 的数值变量被视为具有两个水平的因子变量);和
  2. 似乎没有必要(至少根据这个post,它使用来自geepackgeeglm 来预测二进制结果[不幸的是,当我将代码改编为我自己的数据集时,我收到同样的错误])

问题:

我能收到关于错误的进一步解释吗?

如果我将结果转换为范围为 0-1 的整数,glm 会将其视为二进制变量吗?如果没有,是否有更适合对常见二元结果进行回归的方法?

【问题讨论】:

  • as.numeric(df$cto == "Yes") 将为您提供在glm 中运行良好的 0 和 1。但通常您对此类二元结果使用逻辑回归,对计数或比率变量使用泊松回归,其中结果可以取任何大于 0 的整数值。您确定泊松是您分析的好选择吗?
  • @Marius 我很感激这个小费!在大学期间,我被告知逻辑回归用于二元结果,泊松回归用于计数数据。最近,我所在大学的一位统计学家告诉我,逻辑回归适用于二元结果很少见的情况,但当结果很常见时它会遇到麻烦。在这些情况下,使用泊松回归应该更好。这是 CV 上的一个帖子 - link
  • 关于您上面的第 1 点,“除非”之后的语句是正确的——转换为 0,1 二进制变量(即虚拟变量)正是您想要做的。在您链接的geeglm 示例中,结果编码为TRUEFALSE——即10——这就是为什么他们没有在那篇文章中转换,但能够执行回归。
  • 为了清楚起见 - 对于 any 给定的 0/1 变量,如果编码为因子或数字,它们将是等效的?因此,R 将可靠地区分编码为 0/1(二进制)和 0、1、2、...(计数)的两个数字变量?
  • 当因子被强制转换为数字时,您必须注意完成的顺序。比较 levels(factor(letters[1:3]))levels(factor(letters[1:3], levels = c("c", "a", "b"))) 并将它们强制转换为数字。

标签: r binary glm poisson


【解决方案1】:

我认为这里最好的选择是:

df$cto_binary <- as.numeric(df$cto == "Yes")
fit <- glm(cto_binary ~ sex + migStat + mhAreaBi + empStatBi + pensBenBi + maritStatBi, 
           df, family = poisson)

通过这种方式,您可以在代码中明确显示二进制结果中的 1/成功,并且不会被因子级别排序之类的事情绊倒。请注意,在 R 中,as.numeric(c(FALSE, TRUE)) 给出了c(0, 1),因此您始终知道从逻辑比较中会得到什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2018-08-02
    • 2015-05-07
    • 2017-04-20
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多