## Saturday, October 6, 2012

### Simulating Social Network Hotspots

# This simulation will generate a simulated data set with social network connections.

# Unlike the previous post which made all connections randomly this post will have hotspots such as schools and common areas that networkers access which make it more likely for them to become part of the same social network.

# For reference on how to us R to analyze social networks, check out: Mike Nowak and Sean Westwood. 2010. "Social Network Analysis Labs in R." Stanford University.

# First lets decide how many people we would like to simulate in our social network.

npeople <- 50

# We will create an edgelist which defines all of the relationships between everybody in the network.

# First let's create a pure random network.

# First we will populate it by a list of all of the edges.
rnet <- data.frame(ego=rep(1:npeople,each=npeople), alter=rep(1:npeople,times=npeople), friendship=0, friendships=0)

# Now let's add three hotspots.  These hotspots will be randomly assigned to inviduals.

rnet$hot1 <- runif(npeople) # Perhaps, physical location. rnet$hot2 <- runif(npeople)
# Age group perhaps.
rnet$hot3 <- runif(npeople) # Soioeconomic background. # I will use a scaled version of the equclidean distance of the three hotspots to calculate the probability that any two people are connected. euc.scale = 4 # Unlike the previous post which allowed anybody to be within a network with anybody else the hotspots will restrict social networks to people who are near each other in all three hotspots. for (i in 1:npeople) for (ii in (i+1):npeople) if (i!=ii) { # Calculate Euclidean distance between person i and ii. distance <- ((rnet$hot1[i]-rnet$hot1[ii])^2+ (rnet$hot2[i]-rnet$hot2[ii])^2+ (rnet$hot2[i]-rnet$hot2[ii])^2)^.5 # Make sure the probability of two people being friends is always positive. p.friend <- max(1-distance*euc.scale,0) # Now draw one draw using the probabily. if (rbinom(1,1,p.friend)==1) { rnet$friendship[(rnet$ego==i & rnet$alter==ii)] <- 1
rnet$friendship[(rnet$ego==ii & rnet$alter==i)] <- 1 rnet$friendships[rnet$ego==i] <- rnet$friendships[rnet$ego==i] + 1 # rnet$friendships[rnet$ego==ii] <- rnet$friendships[rnet$ego==ii] + 1 } } head(rnet) # We can see that as we had planned there is a specific number of connections. summary(rnet) summary(rnet$friendships)

# For graphing our connections we will use the package igraph
require(igraph)

# Let's try generating our first graph:
g = graph.data.frame(rnet[rnet$friendship==1,], directed=F) plot(g, vertex.size=(rnet$friendships[rnet\$friendship==1]+2)*2)
# I was hoping to have the scale of the dots to be proportional to the numnber of connections.  However, clearly this is not yet working.  Something to figure out for later posts.