BenjaminLind

The Big Lebowski and Magnolia network datasets into R

Jul 16th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.02 KB | None | 0 0
  1. #Data collected by Moviegalaxies
  2. #Data distributed using Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
  3. #I've read two of their datasets into Gephi and exported the data into Google Docs
  4. #The code below reads the data from Google Docs and returns iGraph "graph" objects.
  5. #Likewise, I've licensed my own derivative work using Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
  6. #See http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
  7.  
  8. library(RCurl); library(igraph)
  9.  
  10. read.sstab<-function(theurl){
  11.   outtab<-getURL(theurl, ssl.verifypeer=FALSE)
  12.   outtab<-textConnection(outtab)
  13.   outtab<-read.table(outtab, sep="\t", header=TRUE, as.is=TRUE, stringsAsFactors=FALSE, strip.white=TRUE, quote="\"")
  14.   return(outtab)
  15.   }
  16.  
  17. #The Big Lebowski
  18. #http://moviegalaxies.com/movies/download/125/The-Big-Lebowski
  19. bigl.nodes<-read.sstab("https://docs.google.com/spreadsheet/pub?key=0Ai--oOZQWBHSdDYtOEpUZ21FUWUyTEc2eXZSSm93TEE&single=true&gid=0&output=txt")
  20. bigl.edges<-read.sstab("https://docs.google.com/spreadsheet/pub?key=0Ai--oOZQWBHSdDYtOEpUZ21FUWUyTEc2eXZSSm93TEE&single=true&gid=1&output=txt")
  21. #Magnolia
  22. #http://moviegalaxies.com/movies/download/523/Magnolia
  23. mag.nodes<-read.sstab("https://docs.google.com/spreadsheet/pub?key=0Ai--oOZQWBHSdDYtOEpUZ21FUWUyTEc2eXZSSm93TEE&single=true&gid=2&output=txt")
  24. mag.edges<-read.sstab("https://docs.google.com/spreadsheet/pub?key=0Ai--oOZQWBHSdDYtOEpUZ21FUWUyTEc2eXZSSm93TEE&single=true&gid=3&output=txt")
  25.  
  26. #BE WARNED that the Magnolia data contains errors in it. I don't know how MovieGalaxies collected their data.  Presumably they wrote a program that scans movie scripts which contain typos and machines can't differentiate between some similar names.  This dataset was not spot checked.  The errors include:
  27.  #"CLAUDIA" and "CLAUIDA" are listed as two separate nodes. They should be one; the second is a typo.
  28.  #"JIMMY" and "JIMMY GATOR" are the same characters.
  29.  #"YOUNG PHARAMCY KID" and "YOUNG PHARMACY KID" should be the same
  30.  #I can't tell if "OFFICER" refers to a distinct character, one of the numbered officers, or "JIM KURRING"
  31.  #"GET OUT" isn't the name of any character listed in IMDB.
  32.  #There's a "DOC", "DOCTOR", and three characters with the title "DR". "DOC" should be separate from all three of the "DR" characters, however I'm unsure who "DOCTOR" refers to.
  33.  #"DON" and "DONNIE" are the same character.
  34.  #"STANELY" and "STANLEY" are the same character.
  35.  #There's probably other errors in it.
  36.  
  37. #The Big Lebowski's dataset looks clean to me.
  38.  
  39. sstab2igraph<-function(theel, thenodes){
  40.   theel$Source<-thenodes$Label[match(theel$Source, thenodes$Id)]
  41.   theel$Target<-thenodes$Label[match(theel$Target, thenodes$Id)]
  42.   theig<-graph.edgelist(as.matrix(theel[,c("Source", "Target")]), directed=FALSE)
  43.   theig<-set.edge.attribute(theig, "weight", index=E(theig), theel$Weight)
  44.   name2row<-match(thenodes$Label, V(theig)$name)
  45.   #The attributes in _thenodes_ we're interested in start with "Eccentricity" until the end
  46.   startcol<-which(colnames(thenodes)=="Eccentricity")
  47.   for (i in startcol:ncol(thenodes))
  48.     theig<-set.vertex.attribute(graph=theig, name=colnames(thenodes)[i], index=V(theig)[name2row], value=as.numeric(thenodes[,i]))
  49.   return(theig)
  50.   }
  51.  
  52. bigl.ig<-sstab2igraph(bigl.edges, bigl.nodes)
  53. mag.ig<-sstab2igraph(mag.edges, mag.nodes)
  54. rm(read.sstab, bigl.edges, bigl.nodes, mag.edges, mag.nodes, sstab2igraph)
  55. #A nice looking plot of The Big Lebowski:
  56. #plot(bigl.ig, vertex.size=2*log(igraph::degree(bigl.ig)+1)*2, vertex.label.cex=log(igraph::degree(bigl.ig)+1)/3, edge.width=E(bigl.ig)$weight/2, vertex.label.family="sans", vertex.label.font=2, vertex.label.color="lightgrey", vertex.label.dist=.60, vertex.label.degree=4.7, vertex.color=rainbow(1, alpha=.75))
  57. #Degree distribution plot
  58. #par(fg="lightgrey", col.lab="lightgrey", col.axis="lightgrey", col.main="lightgrey", col.sub="lightgrey")
  59. #plot(ecdf(igraph::degree(bigl.ig)), main=expression(italic("The Big Lebowski")), xlab="Degree", ylab="Cumlative Proportion of Nodes", verticals=TRUE)
  60. #dev.off()
Advertisement
Add Comment
Please, Sign In to add comment