Hello guys,
I have got following code of basic SIR model. I need the differential equation to be solved without ode() function or any other. Does anybody know, how to make it happen?
library(deSolve)
library(shiny)
ui <- fluidPage(
titlePanel("Choose amounts"),
fluidRow(column(width = 4,
sliderInput(inputId = "time_values", label = "Days", value = 10, min = 1, max = 10),
sliderInput(inputId = "beta", label ="Disease", value = 0.05, min = 0, max = 1, step = 0.05),
sliderInput(inputId = "gamma", label ="Cure", value = 0.5, min = 0, max = 1, step = 0.1),
),
column(width = 8,
(plotOutput("plot"))
)
)
)
server <- function(input, output) {
sir_equations <- function(time, variables, parameters) {
with(as.list(c(variables, parameters)), {
dS <- -beta * I * S
dI <- beta * I * S - gamma * I
dR <- gamma * I
return(list(c(dS, dI, dR)))
})
}
initial_values <- c(S = 1000, I = 1, R = 0)
sir_values_1 <- reactiveValues(val = data.frame())
observe({
sir_values_1$val <- as.data.frame(ode(
y = initial_values,
times = seq(0, input$time_values),
func = sir_equations,
parms = c(beta=input$beta, gamma=input$gamma)
))
})
output$plot <- renderPlot({
with(sir_values_1$val, {
plot(sir_values_1$val$time, sir_values_1$val$S, type = "l", col = "blue",
xlab = "Days", ylab = "Number of people")
lines(sir_values_1$val$time, sir_values_1$val$I, col = "red")
lines(sir_values_1$val$time, sir_values_1$val$R, col = "green")
legend("right", c("halthy", "infected", "cured"),
col = c("blue", "red", "green"), lty = 1, bty = "n")
})
})
}
shinyApp(ui = ui, server = server)
Thank you