【问题标题】:Rshiny SIR model in RstudioRstudio中的R Shiny SIR模型
【发布时间】:2020-06-22 11:17:42
【问题描述】:

我正在尝试使用 Rshiny 和 SIR() 模型创建交互式绘图。基本上,我想要 5 个滑块输入,它们是 beta、gamma、S、I 和 R,并在指定时间内输出 S、I、R 的图。

以下是我在 Rstudio 中的代码。忽略 S+I+R 应该等于 1 的事实。

有什么错误的建议吗?

ui <- fluidPage(
  
  # App title ----
  titlePanel("Sliders"),
    sliderInput(inputId = "gr1", "Beta:",
                min = 0, max = 3,
                value = 1.42),
    sliderInput(inputId = "gr2", "Gamma:",
                min = 0, max = 1,
                value = 0.142),
    sliderInput(inputId = "gr3", "S proportion:",
              min = 0, max = 1,
              value = 1 - 1e-06),
    sliderInput(inputId = "gr4", "I proportion:",
              min = 0, max = 1,
              value = 1e-06),
   sliderInput(inputId = "gr5", "R proportion:",
              min = 0, max = 1,
              value = 1e-06 + 1e-06),
  plotOutput("plot")
  )
  


server <- function(input, output){
  val <- SIR(pars = c(input$gr1,input$gr2), init = c(input$gr3,input$gr4,input$gr5), time = 0:70)$results
  output$plot <- renderPlot({
    
    
    with(val, {
      plot(time, S, type = "l", col = "blue",
           xlab = "period (days)", ylab = "Proportion of people")
      lines(time, I, col = "red")
      lines(time, R, col = "green")
    })
    
    legend("right", c("susceptibles ratio", "infectious ratio", "recovered ratio"),
           col = c("blue", "red", "green"), lty = 1, bty = "n")
  })  
}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 什么是SIR函数?
  • 你能提供你得到的错误吗?
  • 下面使用 reactive() 的解决方案是正确的。非常感谢

标签: r


【解决方案1】:

非常酷的图表,但图书馆有一些非常相似的东西,叫做PlotMods

library(shiny)
library(EpiDynamics)


ui <- fluidPage(
  
  # App title ----
  titlePanel("Sliders"),
  numericInput(inputId = "gr1", "Beta:",
              min = 0, max = 3,
              value = 1.42),
  numericInput(inputId = "gr2", "Gamma:",
              min = 0, max = 1,
              value = 0.142),
  numericInput(inputId = "gr3", "S proportion:",
              min = 0, max = 1,
              value = 0.999999),
  numericInput(inputId = "gr4", "I proportion:",
              min = 0, max = 1,
              value = 1e-06),
  numericInput(inputId = "gr5", "R proportion:",
              min = 0, max = 1,
              value = 2e-06),
  plotOutput("plot")
)

server <- function(input, output){
  
  parameters <- reactive(c(beta = input$gr1, gamma = input$gr2))
  initials <- reactive(c(S = input$gr3, I = input$gr4, R = input$gr5))
  
  # Solve and plot.

  val <- reactive(SIR(pars = parameters(), init = initials(), time = 0:70)$results)
  output$plot <- renderPlot({
    
    
    with(val(), {
      plot(time, S, type = "l", col = "blue",
           xlab = "period (days)", ylab = "Proportion of people")
      lines(time, I, col = "red")
      lines(time, R, col = "green")
    })
    
    legend("right", c("susceptibles ratio", "infectious ratio", "recovered ratio"),
           col = c("blue", "red", "green"), lty = 1, bty = "n")
  })  
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多