Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #MIS 30.03.2020 zadatak 5 SIR model
  2. # Zemlja: Japan
  3.  
  4. PopSt <- 126800000 # populacija japana
  5.  
  6. library(simecol)
  7. library(ggplot2)
  8.  
  9. sir_model <- function(time, # current simulation time unit (day)
  10. init, # number of susceptible, infected and recovered at this time unit
  11. parms # beta and gamma values
  12. ) {
  13. # Function with unpacks given values for easier access
  14. # Note: with only works on lists, not on vectors
  15. with(as.list(c(init, parms)), {
  16. # Once unpacked we can now use these values by their names
  17. dS <- -beta * S * I
  18. dI <- beta * S * I - gamma * I
  19. dR <- gamma * I
  20.  
  21. # This return is a result for with function
  22. return(list(c(dS, dI, dR)))
  23. })
  24.  
  25. # Result from "with()" function is then returned by the main (sir_model(...)) function as we don't have to explicitly write return command when the last line of a function resolves in a value
  26. }
  27.  
  28. param_values <- c(
  29. beta = 0.000000355 / (PopSt / 1000000), # infection rate
  30. gamma = 0.2 # recovery rate
  31. )
  32.  
  33. init_values <- c(
  34. S = PopSt-1, # number of susceptibles at time = 0
  35. I = 1, # number of infectious at time = 0
  36. R = 0 # number of recovered (and immune) at time = 0
  37. )
  38.  
  39. times <- c(from=0, to=200, by=1)
  40.  
  41. SIR <- odeModel(main = sir_model,
  42. parms = param_values,
  43. times = times,
  44. init = init_values)
  45.  
  46. SIR <- sim(SIR)
  47.  
  48. SIR.data <- out(SIR)
  49. head(SIR.data)
  50.  
  51. #graf
  52. ggplot(SIR.data, aes(time)) +
  53. geom_line(aes(y = S, colour = "Podlozno zarazi")) +
  54. geom_line(aes(y = I, colour = "Zarazeno")) +
  55. geom_line(aes(y = R, colour = "Ozdravljeno")) +
  56. labs(color = "State")
  57.  
  58. R_0.func <- function(init, parms) {
  59.  
  60. with(as.list(c(init, parms)), {
  61. # Total population * beta / gamma
  62. sum(S + I + R) * beta / gamma
  63. })
  64.  
  65. }
  66.  
  67. SIM.R_O <- R_0.func(init = init_values,
  68. parms = param_values)
  69.  
  70. print(SIM.R_O)# R0 = 1.775
  71.  
  72. # -- { Prema sluzbenim podacima } -- #
  73. # Ukupno slucajeva: 1,866
  74. # Ukupno umrlih: 54
  75. # Ukupno ozdravljeno: 424
  76. # Prvi slucaj: Jan 14
  77. # Proteklo dana od prvog slucaja: 76 dana ne uključujući današnji dan (30.3)
  78.  
  79. times2 <- c(from=0, to=76, by=1)
  80.  
  81. SIR <- odeModel(main = sir_model,
  82. parms = param_values,
  83. times = times2,
  84. init = init_values)
  85.  
  86. SIR <- sim(SIR)
  87. SIR.data <- out(SIR)
  88. print(SIR.data)
  89.  
  90. ggplot(SIR.data, aes(time)) +
  91. geom_line(aes(y = S, colour = "Podlozno zarazi")) +
  92. geom_line(aes(y = I, colour = "Zarazeno")) +
  93. geom_line(aes(y = R, colour = "Ozdravljeno")) +
  94. labs(color = "State")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement