Original text link: tecdat.cn/?p=14593 original text source: Tuo End number according to tribal public number
Different from ordinary diffusion research network diffusion begins to consider the influence of network structure on the diffusion process. Here is an example of using R to simulate network diffusion.
The basic algorithm is very simple: generate a network: G (V, E). One or more nodes are randomly selected as seeds. Each infected person affects the node to which it is connected with probability P (which can be seen as the node’s contagiousness and is usually expressed as ββ). In fact, this is the simplest SI model in the network implementation. Now, S means susceptible to infection and I means infected. The susceptible – infection-recovery (SIR) model is used to describe epidemics such as chickenpox and measles in which patients fully recover and gain lifelong immunity. For SIR epidemic transmission model, nodes can only be in susceptible state (S) or infected state (I) or recovering state (R) at any time. The susceptible node represents individuals who are not infected by the epidemic and are likely to be infected; The node of infective state indicates that it has been infected by epidemic and has the ability to spread; The recovery node represents a full recovery from an epidemic. Similar to SIS model, within each time step, each infected node tries to infect its neighbor susceptible node with probability λλ, and becomes recovered with probability γγ. SIR model can be expressed as follows:
S = S (t) is the number of susceptible individuals, I = I (t) is the number of infected individuals, and R = R (t) is the number of recovered individuals.
The second set of dependent variables represents the proportion of the total population in the three categories. So, if N is the total population (7.9 million in our example), we have
S (T) = S (T)/N, the susceptible part of the population, I (T) = I (T)/N of the population infection fraction and R (T) = R (T)/N, the recovered part of the population.
By solving this differential equation, we can get an expression for the cumulative growth curve. Interestingly, this is a logistic growth with a distinct S-shaped curve. After crossing the critical point in the early stage, the growth of the model is fast, and then becomes slow in the later stage. Thus it can be used to describe and fit the diffusion of innovations. Of course, the SI model is very naive for disease transmission, mainly because infected individuals either recover with a certain probability or continue to enter a state that can be infected (S, which is extended to the SIS model) or become immune (R, which is extended to the SIR model). Immunity was represented by R, and the removal or recovery rate was represented by γγ. For information diffusion, such considerations are not needed for the time being.
The first step is to generate a network.
Rules of the network
g =graph.tree(size, children =2); plot(g)
Copy the code
g =graph.star(size); plot(g)
Copy the code
g =graph.full(size); plot(g)
Copy the code
g =graph.ring(size); plot(g)
Copy the code
g =connect.neighborhood(graph.ring(size), 2); Plot (g) # Nearest neighbor coupled networkCopy the code
Edges (erdos.renyi.game(size, 0.1)); edges(erdos.renyi.game(size, 0.1); Prob = 0.8)# barabasi.game(size); plot(g)Copy the code
The second step is to randomly select one or n random seeds.
# initiate the diffusers
seeds_num =1 diffusers =sample(V(g),seeds_num) ;
diffusers
## + 1/50 vertex:
## [1] 43
infected =list()
infected[[1]]=diffusers#
Copy the code
Step three, contagiousness
In this simple example, the contagiousness of each node is 0.5, that is, the node connected to it will be infected with a probability of 0.5, and the resilience of each node is 0.5, that is, it will be recovered with a probability of 0.5. Implementation in R is done by flipping a coin.
# # [1] 0Copy the code
Obviously, this can be easily extended to a more general case, such as the average infection capacity of a node is 0.128, which can be written as follows: the average recovery capacity of a node is 0.1
P =0.128 coins =c(rep(1, p*1000), rep(0,(1-p)*1000)) sample(Coins, 1, replace=TRUE, prob=rep(1/n, n)) ## [1] 0 n =length(coins2) sample(coins2, 1, replace=TRUE, prob=rep(1/n, n)) ## [1] 0Copy the code
Of course, the most important step is to be able to update the network node infection information by “time”.
keep =unlist(lapply(nearest_neighbors[,2], toss))
new_infected =as.numeric(as.character(nearest_neighbors[,1][keep >=1]))
diffusers =unique(c(as.numeric(diffusers), new_infected))
return(diffusers)}
set.seed(1);
Copy the code
Start the diffusion process!
Let’s start with the S-curve:
# # "growth_curve"num_cum =unlist(lapply(1:i, function(x) length(infected[[x]]) )) p_cum =num_cum time =1:i ## Large initial population size (X=1000) parms < - c (beta = 0.01, Gamma x0 = 0.1) < - c (S = 49, I = 1, R = 0) a < - c (" beta * S * I ", "gamma * I" nu < - matrix (c (1, 0, + 1, 1, 0, + 1), nrow = 3, byrow = TRUE) out <-ssa(x0,a,nu,parms,tf=4,simName="SIR model")Copy the code
To visualize the spread of the virus, we color the infected in red.
# generate a palette#
plot(g, layout =layout.old)
set.seed(1)#
library(animation)# start the plot
m =1
Copy the code
same=numeric(0)
for(m in 2:length(health))
if(length(setdiff(health[[m ]],health[[m -1 ]]) )==0){same=c(same,m)
}
health=health[-same]
infected=infected[-same]#
Copy the code
As in Netlogo, we can show both network proliferation and growth curves:
set.seed(1) # start the plot m =1 p_cum=numeric(0) h_cum=numeric(0) i_cum=numeric(0) while( m<50 ) {# start the plot Layout (matrix(c(1, 2, 1, 3), 2,2, byrow =TRUE), Widths = C (3,1), Heights = C (1, 1)) V(g)$color = "white" V(g)$color[V(g)%in%infected[[m ]] ] = "red" V(g)$color[V(g)%in%health[[m ]]] = "green" If (m < = length (infected)) plot (pp ~ time, type = "h", ylab = "PDF", xlab = "time", xlim = c (0, I), ylim = c (0, 1), frame.plot =FALSE) m =m +1 }Copy the code
reference
1.R language Poisson regression model analysis case
2. Numerical simulation in R language: Poisson regression model was simulated
3. Poisson regression analysis for R language
4.R language simulation and dynamic visualization of Buffon needle drop (Buffon needle drop) experiment
5. Use R language to simulate the mixed queuing random service queuing system
6. VaR comparison of GARCH (1,1), MA and historical simulation method
7.R language to do geometric Brownian motion simulation of complex financial products
8. Numerical simulation in R language: Poisson regression model is simulated
9. Pricing of reinsurance contracts under catastrophe risk under R language: Generalized linear Models and Pareto distributions