【问题标题】:ordinal logistic regression with continuous variables - scaling具有连续变量的有序逻辑回归 - 缩放
【发布时间】:2019-09-03 01:47:21
【问题描述】:

我一直在学习逻辑回归,并通过 R 数据分析示例发现了这个 great post。我已经为我的分析调整了代码,到目前为止一切正常。

我确实有一个连续的预测器。我已经使用这些命令获得了一个表格,该表格显示了如果我们一次将因变量回归到我们的预测变量上,我们将获得的(线性)预测值。但是,该命令似乎将连续变量转换为分类变量。

> ## Ordinal logistic regression (OLR) ## 
> # https://stats.idre.ucla.edu/r/dae/ordinal-logistic-regression/
> mod_OLRfull <- polr(Percentage_f ~ Gender + SE_track + Total_testscore, data = mydata, Hess=TRUE)

> # calculate essential metrics
> ctable <- coef(summary(mod_OLRfull))
> p <- pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) * 2
> ctable <- cbind(ctable, "p value" = p)

> # check if assumption holds: proportional odds
> sf <- function(y) {
+   c('Y>=1' = qlogis(mean(y >= 1)),
+     'Y>=2' = qlogis(mean(y >= 2)),
+     'Y>=3' = qlogis(mean(y >= 3)))#,
+ #    'Y>=4' = qlogis(mean(y >= 4)))
+ }
> s <- with(mydata, summary(as.numeric(Percentage_f) ~ Gender + SE_track + Total_testscore, fun=sf))
> s
as.numeric(Percentage_f)     N= 286 

+---------------+-------+---+----+---------+----------+
|               |       |N  |Y>=1|Y>=2     |Y>=3      |
+---------------+-------+---+----+---------+----------+
|Gender         |male   | 97|Inf |1.2862109|-1.1685709|
|               |female |189|Inf |1.5170646|-0.8397507|
+---------------+-------+---+----+---------+----------+
|SE_track       |KSO    | 39|Inf |1.0647107|-1.3545457|
|               |TSO    | 40|Inf |0.7308875|-1.7346011|
|               |ASO    |207|Inf |1.6990501|-0.7591051|
+---------------+-------+---+----+---------+----------+
|Total_testscore|[ 2, 8)| 74|Inf |0.8602013|-1.6422277|
|               |[ 8,11)|104|Inf |1.6326948|-1.3156768|
|               |[11,13)| 58|Inf |1.3437347|-0.5663955|
|               |[13,16]| 50|Inf |2.4423470| 0.0000000|
+---------------+-------+---+----+---------+----------+
|Overall        |       |286|Inf |1.4350845|-0.9458495|
+---------------+-------+---+----+---------+----------+
> glm(I(as.numeric(Percentage_f) >= 2) ~ Gender + SE_track + Total_testscore, family = "binomial", data = mydata)

Call:  glm(formula = I(as.numeric(Percentage_f) >= 2) ~ Gender + SE_track + 
    Total_testscore, family = "binomial", data = mydata)

> glm(I(as.numeric(Percentage_f) >= 3) ~ Gender + SE_track + Total_testscore, family = "binomial", data = mydata)

> glm(I(as.numeric(Percentage_f) >= 4) ~ Gender + SE_track + Total_testscore, family = "binomial", data = mydata)


> s[, 4] <- s[, 4] - s[, 3]
> s[, 3] <- s[, 3] - s[, 3]
> s
as.numeric(Percentage_f)     N= 286 

+---------------+-------+---+----+----+---------+
|               |       |N  |Y>=1|Y>=2|Y>=3     |
+---------------+-------+---+----+----+---------+
|Gender         |male   | 97|Inf |0   |-2.454782|
|               |female |189|Inf |0   |-2.356815|
+---------------+-------+---+----+----+---------+
|SE_track       |KSO    | 39|Inf |0   |-2.419256|
|               |TSO    | 40|Inf |0   |-2.465489|
|               |ASO    |207|Inf |0   |-2.458155|
+---------------+-------+---+----+----+---------+
|Total_testscore|[ 2, 8)| 74|Inf |0   |-2.502429|
|               |[ 8,11)|104|Inf |0   |-2.948372|
|               |[11,13)| 58|Inf |0   |-1.910130|
|               |[13,16]| 50|Inf |0   |-2.442347|
+---------------+-------+---+----+----+---------+
|Overall        |       |286|Inf |0   |-2.380934|
+---------------+-------+---+----+----+---------+

