更改预测变量时如何确保 odds.ratio ?
how to make sure the odds.ratio when the predictor is changed?
我遇到了一个问题。我使用 glm 计算 odd.raio。
当预测结果分别为 "A" 和 "B" 时,odd.ratio 值为 0.88。但是,结果是 "C" 和 "B",odd.ratio 值为 1.127948。
我不知道glm中的"AB"和"BC"有什么区别。该字符可以影响 odd.ratios 的结果
哪个是对的?
示例代码
outcome <- as.factor(c(rep("C",30),rep("B",28)))
#outcome <- as.factor(c(rep("A",30),rep("B",28)))
a <- c(seq(1,30,1),seq(1,28,1))
glm.log <- glm(outcome ~ scale(a), control = list(maxit = 50), family = binomial(link = "logit"))
res <- summary(glm.log)$coefficients[2, 1:2]
odd.ratio <- exp(res[1])
print(odd.ratio)
odds ratio
显示了如果我们遇到预测变量 (formula) 的变化,odds
将如何变化。这是一个二进制组变量的示例,因为这里的更改很容易理解。
# generate data and run model
outcome <- as.factor(c(rep("C",30),rep("B",30)))
groups<- c(c(rep(1, 20), rep(2, 10)),
c(rep(2, 20), rep(1, 10)))
fit <- glm(outcome ~ groups, family= binomial(link = "logit"))
odds.ratio <- exp(fit$coefficients["groups"])
# calculate odds by hand
tbl <- table(outcome, groups)
a <- tbl[1,1]
b <- tbl[1,2]
c <- tbl[2,1]
d <- tbl[2,2]
odds1 <- a/c
odds2 <- b/d
odds.ratio; odds1/odds2
# both 0.25
如您所见,模型 odds.ratio
和我们的优势比 odds1/odds2
为 0.25
。如果我们 运行 与 outcome <- as.factor(c(rep("A",30),rep("B",30)))
相同的代码,则 odds.ratio
将是 4
。这是因为计算出相反的比例,即odds2/odds1
.
此外,正如@Dason 指出的那样,您可以从另一个计算 odds ratio
。看这里
odds_ratio_a <- odds1/odds2
odds_ratio_b <- odds2/odds1
odds_ratio_a == 1/odds_ratio_b
# TRUE
odds_ratio_b == 1/odds_ratio_a
# TRUE
这就是为什么你的情况 1/ 1.127948 = 0.8865657
和 1/0.8865657 = 1.127948
。
问题仍然是为什么会发生这种情况。这很简单:因为参考水平发生了变化!在 as.factor(c(rep("C",30),rep("B",28)))
中参考水平是 "B",在 as.factor(c(rep("A",30),rep("B",28)))
中是 "A"。要在这两种情况下获得相同的结果,您只需使用 outcome <-relevel(as.factor(c(rep("A",30),rep("B",28))), ref= "B")
.
我遇到了一个问题。我使用 glm 计算 odd.raio。
当预测结果分别为 "A" 和 "B" 时,odd.ratio 值为 0.88。但是,结果是 "C" 和 "B",odd.ratio 值为 1.127948。
我不知道glm中的"AB"和"BC"有什么区别。该字符可以影响 odd.ratios 的结果 哪个是对的?
示例代码
outcome <- as.factor(c(rep("C",30),rep("B",28)))
#outcome <- as.factor(c(rep("A",30),rep("B",28)))
a <- c(seq(1,30,1),seq(1,28,1))
glm.log <- glm(outcome ~ scale(a), control = list(maxit = 50), family = binomial(link = "logit"))
res <- summary(glm.log)$coefficients[2, 1:2]
odd.ratio <- exp(res[1])
print(odd.ratio)
odds ratio
显示了如果我们遇到预测变量 (formula) 的变化,odds
将如何变化。这是一个二进制组变量的示例,因为这里的更改很容易理解。
# generate data and run model
outcome <- as.factor(c(rep("C",30),rep("B",30)))
groups<- c(c(rep(1, 20), rep(2, 10)),
c(rep(2, 20), rep(1, 10)))
fit <- glm(outcome ~ groups, family= binomial(link = "logit"))
odds.ratio <- exp(fit$coefficients["groups"])
# calculate odds by hand
tbl <- table(outcome, groups)
a <- tbl[1,1]
b <- tbl[1,2]
c <- tbl[2,1]
d <- tbl[2,2]
odds1 <- a/c
odds2 <- b/d
odds.ratio; odds1/odds2
# both 0.25
如您所见,模型 odds.ratio
和我们的优势比 odds1/odds2
为 0.25
。如果我们 运行 与 outcome <- as.factor(c(rep("A",30),rep("B",30)))
相同的代码,则 odds.ratio
将是 4
。这是因为计算出相反的比例,即odds2/odds1
.
此外,正如@Dason 指出的那样,您可以从另一个计算 odds ratio
。看这里
odds_ratio_a <- odds1/odds2
odds_ratio_b <- odds2/odds1
odds_ratio_a == 1/odds_ratio_b
# TRUE
odds_ratio_b == 1/odds_ratio_a
# TRUE
这就是为什么你的情况 1/ 1.127948 = 0.8865657
和 1/0.8865657 = 1.127948
。
问题仍然是为什么会发生这种情况。这很简单:因为参考水平发生了变化!在 as.factor(c(rep("C",30),rep("B",28)))
中参考水平是 "B",在 as.factor(c(rep("A",30),rep("B",28)))
中是 "A"。要在这两种情况下获得相同的结果,您只需使用 outcome <-relevel(as.factor(c(rep("A",30),rep("B",28))), ref= "B")
.