Useful commands or R Markdown Cheat Sheet

@rowling2019harry said this

@canche2014localized

1 Create and describe your own two-mode edgelist with at least 15 human actors following a two-mode network describe the elements

The following edgelist represents a set of relationships among participants of the Statistical Network Analysis class ascribed to their academic departments. These relationships are stored in the first and second columns. The third column is an attribute of the human actors and the fourth column is an attribute of the relationships recoredd in columns 1 and 2. The following code serves to create this dataset.

@canche2017heterogeneous said this blah blah

dtm2<- read.csv("https://raw.githubusercontent.com/msgc/mapping/master/dtm2.csv")
str(dtm2)
## 'data.frame':    22 obs. of  4 variables:
##  $ Actor     : chr  "Manuel" "Manuel" "Abigail" "Marissa" ...
##  $ Department: chr  "HEd" "IEDP" "EdPol" "IEDP" ...
##  $ RUser     : chr  "Med" "Med" "New" "New" ...
##  $ Year      : int  2 2 3 1 2 1 1 1 2 1 ...
#Loading one of our main packages
# install.packages("igraph")
library(igraph)
## Warning: package 'igraph' was built under R version 4.1.3
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
# dtm2[,1:2] reads our relationships
g<-graph.data.frame(dtm2[ ,c(1,2,4)]) #This will read the first two columns as relationships and the third an edge attribute

names(dtm2)
## [1] "Actor"      "Department" "RUser"      "Year"
g<-graph.data.frame(dtm2[,c("Actor","Department","Year")]) #Same as line 23 but with column names
plot(g)

#Creating our dataset within R
#dtm2<- read.csv("G:/My Drive/SSNA 2017 Penn/SSNA 2018/Deliverables/dtm2.csv")
dtm2<-data.frame(Actor=c("Manuel","Manuel","Abigail","Marissa","Alexander","Ian", "Magali","Giovi","Matt","Emily","Michael","SoYeong","Alexis","Nikita","Fengyin","Liu", "Jimin", "Zhiyuan","Siyu" ,"Qing", "Kate", "Manuel"),#write it in quotation marks separated by commas
                 Department=c("HEd","IEDP", "EdPol","IEDP","Sociology","IEDP","IEDP","IEDP","IEDP","QM","QM","IEDP", "TLL","BDS","QM","QM","QM","QM","QM" ,"QM", "TLTE", "QM"),# 
                 RUser=c("Med","Med","New","New","Med","New","New","New","New", "Med","Med","New","New","New","New","NEW","Med","New","New","New", "Med", "Med"), #Med, New, Adv
                 Year=c(2,2,3,1,2,1,1,1,2,1,2,1,1,1,2,2,3,2,2,2,2,2)) #Actual numbers no quotations
# Looking at the first six elements of our dataset
head(dtm2)
##       Actor Department RUser Year
## 1    Manuel        HEd   Med    2
## 2    Manuel       IEDP   Med    2
## 3   Abigail      EdPol   New    3
## 4   Marissa       IEDP   New    1
## 5 Alexander  Sociology   Med    2
## 6       Ian       IEDP   New    1

1.1 Read your edgelist as a network and create a sociogram

#install.packages("igraph")
library(igraph)
# dtm2[,1:2] reads our relationships
g<-graph.data.frame(dtm2[,c(1,2,4)]) #This will read the first two columns as relationships, you should probably expand more on this given our class discussion.
plot(g)

2 II. UPDATE transformation using code retaining human actors (column 1 of original edgelist)

As a preamble I tabulate the unique number of human and non-human actors

table(duplicated(dtm2$Actor))#I am interested in the first element
## 
## FALSE  TRUE 
##    20     2
table(duplicated(dtm2$Department)) #The first element plus the first element of the previous line add the total number of actors
## 
## FALSE  TRUE 
##     8    14
g#prints the graph information that corroborates the number of actors. What is the meaning of 22 here? 
## IGRAPH 272f308 DN-- 28 22 -- 
## + attr: name (v/c), Year (e/n)
## + edges from 272f308 (vertex names):
##  [1] Manuel   ->HEd       Manuel   ->IEDP      Abigail  ->EdPol    
##  [4] Marissa  ->IEDP      Alexander->Sociology Ian      ->IEDP     
##  [7] Magali   ->IEDP      Giovi    ->IEDP      Matt     ->IEDP     
## [10] Emily    ->QM        Michael  ->QM        SoYeong  ->IEDP     
## [13] Alexis   ->TLL       Nikita   ->BDS       Fengyin  ->QM       
## [16] Liu      ->QM        Jimin    ->QM        Zhiyuan  ->QM       
## [19] Siyu     ->QM        Qing     ->QM        Kate     ->TLTE     
## [22] Manuel   ->QM
v<-as.matrix(get.adjacency(g))
dim(v)
## [1] 28 28
v<-v[1:table(duplicated(dtm2$Actor))[1],(table(duplicated(dtm2$Actor))[1]+1):ncol(v)]#length(V(g)$name)
dim(v)#what does this mean? You should probably expand on this.
## [1] 20  8

Now we are ready to dothe II point

v1<-v%*%t(v) #Actor relationships given their common ascription, probably expand more on this
dim(v1)
## [1] 20 20

To do the III point3.

v2<-t(v)%*%v #Community relationships, probably expand more on this
dim(v2)
## [1] 8 8

2.1 Questions

After executing the following code address the following questions:

nofcomm<-diag(v1)
nofcomm#keep or save that information
##    Manuel   Abigail   Marissa Alexander       Ian    Magali     Giovi      Matt 
##         3         1         1         1         1         1         1         1 
##     Emily   Michael   SoYeong    Alexis    Nikita   Fengyin       Liu     Jimin 
##         1         1         1         1         1         1         1         1 
##   Zhiyuan      Siyu      Qing      Kate 
##         1         1         1         1
nofactors<-diag(v2)
nofactors
##       HEd      IEDP     EdPol Sociology        QM       TLL       BDS      TLTE 
##         1         7         1         1         9         1         1         1
diag(v1)<-0
g1<-graph.adjacency(v1, mode = "undirected")
plot(g1)

g1edgelist<-get.edgelist(g1)
head(g1edgelist)
##      [,1]     [,2]     
## [1,] "Manuel" "Marissa"
## [2,] "Manuel" "Ian"    
## [3,] "Manuel" "Magali" 
## [4,] "Manuel" "Giovi"  
## [5,] "Manuel" "Matt"   
## [6,] "Manuel" "Emily"
diag(v2)<-0
g2<-graph.adjacency(v2, mode = "undirected")
plot(g2)

g2edgelist<-get.edgelist(g2)
head(g2edgelist)
##      [,1]   [,2]  
## [1,] "HEd"  "IEDP"
## [2,] "HEd"  "QM"  
## [3,] "IEDP" "QM"

2.2 What is the meaning of the diagonal for point II?

Your answer

2.3 What is the meaning of the diagonal for point III?

Your answer

2.4 What would have happened if you did not added zeroes to the diagonals in points II and III?

Your answer

2.5 Explain the meaning of the sociograms resulting from points II and III.

Your answer

2.6 Why did you have to add the option undirected in the code?

Your answer

3 References