Created
October 20, 2014 09:25
-
-
Save chengjun/1b5836a6b617efe98004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(igraph) | |
# generate a social graph | |
node_number = 100 | |
g = barabasi.game(node_number) ; plot(g) | |
seeds_num = 1 | |
set.seed(2014); diffusers = sample(V(g),seeds_num) ; diffusers | |
infected =list() | |
infected[[1]]= diffusers | |
# for example, set percolation probability | |
transmission_rate = 0.4 | |
coins = c(1, 0) | |
probabilities = c(transmission_rate, 1-transmission_rate ) | |
# sample(coins, 1, rep=TRUE, prob=probabilities) # Generate a sequence | |
# toss the coins | |
toss = function(freq) { | |
tossing = NULL | |
for (i in 1:freq ) tossing[i] = sample(coins, 1, rep=TRUE, prob=probabilities) | |
tossing = sum(tossing) | |
return (tossing) | |
} | |
update_diffusers = function(diffusers){ | |
nearest_neighbors = data.frame(table(unlist(neighborhood(g, 1, diffusers)))) | |
nearest_neighbors = subset(nearest_neighbors, !(nearest_neighbors[,1]%in%diffusers)) | |
keep = unlist(lapply(nearest_neighbors[,2], toss)) | |
new_infected = as.numeric(as.character(nearest_neighbors[,1][keep >= 1])) | |
diffusers = unique(c(diffusers, new_infected)) | |
return(diffusers) | |
} | |
total_time = 1 | |
while(length(infected[[total_time]]) < node_number){ | |
infected[[total_time+1]] = sort(update_diffusers(infected[[total_time]])) | |
cat(length(infected[[total_time+1]]), "-->") | |
total_time = total_time + 1 | |
} | |
plot_time_series = function(infected, m){ | |
num_cum = unlist(lapply(1:m, | |
function(x) length(infected[[x]]) )) | |
p_cum = num_cum/node_number | |
p = diff(c(0, p_cum)) | |
time = 1:m | |
plot(p_cum~time, type = "b", | |
ylab = "CDF", xlab = "Time", | |
xlim = c(0,total_time), ylim =c(0,1)) | |
} | |
plot_time_series(infected, length(infected)) |
Hello noppanit,
I stumbled upon this code in my search for simulating transmission in contact networks. I had exactly the same errors as you. Found out that it has to do with that R is trying to compare and find uniques between two different types of objects, namely diffusers = igraph.vs where new_infected is a simple numeric vector.
My solution is to add a new line and change the class of new_infected so:
update_diffusers = function(diffusers){
nearest_neighbors = data.frame(table(unlist(neighborhood(g, 1, diffusers))));
nearest_neighbors = subset(nearest_neighbors, !(nearest_neighbors[,1]%in%diffusers))
keep = unlist(lapply(nearest_neighbors[,2], toss))
new_infected = as.numeric(as.character(nearest_neighbors[,1][keep >= 1]))
class(new_infected) <- "igraph.vs"
diffusers = unique(c(diffusers, new_infected))
return(diffusers)
}
This solved all problems for me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I ran the code I get this error.
Not sure if you know where the problem is?