Useful commands or R Markdown Cheat Sheet

Equation for transformation $edgelist - graph - matrix $

González Canché (2023) said this

Canché et al. (2025) said that

1 Deliverable 1

1.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

getwd()
## [1] "/Users/msgc-admin/Library/CloudStorage/Dropbox-PennGSE/Manuel S. Gonzalez Canche/Blessing PHUDCFILY/Courses_PHUDCFILY/SSNA_PHUDCFILY/Deliverables"
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
## Warning: `graph.data.frame()` was deprecated in igraph 2.0.0.
## ℹ Please use `graph_from_data_frame()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
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
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      : num  2 2 3 1 2 1 1 1 2 1 ...
dim(dtm2)
## [1] 22  4

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 91bf64e DN-- 28 22 -- 
## + attr: name (v/c), Year (e/n)
## + edges from 91bf64e (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.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.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

References

Canché, Manuel S González, Kaiwen Zheng, Yantao Song, and Yunhao Liang. 2025. “Standardized Testing for Diverse Talent Identification: A Framework to Address Geographical Bias in Standardized Testing and Increase Diversity in College Admissions in the Post-Affirmative Action/Race-Neutral Admissions Era.” Research in Higher Education 66 (2): 13.
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.
González Canché, Manuel S. 2023. Spatial Socio-Econometric Modeling (SSEM): A Low-Code Toolkit for Spatial Data Science and Interactive Visualizations Using r. Springer Nature. https://link.springer.com/book/10.1007/978-3-031-24857-3.