Ode ggplot Error

library(deSolve)

CCHFSimpleSIR = function(t, x, params)
{
  # get SIR values
  SH = x[1]
  EH = x[2]
  IH = x[3]
  RH = x[4]
  ST = x[5]
  ET = x[6]
  IT = x[7]
  
  
  # Beta values
  betaHH = params["betaHH"]
  betaTH = params["betaTH"]
  betaTTV = params["betaTTV"]
  betaTTH = params["betaTTH"]
  
  gamma = params["gamma"]
  
  muH = params["muH"]
  muT = params["muT"]
  
  # birth rates
  piH  = params["piH"]
  piT = params["piT"]
  
  deltaH1 = params["deltaH1"]
  deltaT = params["deltaT"]
  
  # recovery rate
  alpha = params["alpha"]
  
  
  #computations
  
  dSHdt = piH - (betaHH * SH * IH) - (betaTH * SH * IT) - (muH * SH)
  dEHdt = (betaHH * SH * IH) + (betaTH * SH * IT) - ((deltaH1 + muH) * EH)
  dIHdt = (deltaH1 * EH) - (alpha + gamma + muH) * IH
  dRHdt = (alpha * IH) - (muH * RH)
  dSTdt = piT - (betaTTV * ST * IT) - (betaTTH * ST * IT) - (muT * ST)
  dETdt = (betaTTV * ST * IT) + (betaTTH * ST * IT) - ((deltaT + muT) * ET)
  dITdt = (deltaT * ET) - (muT * IT)
  
  # return results
  list(c(dSHdt, dEHdt, dIHdt, dRHdt, dSTdt, dETdt, dITdt))
}

params = c(betaHH = .2, betaTH = 1.2, betaTTV = .4, betaTTH = 4, gamma = .15, muH = (1/79 * 365), muT = (1/2 * 365), piH = 18.5, piT = 100, deltaH1 = 1/2, deltaT = 1/1, alpha = 1/15)

# time to start solution 
time = seq(from = 0, to = 25, by = 1)

#initialize initial conditions
initialX = c(SH = 999, EH = 0, IH = 0, RH = 0, ST = 2000, ET = 0, IT = 2)

library(tidyverse)
data = ode(y = initialX, times = time, func = CCHFSimpleSIR, parms = params)%>%
  as.data.frame()

# graph data
ggplot(data, aes(x=time, y= initialX)) + geom_line()


Hi! This is my first time programming in R and I am trying to simulate an SIR model. I am getting an error on my last line " Aesthetics must be either length 1 or the same as the data (26): y". If anyone could help me understand this error that would be great thank you!

data2 <- pivot_longer(data=data,
                      cols=-time,
                      names_to = "initialX",
                      values_to="value")

ggplot(data2, aes(x=time, y= value,color=initialX)) + geom_line(lty="dashed")

Thank you! If it isn't a hassle could you explain your code a little bit? I'm new to R and still trying to understand everything especially graphing I have been having trouble with. Thank you so much.

pivot_longer transforms the structure of your dataframe (lengthening it).
read about it by ?pivot_longer on your console.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.