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 }keep recovery rate (mu) at 0.1
mu <- 0.1
randomly set 1% as seeds - this is 12 nodes (of our 1133 total)
set.seed(123) num_seeds <- ceiling(0.01 * vcount(graph_undirected)) seeds <- sample(1:vcount(graph_undirected), num_seeds)
produce summary results and plot
results <- map_dfr(seq(0,0.2,0.01), # beta range (beta){ seeds <- sample(1:vcount(g),vcount(g)*0.01) realization <- sim_sir(g,beta,mu,seeds) # make a realization for each beta data.frame(beta,ninf=nrow(realization)) # create dataframe row }) results %>% ggplot(aes(x=beta,y=ninf))+ geom_point()+ geom_vline(xintercept = betac,linetype=2)
Assistant
To create a plot of infections over time for multiple beta values in your SIR model, you can extend your existing code to simulate the model for each beta value and then aggregate the results for plotting. Below is a step-by-step guide to achieve this:
- Modify the Simulation Function to Track Infections Over Time: Ensure the simulation function returns the number of infections at each time step for each beta value.
- Run Simulations for Multiple Beta Values: Use a loop or
mapto run the simulation for each beta value. - Aggregate and Plot the Results: Combine the results and plot the number of infections over time for each beta value.
Here’s the complete code to achieve this:
# Load necessary libraries library(tidyr) library(dplyr) library(ggplot2) library(igraph) # Assuming 'graph_undirected' is already defined # graph_undirected <- ... # SIR simulation function with time series tracking sim_sir_time <- function(g, beta, mu, seeds) { state <- rep(0, vcount(g)) state[seeds] <- 1 t <- 0 infections_over_time <- data.frame(t = 0, n_infected = sum(state == 1)) while(sum(state == 1) > 0) { t <- t + 1 new_inf <- c() for(i in which(state == 1)) { neighbors <- neighbors(g, i, mode = "out") for(j in neighbors) { if(state[j] == 0 && runif(1) < beta) { new_inf <- c(new_inf, j) } } if(runif(1) < mu) { state[i] <- 2 } } state[new_inf] <- 1 infections_over_time <- rbind(infections_over_time, data.frame(t = t, n_infected = sum(state == 1))) } infections_over_time } # Parameters mu <- 0.1 beta_values <- seq(0.01, 0.2, 0.01) set.seed(123) num_seeds <- ceiling(0.01 * vcount(graph_undirected)) seeds <- sample(1:vcount(graph_undirected), num_seeds) # Run simulations for multiple beta values results <- map_dfr(beta_values, ~{ beta <- .x realization <- sim_sir_time(graph_undirected, beta, mu, seeds) realization$beta <- beta realization }) # Plot infections over time for each beta value results %>% ggplot(aes(x = t, y = n_infected, color = factor(beta))) + geom_line() + labs(title = "Infections Over Time for Different Beta Values", x = "Time (t)", y = "Number of Infected Nodes", color = "Beta Value") + theme_minimal()Explanation:
-
Simulation Function (
sim_sir_time):- Tracks the number of infected nodes at each time step.
- Returns a data frame with time (
t) and the number of infected nodes (n_infected).
-
Running Simulations for Multiple Beta Values:
- Uses
map_dfrfrom thepurrrpackage to run the simulation for each beta value and combine the results into a single data frame. - Each simulation result is tagged with the corresponding beta value.
- Uses
-
Plotting:
- Uses
ggplot2to create a line plot showing the number of infections over time for each beta value. - The
coloraesthetic is used to differentiate between different beta values.
- Uses
This code will generate a plot that allows you to compare how the infection spreads over time for different beta values in your SIR model.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.