R语言Logistic回归

最后发布时间:2022-03-14 14:05:17 浏览量:

病例对照研究
队列研究

单因素logistic回归分析

单个分类变量的logistic

点击下载

barcodeYTHDC1stagefutime
TCGA-4T-AA8H-01A-11R-A41B-076.127690833Stage II385
TCGA-AA-A00Q-01A-01R-A002-076.378889099Stage III1278
TCGA-AZ-4614-01A-01R-1410-076.373133808Stage IV172
TCGA-A6-2675-01A-02R-1723-076.135427897Stage II1321
TCGA-AA-A00U-01A-01R-A002-076.444999352Stage III518
TCGA-A6-6781-01A-22R-1928-076.212837814Stage III598
TCGA-CM-6169-01A-11R-1653-076.570983898Stage II396
TCGA-AA-3516-01A-02R-0826-075.670502217Stage III396

处理数据

y <- ifelse(expr$YTHDC1>median(expr$YTHDC1),1,0)
df <- data.frame(y=y,x=expr$stage,YTHDC1 = expr$YTHDC1)

建立模型

logistic <- glm(y~x,family = binomial(link="logit"),data = df)
summ <- summary(logistic)
Call:
glm(formula = y ~ x, family = binomial(link = "logit"), data = df)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.23169  -1.16396  -0.00977   1.19092   1.21159  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)   0.1268     0.2255   0.562    0.574
xStage II    -0.2068     0.2715  -0.762    0.446
xStage III   -0.0795     0.2870  -0.277    0.782
xStage IV    -0.1585     0.3382  -0.469    0.639

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 615.51  on 443  degrees of freedom
Residual deviance: 614.83  on 440  degrees of freedom
AIC: 622.83

Number of Fisher Scoring iterations: 3

查看系数

coefficients(logistic)
(Intercept)xStage IIxStage IIIxStage IV
0.12675171-0.20679441-0.07949882-0.15850040

使用模型预测

predict_=predict.glm(logistic,type="response",newdata=df)
table(predict_)
my_predict <- function(x2=0,x3=0,x4=0){
  a <- 0.12675171+ (x2* -0.20679441)+ (x3* -0.07949882) +(x4* -0.15850040)
  res <- exp(a)/(exp(a)+1)
  return(res)
}
my_predict()
my_predict(x2=1)
my_predict(x3=1)
my_predict(x4=1)

图片alt

图片alt

如果观测到某个分类变量,该分类变量的取值为1,其余虚拟变量的取值为0

计算OR值

conf <- confint(logistic,level = 0.95)
cbind(OR=exp(summ$coefficients[,1]),
      OR.95L=exp(conf[,1]),
      OR.95H=exp(conf[,2]),
      p=summ$coefficients[,4])
OROR.95LOR.95Hp
1.1351351350.7298630841.7727786810.574001544
0.8131868130.4762847671.3839230340.446310221
0.9235791090.5251147771.6208328380.781756194
0.8534226190.4386203181.6562657710.639262696

多因素logistic回归分析

参考