By this point in the semester, you will generally have used
igraph extensively and statnet only if you
have gotten precocious and checked out the alternate RPubs that I
provided. Igraph’s strengths are that it is generally considered to be
simpler (if a bit more mysterious) for graphing and simple operations.
Igraph does not, however, have a very extensive repitoire of statistical
operations.
For that reason, we are switching to statnet. Statnet is
actually a suite of packages, chief among them being sna
and network, each conceived by Carter
Butts. Statnet can do nearly everything that igraph can do, but with
the added bonus of being able to run the statistical operations that we
are covering now.
Because everyone has recorded ties using 1 to signify the presence of a tie and either a blank cell or 0 to indicate the absence of a tie, we have, together, created an adjacency matrix. An ajacency matrix describes which nodes are adjacent (connected) and which are not.
Now that the matrix have been completed, please take a couple minutes to make sure that the character that you coded has the ties that you observed. If the information in the network is accurate to the best of your knowledge, then you are ready to download the matrix and begin analyses.
The easiest means of getting the data into R involves downloading a sheet from the Google spreadsheet we created. When you download each sheet, save it as a csv file. This will simplify your task.
In Google Sheets: File > Download as > Coma-separated values (.csv, current sheet)
Begin by starting statnet.
library(statnet)
…or, if you prefer, you can just open the sna and
network packages.
library(sna)
library(network)
Now, import the data as a matrix. Make sure that the first column in
the spreadsheet is used as the character names using
row.names=1. If you do not have names included with the
matrix, then omit this.
I am using the matrices that an earlier class developed for the movie Snatch.
setwd("~/Desktop/Snatch")
answer <- read.csv(file.choose(),
row.names = 1,
check.names = FALSE) # "Answers to" network
posneg <- read.csv(file.choose(),
row.names = 1,
check.names = FALSE) # "positive and negative ties" network
work <- read.csv(file.choose(),
row.names = 1,
check.names = FALSE) # "works with" network
The rest of the data import and cleaning will go the same as with
igraph. To check that out again you are welcome to check out that RPub.
But, if everything goes well in the data import, then convert the object you imported into a matrix and (you can do this simultaneously) use that to create a network object.
ans <- as.network(as.matrix(answer))
p_n <- as.network(as.matrix(posneg))
wk <- as.network(as.matrix(work))
If you want to see if it worked, try plotting the networks you just created.
plot(ans, displaylabels=TRUE,
main="Snatch \nAnswers to...")
plot(p_n, displaylabels=TRUE,
main="Snatch \nPostive and negative ties")
plot(wk, displaylabels=TRUE,
main="Snatch \nWorks with...")
If the plots worked, you are ready to roll.
If they didn’t work out as you had hoped, go back and start the troubleshooting.
If you really are experiencing some frustration, then I recommend looking back at the other RPub that introduced how to import movie data (into igraph). There was additional troubleshooting help there.
Good luck!