循环逻辑回归-bestglm

之前做回归,老自己设训练集啥的,有人为误差,为了减少误差,进行循环逻辑回归,并从中选出最优模型
一、常规导入数据啥的

# 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")

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值