简体   繁体   English

如何使用R进行逻辑回归,测试有序分类变量中线性趋势的偏离?

[英]How do you test for departure from linear trend across ordered categorical variables with logistic regression using R?

I'm testing for a linear trend in the log odds of a binary outcome across an ordered categorical independent variable. 我正在测试有序分类自变量中二进制结果的对数几率的线性趋势。 This is straightforwardly achieved by treating the independent variable as continuous. 通过将自变量视为连续变量可以直接实现。

I'm also trying to test for departure from linear trend. 我还试图测试是否偏离线性趋势。 I understand that this is about comparing (a) the model where the independent variable is categorical with (b) the model where the independent variable is treated as continuous. 我了解这是将(a)将自变量分类的模型与(b)将自变量视为连续的模型进行比较。 But I'm not sure how to do this in R. 但是我不确定如何在R中执行此操作。

Can anyone help? 有人可以帮忙吗?

I've created a reproducible example below. 我在下面创建了一个可复制的示例。 The first 7 lines just create a dataset. 前7行仅创建一个数据集。 model1 treats the independent variable as categorical; model1将自变量视为分类变量; model2 as continuous. model2为连续的。

model2 provides strong evidence that a linear trend explains the trend better than no trend at all, but a linear trend is clearly not a very good explanation in this case. model2提供了有力的证据,线性趋势比完全没有趋势更好地解释了趋势,但是在这种情况下,线性趋势显然不是很好的解释。

# create dataset 'a'

n <- 200
ngroups <- 7
a <- data.frame(group = rep(letters[1:ngroups], n), group2 = rep(1: ngroups, n), n = runif(n * ngroups, 0, 1))
y <- data.frame(group = letters[1:ngroups], fac = 1/(1 + exp(-1 * 1:ngroups)))
a <- merge(a, y, by = "group")
a$n2 <- a$n * a$fac
a$ind <- ifelse(a$n2 > quantile(a$n2)[4], 1, 0)

# run models

model1 <- glm(ind ~ group, data = a, family = "quasibinomial")
model2 <- glm(ind ~ group2, data = a, family = "quasibinomial") 

Answering my own question... 回答我自己的问题...

You do a likelihood ratio test to compare the two models: 您进行似然比测试以比较两个模型:

anova(model1, model2, test = "LRT")

This is likely to give a low p-value, suggesting that the models are different, and there is evidence that a non-linear trend fits the data better than a linear trend. 这很可能会给出较低的p值,这表明模型是不同的,并且有证据表明非线性趋势比线性趋势更适合数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM