Useful commands or R Markdown Cheat Sheet

Manuel S. Gonzalez Canche (2024) state of the art in 2009

Rowling (2019) said this

Manuel Sacramento Gonzalez Canche (2014)

(mason2021internationality?) etc

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.

González Canché (2017) 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)
## 
## 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 A better approach to add tables

knitr::kable(head(dtm2),
caption = "Table with kable")
Table with kable
Actor Department RUser Year
Manuel HEd Med 2
Manuel IEDP Med 2
Abigail EdPol New 3
Marissa IEDP New 1
Alexander Sociology Med 2
Ian IEDP New 1

1.2 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 a10cb72 DN-- 28 22 -- 
## + attr: name (v/c), Year (e/n)
## + edges from a10cb72 (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))
## Warning: `get.adjacency()` was deprecated in igraph 2.0.0.
## ℹ Please use `as_adjacency_matrix()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
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.0.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")
## Warning: `graph.adjacency()` was deprecated in igraph 2.0.0.
## ℹ Please use `graph_from_adjacency_matrix()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(g1)

g1edgelist<-get.edgelist(g1)
## Warning: `get.edgelist()` was deprecated in igraph 2.0.0.
## ℹ Please use `as_edgelist()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
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.1 What is the meaning of the diagonal for point II?

Your answer

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

Your answer

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

Your answer

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

Your answer

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

Your answer

References

Canche, Manuel S Gonzalez. 2024. “Graphical Retrieval and Analysis of Temporal Information Systems (GRATIS): An Integrative Mixed Methodology and Open-Access Software to Analyze the (Non-) Linear Chronological Evolution of Information Embedded in Textual/Qualitative Data.” Journal of Mixed Methods Research 18 (1): 71–103.
Canche, Manuel Sacramento Gonzalez. 2014. “Localized Competition in the Non-Resident Student Market.” Economics of Education Review 43: 21–35.
González Canché, Manuel S. 2017. “The Heterogeneous Non-Resident Student Body: Measuring the Effect of Out-of-State Students’ Home-State Wealth on Tuition and Fee Price Variations.” Research in Higher Education 58 (2): 141–83.
Rowling, JK. 2019. “Harry Potter.” The 100 Greatest Literary Characters, 183.