之前做回归,老自己设训练集啥的,有人为误差,为了减少误差,进行循环逻辑回归,并从中选出最优模型
一、常规导入数据啥的
# install.packages("pROC")
library(bestglm)
##########################回归#############
daata <- read.csv('C:\\Users\\xikun\\Desktop\\R\\R\\循环检测验证logistic_LC\\胃癌\\all_score79.csv', header = TRUE,stringsAsFactors = TRUE)
二、逻辑回归
1、普通逻辑回归
link=logit可以不写,因为logit是二项分布族连接函数的缺省状态。
lg<-glm(y ~x1+x2,data=file,family=binomial(link='logit'))
summary(lg)
参数family规定了回归模型的类型:
----family="gaussian"适用于一维连续因变量
----family=mgaussian"适用于多维连续因变量
----family="poisson"适用于非负次数因变量(count)
----family="binomial"适用于二元离散因变量(binary)
----family="multinomial"适用于多元离散因变量(category)
#处理数据
daata <- replace(daata,daata=='#N/A',NA)
daata <- daata[,-6]
#daata <- daata[complete.cases(daata),]
daata$year <- as.numeric(daata$year)
dataPRS <- daata[,c(1,4)]
切10份,9份用于回归,1份验证,
L<-bestglm(Xy = dataPRS, IC = "CV", CVArgs = list(Method="HTF", K=10,
REP=1), family = binomial)
#logistic <- glm(result ~ PRS+sex+age, daata, family=binomial(link = 'logit'))
formatFit<-function(fit){
#取P值
p<-summary(fit)$coefficients[,4]
#wald值
wald<-summary(fit)$coefficients[,3]^2
#B值
valueB<-coef(fit)
#OR值
valueOR<-exp(coef(fit))
#OR值得95%CI
confitOR<-exp(confint(fit))
data.frame(
B=round(valueB,3),
Wald=round(wald,3),
OR_with_CI=paste(round(valueOR,3),"(",
round(confitOR[,1],3),"~",round(confitOR[,2],3),")",sep=""),
P=format.pval(p,digits = 3,eps=0.001)
)
}
formatFit(L)
summary(L)
pROC画曲线,看看曲线下面积情况
####################pROC画曲线#####################
library(pROC)
rocobj1 <- roc(daata$result, L$BestModel$fitted.values)
plot(rocobj1,
legacy.axes = TRUE,
main="ROC曲线最佳阈值点",
thresholds="best", # 基于youden指数选择roc曲线最佳阈值点
print.thres="best")
roc_result <- coords(rocobj1, "best")