R语言Logistic回归
最后发布时间:2022-03-14 14:05:17
浏览量:
病例对照研究
队列研究
单因素logistic回归分析
单个分类变量的logistic
barcode | YTHDC1 | stage | futime |
---|---|---|---|
TCGA-4T-AA8H-01A-11R-A41B-07 | 6.127690833 | Stage II | 385 |
TCGA-AA-A00Q-01A-01R-A002-07 | 6.378889099 | Stage III | 1278 |
TCGA-AZ-4614-01A-01R-1410-07 | 6.373133808 | Stage IV | 172 |
TCGA-A6-2675-01A-02R-1723-07 | 6.135427897 | Stage II | 1321 |
TCGA-AA-A00U-01A-01R-A002-07 | 6.444999352 | Stage III | 518 |
TCGA-A6-6781-01A-22R-1928-07 | 6.212837814 | Stage III | 598 |
TCGA-CM-6169-01A-11R-1653-07 | 6.570983898 | Stage II | 396 |
TCGA-AA-3516-01A-02R-0826-07 | 5.670502217 | Stage III | 396 |
处理数据
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 II | xStage III | xStage 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)
如果观测到某个分类变量,该分类变量的取值为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])
OR | OR.95L | OR.95H | p |
---|---|---|---|
1.135135135 | 0.729863084 | 1.772778681 | 0.574001544 |
0.813186813 | 0.476284767 | 1.383923034 | 0.446310221 |
0.923579109 | 0.525114777 | 1.620832838 | 0.781756194 |
0.853422619 | 0.438620318 | 1.656265771 | 0.639262696 |