问题:

如何更改我的变量 Total_testscore 以间隔 [ 2, 8), [ 8,11), [11,13), [13,16] 拆分?我想把它们改成[ 0, 5), [ 5,10), [10,13), [13,16]

【问题讨论】:

    标签: r statistics linear-regression logistic-regression scaling


    【解决方案1】:

    您可以在运行模型之前在数据框中创建所需的间隔。可能有更好的方法,但如果不查看数据,这样的方法应该可以工作。

    library(dplyr)
    
    mydata = mydata %>% 
      mutate(
        `Total_testscore_[0,5)` = ifelse(Total_testscore>= 0 & Total_testscore < 5,1,0),
        `Total_testscore_[5,10)` = ifelse(Total_testscore>= 5 & Total_testscore < 10,1,0),
        `Total_testscore_[10,13)` = ifelse(Total_testscore>= 10 & Total_testscore < 13,1,0),
        `Total_testscore_[13,16)` = ifelse(Total_testscore>= 13 & Total_testscore < 16,1,0)) %>% 
      select(.,-Total_testscore)
    

    【讨论】:

    • 是的,这行得通。还有一个更短的选项,在下面回答:)
    【解决方案2】:

    解决方案是在将连续变量用于回归之前对其进行缩放,使用:

    starters$Total_testscore_f <- cut(starters$Total_testscore, breaks = c(0,5,10,13,16))
    
    s <- with(mydata, summary(as.numeric(Percentage_f) ~ Gender + SE_track + Total_testscore_f, fun=sf))
    glm(I(as.numeric(Percentage_f) >= 2) ~ Gender + SE_track + Total_testscore_f, family = "binomial", data = mydata)
    glm(I(as.numeric(Percentage_f) >= 3) ~ Gender + SE_track + Total_testscore_f, family = "binomial", data = mydata)
    glm(I(as.numeric(Percentage_f) >= 4) ~ Gender + SE_track + Total_testscore_f, family = "binomial", data = mydata)
    s[, 4] <- s[, 4] - s[, 3]
    s[, 3] <- s[, 3] - s[, 3]
    s
    
    # plot 
    par(mfrow = c(1,1))
    plot(s, which=1:3, pch=1:3, xlab='logit', main=' ', xlim = c(-3,0))#xlim=range(s[,3:4]))
    #  suggesting that the proportional odds assumption may not hold
    
    as.numeric(Percentage_f)     N= 286 , 2 Missing 
    
    +-----------------+-------+---+----+----+---------+
    |                 |       |N  |Y>=1|Y>=2|Y>=3     |
    +-----------------+-------+---+----+----+---------+
    |Gender           |male   | 97|Inf |0   |-2.454782|
    |                 |female |189|Inf |0   |-2.356815|
    +-----------------+-------+---+----+----+---------+
    |SE_track         |KSO    | 39|Inf |0   |-2.419256|
    |                 |TSO    | 40|Inf |0   |-2.465489|
    |                 |ASO    |207|Inf |0   |-2.458155|
    +-----------------+-------+---+----+----+---------+
    |Total_testscore_f|(0,5]  | 25|Inf |0   |-1.912387|
    |                 |(5,10] |153|Inf |0   |-2.956124|
    |                 |(10,13]| 81|Inf |0   |-2.096264|
    |                 |(13,16]| 27|Inf |0   |-2.151035|
    +-----------------+-------+---+----+----+---------+
    |Overall          |       |286|Inf |0   |-2.380934|
    +-----------------+-------+---+----+----+---------+
    

    【讨论】:

    • 嗨!请解释为什么这是 OPs 代码的解决方案,因为在 SO 上不鼓励仅代码答案。这将有助于 OP 和该网站的未来访问者。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多