前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >让ChatGPT编写交互式网页应用的临床预测模型

让ChatGPT编写交互式网页应用的临床预测模型

作者头像
Jamesjin63
发布2023-03-08 14:59:41
1.6K0
发布2023-03-08 14:59:41
举报
文章被收录于专栏:EpiHubEpiHub

R Shiny是一种基于Web的交互式数据可视化工具,能够帮助研究人员和临床医生快速构建交互式应用程序,从而进行数据分析和可视化。

在临床决策中,R Shiny可以用于以下方面:

  1. 数据可视化:医生可以使用R Shiny构建交互式图表和图形,以更好地展示和解释患者的病情和治疗效果。
  2. 临床预测模型:R Shiny可以帮助医生构建和验证临床预测模型,以便更好地了解患者的风险和预测未来病情的可能性。
  3. 决策支持系统:R Shiny可以用于构建决策支持系统,帮助医生制定更准确、更个性化的治疗方案。
  4. 临床试验监管:R Shiny可以用于临床试验监管,帮助研究人员快速掌握数据,监测研究的进展和效果。

那么,结合R强大的数据分析能力,在医学领域Shiny有哪些应用呢?这里给出了介绍。 https://zhuanlan.zhihu.com/p/471281332

模型准备

1.准备数据(测试集/训练集) 2.建立Logistics回归模型 3.预测指标(AUC) 4.个体预测概率

上述模型的准备是关键,其实Shiny只是可视化的展示网页,并进行交互式的操作。详细案例见:OR与RR的计算及可视化展示

Shiny基础

这里不多做介绍,直接看官网链接

image.png

ChatGPT编写shiny

ChatGPT编程运行的怎么样,我们来看看。

image.png

在这个示例程序中,使用了numericInputselectInput函数创建输入变量,使用actionButton函数创建计算患病概率的按钮。在Server端,使用reactive函数创建数据框data和逻辑回归模型model

image.png

一个大致的界面就完成了,而且出现了一些错误,所以ChatGPT也并不是完美的。

接下来我们将对界面这个进行完成

逐步完善shiny

在空白处增加两个数据输出图像输出框架,可以借助tabBox完成。

代码语言:javascript
复制
tabBox(title = "Data Result",height = "500px", width = NULL,    
                    tabPanel("Dataview",h4("we can see the inut data"),
                             textOutput("text1"),
                             dataTableOutput("data1"), # 展示录入数据
                             dataTableOutput("data2"),
                             textOutput("prediction_text")
                             ), # 展示模型输出
                    tabPanel("Plotfigure", h4("Here output the AUC"),
                             textOutput("text2"),
                             verbatimTextOutput("Data_Management_Normal"),
                             plotOutput("plot1"))
                    )

这样就可以自由切换。

image.png

Shiny集合

这里贴上,其人网站的优化UI,给大家做扩展,以后可以按照这些来设计。

image.png

image.png

Code

这里附上源代码:

代码语言:javascript
复制
library(shiny)
library(ggplot2)
library(pROC)  
library(DT)
library(tidyverse)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)

# 产生fake数据
set.seed(123) # for reproducibility

# create a data frame with patient data
patients <- data.frame(
  age = round(rnorm(500, 50, 10)), # age (normally distributed, mean=50, sd=10)
  sex = sample(c("M", "F"), 500, replace = TRUE), # sex
  blood_pressure = round(rnorm(500, 120, 10)), # blood pressure (normally distributed, mean=120, sd=10)
  cholesterol = round(rnorm(500, 200, 30)), # cholesterol (normally distributed, mean=200, sd=30)
  blood_sugar = round(rnorm(500, 5, 1)), # blood sugar (normally distributed, mean=5, sd=1)
  heart_rate = round(rnorm(500, 70, 10)), # heart rate (normally distributed, mean=70, sd=10)
  disease = sample(c(0, 1), 500, replace = TRUE, prob = c(0.8, 0.2)) # heart disease (20% of patients have the disease)
)


