将glm的结果保存到R中的矩阵中

问题描述 投票:0回答:2

我使用 glm 进行多变量逻辑回归。

example <- glm(Death ~ age + sex + time, db, family = binomial)

我只想将结果保存到 R 中的一个简单矩阵中,就像这样:

其中 A 列 = 比值比; B= 较低的 95%CI; C= 上 95%CI。每行对应一个变量的结果(所以如果年龄是第一个变量,OR:1.4,95% CI:0.9 - 1.6)

我该怎么做?问题是 glm 的结果会自动保存到列表中,所以我不知道如何编码。

此外,有没有办法将 OR 和 95%CI 所指的变量自动放在单独的列中?我只需要这张表来做图,我不想混淆自己。

r dataframe matrix dataset logistic-regression
2个回答
0
投票

我找到了解决方案!希望它可能对某人有用

example <- glm(Death ~ age + sex + time, db, family = binomial)
CI <- exp(confint.default(example))
aOR <- exp(coefficients(example))

df<-as.data.frame(CI) %>%
                  mutate(aOR=aOR) %>%
                  filter(!row_number() %in% c(1)) 

df <- df[, c(3,1,2)]
rm(CI, example, aOR)

0
投票
summary(glm(...))$coefficients # contains estimates, standard errors and inferential statistics for all predictors

估计 +/- 1.96 se 导致上下 95% 置信区间。所以下面的代码应该可以解决问题:

# for 95%-Conficene Intervals around regression coefficients (coefficients = log(odds))
df<- as.data.frame(summary(example)$coefficients)
df$CI_lower <- df$Estimate-1.96*df$`Std. Error`
df$CI_upper <- df$Estimate+1.96*df$`Std. Error`
matrix<-as.matrix(df[c("Estimate","CI_lower","CI_upper")])
© www.soinside.com 2019 - 2024. All rights reserved.