统计计算模型的P值或者截距等变量是常有的事情,一般在变量不多的情况下,多数一个一个的手动计算,但不免有时原始数据的改动,导致一系列的变动,很不好受。
这时R语言的作用就很明显,只需要run
一下,就可以重新处理一遍数据。
同时R语言中的for
循环或者apply
族循环,也是解放双手的一大利器。
今天举个小栗子,说说循环这件事。
1 数据格式
- 136行27列,
- 27个变量,第一个变量与其余26个变量都需要建立一一联系
> str(data)
'data.frame': 136 obs. of 27 variables:
$ recurrence : int 1 0 1 0 1 1 1 0 0 0 ...
$ Etiology : Factor w/ 5 levels "1","2","3","4",..: 1 1 4 2 3 1 4 1 1 1 ...
$ Child.pugh : Factor w/ 3 levels "1","2","3": 1 2 1 1 1 2 2 2 1 2 ...
$ Ascites : Factor w/ 4 levels "0","1","2","3": 2 3 3 1 2 3 3 4 1 3 ...
$ ALBI.stage : Factor w/ 3 levels "1","2","3": 1 2 2 1 2 2 2 1 1 3 ...
$ CTP : Factor w/ 3 levels "1","2","3": 1 2 2 1 1 2 2 2 1 3 ...
$ treat.method : Factor w/ 3 levels "1","2","3": 2 1 2 1 1 1 2 3 1 1 ...
$ MELD : num 66.6 62 58.9 52.9 62.4 ...
$ Gender : int 1 1 1 1 0 0 1 0 0 1 ...
$ Fundus.of.stomach: int 2 2 2 2 2 2 2 2 2 2 ...
$ Glue : int 0 0 0 0 0 0 0 0 1 0 ...
$ age : int 27 51 54 52 69 48 44 51 48 51 ...
$ diameter : int 0 0 0 0 0 0 0 0 0 0 ...
$ WBC : num 2.6 2.39 4.04 3.97 3.9 ...
$ Hb : int 94 138 99 158 129 117 88 53 117 114 ...
$ PLT : int 87 67 44 122 52 76 265 61 50 104 ...
$ ALT : int 29 26 10 337 13 29 13 20 8 41 ...
$ AST : int 29 23 16 167 39 47 28 28 11 57 ...
$ GGT : int 57 74 37 103 47 38 138 24 11 87 ...
$ CHE : int 4695 4815 3023 7997 3139 3612 3467 4163 4101 1828 ...
$ INR : num 1.42 1.22 1.19 1.07 1.36 1.07 1.27 1.35 1.74 1.42 ...
$ CR : num 92 89 82 37 66 47 45 37 43 70 ...
$ Urea : num 9.5 10.3 5 4.6 9 3.9 4.8 10.2 4.3 5.8 ...
$ GLU : num 5.65 5.88 7.14 6.92 7.67 5.53 5.84 7.36 9.82 5.49 ...
$ Ammonia : num 15.4 8.5 13.1 11.7 57.4 31.9 25.3 2.6 32.3 11.1 ...
$ TBIL : num 31 15.8 9.1 19.1 27.1 23.3 29.7 12.3 11.8 51.2 ...
$ ALB : num 47.5 36.1 35.4 52.7 38.5 33.4 37.6 46.6 40 29.1 ...
2 建立formula
连接
> aa1 <- paste("recurrence",colnames(data[-1]),sep="~")
> aa1
[1] "recurrence~Etiology" "recurrence~Child.pugh"
[3] "recurrence~Ascites" "recurrence~ALBI.stage"
[5] "recurrence~CTP" "recurrence~treat.method"
[7] "recurrence~MELD" "recurrence~Gender"
[9] "recurrence~Fundus.of.stomach" "recurrence~Glue"
[11] "recurrence~age" "recurrence~diameter"
[13] "recurrence~WBC" "recurrence~Hb"
[15] "recurrence~PLT" "recurrence~ALT"
[17] "recurrence~AST" "recurrence~GGT"
[19] "recurrence~CHE" "recurrence~INR"
[21] "recurrence~CR" "recurrence~Urea"
[23] "recurrence~GLU" "recurrence~Ammonia"
[25] "recurrence~TBIL" "recurrence~ALB"
3 建立空数据集
P_1 <- list()
combine_1 <- list()
combine_2 <- data.frame()
P_2 <- data.frame()
4 for
循环
#for循环
for (i in 1:26) {
fit_logistic1 <- glm(formula(aa1[i]),family = binomial(),data = data)
P_1[[i]]<- data.frame(P=coef(summary(fit_logistic1))[,4])
combine_1[[i]]<-data.frame(exp(cbind(OR=coef(fit_logistic1),confint(fit_logistic1))))
#P_file <- paste0(paste("p",i,sep="_"),".csv")
#write.table(P[[1]],file=paste0("20211101/1/",P_file,sep = ""))
#OR_file <- paste0(paste("OR",i,sep="_"),".csv")
#write.table(combine[[i]],file=paste0("20211101/1/",OR_file,sep = ""))
combine_2 <- rbind(combine_2,combine_1[[i]])
P_2 <- rbind(P_2,P_1[[i]])
}
5 合并及导出数据
#合并
combine_3 <- cbind(combine_2,P_2)
write.csv(combine_3,"20211101/1/combine_3.csv")