Sorry, I actually want to plot the final number of infections under each beta value…
- User
Sorry, I actually want to plot the final number of infections under each beta value while using my exact simulation function above, unless that provides an error? That simulation funciton should provide a table of the node and time it becomes infected. So I want this final code to be corrected to accurately plot my final infection values at each beta level
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
Looking at your code, there are a few issues to fix:
- You're using
gbut definedgraph_undirected - You're redefining
seedsinside the map function, overriding your original seeds - You need to define
betacfor the vertical line
Here's the corrected code:
# Keep recovery rate (mu) at 0.1 mu <- 0.1 # Set seeds once (randomly set 1% as seeds) set.seed(123) num_seeds <- ceiling(0.01 * vcount(graph_undirected)) seeds <- sample(1:vcount(graph_undirected), num_seeds) # Define betac (critical beta value) - you'll need to set this based on your network # For example, if you know the critical value or want to highlight a specific beta: betac <- 0.1 # Adjust this value as needed # Produce summary results and plot results <- map_dfr(seq(0, 0.2, 0.01), # beta range \(beta){ realization <- sim_sir(graph_undirected, beta, mu, seeds) # use consistent graph name and fixed seeds data.frame(beta, ninf = nrow(realization)) # create dataframe row }) results %>% ggplot(aes(x = beta, y = ninf)) + geom_point() + geom_vline(xintercept = betac, linetype = 2) + labs(title = "Final Number of Infections by Beta Value", x = "Beta (Transmission Rate)", y = "Total Number of Infections") + theme_minimal()The key changes:
- Used
graph_undirectedconsistently instead ofg - Removed the
seeds <- sample(...)line from inside the map function so you use the same initial seeds for all beta values - Added
betacdefinition (you'll need to set this to your desired critical value) - Added proper labels to the plot
This will now correctly show how the final number of infections varies with different beta values while keeping the initial conditions (seeds) constant across all simulations.
- You're using
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.