8.1
Load libraries.
library(igraph)
library(tidyverse)
We begin by reading in the data from the GSS network module 2004.
<-"https://raw.githubusercontent.com/mahoffman/stanford_networks/main/data/gss_local_nets.csv"
gss_url <- read_csv(gss_url) gss
## Rows: 1426 Columns: 41
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): sex, race, partyid, relig, educ2, educ3, educ4, educ5, relig1, rel...
## dbl (28): age, numgiven, close12, close13, close14, close15, close23, close2...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Let’s have a broad overview of the data
::vis_dat(gss,sort_type = FALSE)+
visdatlabs(title = "GSS data",
subtitle = glue::glue("#rows = {nrow(gss)}, #columns = {ncol(gss)} "))
The first five concern the attributes of a given respondent:
sex
age
race
partyid
religion.
<- 5
atrr_n ::vis_dat(gss[,1:atrr_n],sort_type = FALSE)+
visdatlabs(title = "GSS data: attributes of a given respondent",
subtitle = glue::glue("#rows = {nrow(gss[,1:atrr_n])}, #columns = {ncol(gss[,1:atrr_n])} "))
The basic idea of the module was to ask people about up to five others with whom they discussed “important matters” in the past six months. The respondents reported the number of people whom they discussed “important matters”:
numgiven: the number of others whom they repondents discussed important matters with.
“close” columns: The relationship between others (e.g., close12 is the closeness of person 1 to person 2, for each respondent).
“sex, race, age” columns: attributes of each of the others (n=5) in the ego network. (3*5)
<- 41
net_n ::vis_dat(gss[,(atrr_n+1):net_n],sort_type = FALSE)+
visdatlabs(title = "GSS data: ''netwok' part",
subtitle = glue::glue("#rows = {nrow(gss[,(atrr_n+1):net_n])}, #columns = {ncol(gss[,(atrr_n+1):net_n])} "))
To do so, we have to first turn the variables close12 through close45 into an edge list, one for each respondent.
<- gss %>%
ties ::select(starts_with("close"))
dplyrhead(ties)
## # A tibble: 6 × 10
## close12 close13 close14 close15 close23 close24 close25 close34 close35
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NA NA NA NA NA NA NA NA NA
## 2 NA NA NA NA NA NA NA NA NA
## 3 1 2 0 NA 2 2 NA 1 NA
## 4 2 0 2 NA 2 2 NA 2 NA
## 5 NA NA NA NA NA NA NA NA NA
## 6 0 2 1 1 1 1 1 2 2
## # ℹ 1 more variable: close45 <dbl>
A function, which uses the code above to turn any row in the ties data set into an ego network, and then apply that function to every row in the data
<- function(tie){
make_ego_nets # make the matrix
= matrix(nrow = 5, ncol = 5)
mat # assign the tie values to the lower triangle
lower.tri(mat)] <- as.numeric(tie)
mat[# symmetrize
upper.tri(mat)] = t(mat)[upper.tri(mat)]
mat[# identify missing values
<- is.na(mat)
na_vals # identify rows where all values are missing
<- rowSums(na_vals) < nrow(mat)
non_missing_rows
# if any rows
if(sum(!non_missing_rows) > 0){
<- mat[non_missing_rows,non_missing_rows]
mat
}diag(mat) <- 0
<- graph.adjacency(mat, mode = "undirected", weighted = T)
ego_net return(ego_net)
}
A simpler approach
<- function(tie){
make_ego_nets_simple #get the all possible links among others
<- tie %>% unlist
tie #remove missing links
<- tie[!is.na(tie)]
tie #remove zero links
<- tie[tie!=0]
tie #get the identity of linked pairs
<- str_extract(names(tie), "[0-9]+")
others #split the linked others
<- str_split(others, "",simplify = TRUE) %>% as.data.frame
others_link #make edge list of others
<- cbind(others_link, tie)
others_link #ego graph with
graph_from_data_frame(others_link,
directed=FALSE)
}
Clean ties before creating the networks
#set zero links as missing links
==0] <- NA
ties[ties#repondents where all links among others are missing
<- rowSums(is.na(ties))==ncol(ties)
others_missing
#remove any respondent that falls in any of the above
<- ties[!(others_missing),] ties
Compare the two functions
<- apply(ties,1,make_ego_nets)
ego_nets <- apply(ties,1,make_ego_nets_simple)
ego_nets_simple
plot(ego_nets[[1]])
plot(ego_nets_simple[[1]])
Not the same ! Where did thing go wrong?
1,] ties[
## # A tibble: 1 × 10
## close12 close13 close14 close15 close23 close24 close25 close34 close35
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 2 NA NA 2 2 NA 1 NA
## # ℹ 1 more variable: close45 <dbl>