test_data<- data.frame(
  age = round(rnorm(200, 50, 10)), # age (normally distributed, mean=50, sd=10)
  sex = sample(c("M", "F"), 200, replace = TRUE), # sex
  blood_pressure = round(rnorm(200, 120, 10)), # blood pressure (normally distributed, mean=120, sd=10)
  cholesterol = round(rnorm(200, 200, 30)), # cholesterol (normally distributed, mean=200, sd=30)
  blood_sugar = round(rnorm(200, 5, 1)), # blood sugar (normally distributed, mean=5, sd=1)
  heart_rate = round(rnorm(200, 70, 10)), # heart rate (normally distributed, mean=70, sd=10)
  disease = sample(c(0, 1), 200, replace = TRUE, prob = c(0.8, 0.2)) # heart disease (20% of patients have the disease)
)


# create a factor variable for sex
patients$sex <- factor(patients$sex)

# show the first few rows of the dataset
head(patients)

# fit a logistic regression model
modelx <- glm(disease ~ age + sex + blood_pressure + cholesterol + blood_sugar + heart_rate, 
             data = patients, family = "binomial")

# show the model summary
summary(modelx)

library(broom)
# convert model output to a tidy data frame
model_summary <- tidy(modelx)




## Shiny web
ui <- fluidPage(
  titlePanel("临床预测模型"),
  sidebarLayout(
    sidebarPanel(
      numericInput("age", "年龄(岁):", min = 0, max = 150, value = 50),
      selectInput("sex", "性别:", choices = c("F", "M")),
      numericInput("blood_pressure", "血压(mmHg):", min = 0, max = 300, value = 120),
      numericInput("cholesterol", "胆固醇(mg/dL):", min = 0, max = 500, value = 200),
      numericInput("blood_sugar", "血糖(mmol/L):", min = 0, max = 50, value = 5),
      numericInput("heart_rate", "心率(次/分):", min = 0, max = 300, value = 70),
      actionButton("calculate", "计算患病概率")
    ),
    
    mainPanel(
      
      

             tabBox(title = "Data management",height = "500px", width = NULL,   
                    tabPanel("Dataview",h4("we can see the inut data"),
                             textOutput("text1"),
                             dataTableOutput("data1"), # 展示录入数据
                             dataTableOutput("data2"),
                             textOutput("prediction_text")
                             ), # 展示模型输出
                    tabPanel("Plotfigure", h4("Here output the AUC"),
                             textOutput("text2"),
                             verbatimTextOutput("Data_Management_Normal"),
                             plotOutput("plot1"))
                    ),

    )
  )
)

server <- function(input, output) {
  # 定义数据框
  datax <- reactive({
    data.frame(
      age = input$age,
      sex = factor(input$sex),
      blood_pressure =input$blood_pressure,
      cholesterol = input$cholesterol,
      blood_sugar = input$blood_sugar,
      heart_rate = input$heart_rate) 

  })
  
  # 输出录入数据
  output$data1 <- renderDataTable({
    datax()
  })
  
  # 输出LR模型结果
  output$data2 <- renderDataTable({
    model_summary
  })

  # 计算患病概率
  prediction <- eventReactive(input$calculate, {
    prob <- predict(modelx,newdata =  datax(),type = "response")
    prob 
  })
  
  # 输出患病概率
  output$text1 <- renderText({
    paste0("该病人的患病概率为:", round(prediction() * 100, 2), "%")
    #print(str(datax()))
  })
  
  # 绘制AUC图表
  output$plot1 <- renderPlot({
    ggplot(mtcars)+ geom_point(aes(mpg,hp))
    #验证集
    test_data$P1<-predict(modelx,newdata = test_data , type = "response")
    roc1 <- plot.roc(test_data$disease,test_data$P1,ci=TRUE,print.auc=TRUE,levels = c(0,1),
                     direction='<')
    plot(roc1, print.auc=TRUE,auc.polygon=TRUE, grid=c(0.2, 0.2),
         grid.col=c("green", "red"), max.auc.polygon=TRUE,legacy.axes=T,
         auc.polygon.col="skyblue", print.thres=F,main="mtDNA-CN")
    
  })
  
  
}

shinyApp(ui = ui, server = server)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模型准备
  • Shiny基础
  • ChatGPT编写shiny
  • 逐步完善shiny
  • Shiny集合
  • Code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档