我一直在学习逻辑回归,并通过一个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]
答案 0 :(得分:0)
您可以在运行模型之前在数据框中创建所需的间隔。也许有更好的方法,但是在不查看数据的情况下,这样的方法应该起作用。
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)
答案 1 :(得分:0)
解决方案是在连续变量用于回归之前缩放它,方法是:
<context:property-placeholder location="classpath:application.properties"/>
<!-- BEANS -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${spring.datasource.driverClassName}"/>
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:sql_mapper.xml"/>
</bean>
<bean id="organisationMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.OrganisationMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>