Can you help me create a plot of infections over time with my network analysis. I have…
- User
Can you help me create a plot of infections over time with my network analysis. I have this function to create a table with infections over time. I want to run it for multiple beta values in a SIR model and compare the results over time for each beta setting.
function to create SIR simulation
SIR simulation function
sim_sir <- function(g, beta, mu, seeds){ state <- rep(0, vcount(g))
state[seeds] <- 1 # set initial spreaders t <- 0table <- data.frame(t=0, inf=seeds) while(sum(state == 1) > 0){
t <- t + 1 new_inf <- c() for(i in which(state == 1)){ # among all infected ones neighbors <- neighbors(g, i) for(j in neighbors){ if(state[j] == 0 && runif(1) < beta){ # If neighbor j is susceptible (i.e., state[j] == 0), then infect it with probability β. "runif(1) < beta" means to generate a random number between 0 and 1. If it’s less than β, the infection is considered successful. new_inf <- c(new_inf, j) } } if(runif(1) < mu){ # Recover an infected node with probability μ state[i] <- 2 } } state[new_inf] <- 1 if(length(new_inf) > 0){ # only record when there is new infection table <- rbind(table, data.frame(t=t, inf=new_inf)) } } table }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.