当前位置:   article > 正文

让ChatGPT编写交互式网页应用的临床预测模型_开发模型可视化工具,方便临床应用

开发模型可视化工具,方便临床应用

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完成。

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"))
                    )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这样就可以自由切换。

image.png

Shiny集合

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

image.png

image.png

Code

这里附上源代码:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/839919
推荐阅读
相关标签
  

闽ICP备14008679号