############################
library(statnet)
############################
# IMPORT DATA
farmers <- read.table("http://alanphd.com/archivos/Farmers_H.txt",
header = TRUE)
# Table 2. (page 5 in the paper)
farmers
class(farmers) #[1] "data.frame"
farmers[,1:20] # last column is the SES variable
# data.frame to matrix
farmers_mat <- as.matrix(farmers[,1:20])
farmers_mat
class(farmers_mat) #[1] "matrix"
############################
# from matrix to a network object
farmers_net <- network(farmers_mat, directed=TRUE)
class(farmers_net) #[1] "network"
############################
v2<- farmers[,21] # Just extract the column with the attributes "SES"
v2 # [1] 1 2 2 2 2 3 2 1 3 1 3 1 3 3 2 2 3 3 1 2
set.vertex.attribute(farmers_net,"SES",v2)
list.vertex.attributes(farmers_net)
#[1] "na" "SES" "vertex.names"
############################
### Figure 1 (page 7 in the paper)
plot.network(farmers_net, edge.col=4, vertex.cex = (farmers_net %v% 'SES'),
attrname = NULL,displaylabels=FALSE, mode = "fruchtermanreingold",
vertex.col = (farmers_net %v% 'SES'),
main="Hypothetical Social Network,\n 20 Farmers were asked to nominate 5 Alters,\n node's color = Socio Economic Status")
############################
# ERG Model Model 01 Table 4
farmers_net1.01<-ergm(farmers_net~ edges) #Fit the model
farmers_net1.01
#Newton-Raphson iterations: 5
#MLE Coefficients:
# edges
#-1.030
#How to interpret this model? The log-odds of any tie occuring is:
# = -1.030 * change in the number of ties
# = -1.030 * 1 For all the ties, since the addition of any tie to the network
# changes the number of ties by 1!
exp(-1.030)/(1+exp(-1.030)) #[1] 0.2630841
# corresponding Prob. = exp(-1.030)/(1+exp(-1.030)) = 0.2630841 what would you
# expect, since there are 100/((20)*(20-1) ties
farmers_net
# vertices = 20
# total edges = 100
100/((20)*(20-1))
farmers_net1.01$coef
summary(farmers_net1.01)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_net ~ edges
#Newton-Raphson iterations: 5
#Maximum Likelihood Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.0296 0.1165 NA <1e-04 ***
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
#For this model, the pseudolikelihood is the same as the likelihood.
# Null Deviance: 526.792 on 380 degrees of freedom
# Residual Deviance: 438.014 on 379 degrees of freedom
# Deviance: 88.778 on 1 degrees of freedom
#AIC: 440.01 BIC: 443.95
##################################################################
# ERG Model Model 02 Table 4
farmers_net1.02<-ergm(farmers_net~ edges + mutual, verbose=T) #Fit the model
summary(farmers_net1.02)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_net ~ edges + mutual
#Newton-Raphson iterations: 5
#MCMC sample of size 10000
#Monte Carlo MLE Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.5268 0.1091 0.016 <1e-04 ***
#mutual 1.5266 0.2317 NA <1e-04 ***
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
#Warning: The standard errors are suspect due to possible poor convergence.
# Null Deviance: 526.79 on 380 degrees of freedom
# Residual Deviance: 401.39 on 378 degrees of freedom
# Deviance: 125.40 on 2 degrees of freedom
#AIC: 405.39 BIC: 413.27
##########################################################################
# ERG Model Model 03 Table 4
farmers_net1.03<-ergm(farmers_net~ edges + mutual + nodematch("SES"))
summary(farmers_net1.03)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_net ~ edges + mutual + nodematch("SES")
#Newton-Raphson iterations: 5
#MCMC sample of size 10000
#Monte Carlo MLE Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.8573 0.1574 0.011 < 1e-04 ***
#mutual 1.2262 0.3255 0.018 0.000192 ***
#nodematch.SES 1.1388 0.1999 0.004 < 1e-04 ***
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
# Null Deviance: 526.79 on 380 degrees of freedom
# Residual Deviance: 382.62 on 377 degrees of freedom
# Deviance: 144.17 on 3 degrees of freedom
#AIC: 388.62 BIC: 400.44
###############################################################################
###############################################################################
mcmc.diagnostics(farmers_net1.03)
names(farmers_net1.03)
farmers_net1.03.gof<-gof(farmers_net1.03~distance)
farmers_net1.03.gof
# Figure 3
plot(farmers_net1.03.gof)
farmers_net1.02.gof<-gof(farmers_net1.02~distance)
plot(farmers_net1.02.gof)
###############################################################################
###############################################################################
###############################################################################
# Actually, You Don't Need To Go the process of permuting the matrix
# GO to line 141
#I want to permute the "farmers_mat"
x5 <- sample(1:20, 20, replace=F)
#[1] 17 2 1 5 16 13 11 14 4 20 9 10 6 3 8 15 12 19 7 18
# I used Ucinet Data>Permute and input this new order of rows:
# 17 2 1 5 16 13 11 14 4 20 9 10 6 3 8 15 12 19 7 18
# It has to be separated by spaces, the R output is tab separated,
# so I used Notepad++ to replace tabs by spaces
#Then I export the permuted matrix to Excel
# Copy and paste the permuted matrix entries onto the original matrix,
# This is I am keeping the "labels" of the first matrix, and pasting
# the scrambled entries of the permuted matrix
###############################################################################
############################# IMPORT DATA
farmersP = read.table("http://alanphd.com/archivos/Farmers_HP.txt",
header = TRUE)
# Table 3. Matrix 2 a randomly permuted sociomatrix
farmersP
class(farmersP) #[1] "data.frame"
farmersP[,1:20]
############################
# from data.frame to a matrix object
farmers_matP <- as.matrix(farmersP[,1:20])
farmers_matP
class(farmers_matP) #[1] "matrix"
############################
# from matrix to a network object
farmers_netP <- network(farmers_matP, directed=TRUE)
class(farmers_netP) #[1] "network"
############################
#Set vertex attributes
v2<- farmersP[,21] # Just extract the column with the attributes "SES"
v2 # [1] 1 2 2 2 2 3 2 1 3 1 3 1 3 3 2 2 3 3 1 2
set.vertex.attribute(farmers_netP,"SES",v2)
list.vertex.attributes(farmers_netP)
#[1] "na" "SES" "vertex.names"
################################################################################
# Figure 2 (page 7 in the paper)
plot.network(farmers_netP, edge.col=4, vertex.cex = (farmers_netP %v% 'SES'),
attrname = NULL,displaylabels=FALSE, mode = "fruchtermanreingold",
vertex.col = (farmers_netP %v% 'SES'),
main="Hypothetical Social Network,\n 20 Farmers Randomly Nominate 5 Alters,\n node's color = Socio Economic Status")
################################################################################
################################################################################
# Table 5 Model 01
farmers_net1P.01<-ergm(farmers_netP~ edges) #Fit the model
farmers_net1P.01
#Newton-Raphson iterations: 5
#MLE Coefficients:
# edges
#-1.030
summary(farmers_net1P.01)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_netP ~ edges
#Newton-Raphson iterations: 5
#Maximum Likelihood Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.0296 0.1165 NA <1e-04 ***
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
#For this model, the pseudolikelihood is the same as the likelihood.
# Null Deviance: 526.792 on 380 degrees of freedom
# Residual Deviance: 438.014 on 379 degrees of freedom
# Deviance: 88.778 on 1 degrees of freedom
#AIC: 440.01 BIC: 443.95
###############################################################################
# Table 5 Model 02
farmers_net1P.02<-ergm(farmers_netP~ edges + mutual, verbose=F) #Fit the model
summary(farmers_net1P.02)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_netP ~ edges + mutual
#Newton-Raphson iterations: 5
#MCMC sample of size 10000
#Monte Carlo MLE Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.5285 0.1076 0.016 <1e-04 ***
#mutual 1.5244 0.2259 NA <1e-04 ***
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
#Warning: The standard errors are suspect due to possible poor convergence.
# Null Deviance: 526.79 on 380 degrees of freedom
# Residual Deviance: 401.39 on 378 degrees of freedom
# Deviance: 125.41 on 2 degrees of freedom
#AIC: 405.39 BIC: 413.27
###############################################################################
# Table 5 Model 03
farmers_net1P.03<-ergm(farmers_netP~ edges + mutual + nodematch("SES")) #Fit the model
summary(farmers_net1P.03)
#==========================
#Summary of model fit
#==========================
#Formula: farmers_netP ~ edges + mutual + nodematch("SES")
#Newton-Raphson iterations: 5
#MCMC sample of size 10000
#Monte Carlo MLE Results:
# Estimate Std. Error MCMC s.e. p-value
#edges -1.523035 0.158138 0.010 <1e-04 ***
#mutual 1.527629 0.309020 0.018 <1e-04 ***
#nodematch.SES -0.001014 0.189406 0.003 0.996
#---
#Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
# Null Deviance: 526.79 on 380 degrees of freedom
# Residual Deviance: 401.39 on 377 degrees of freedom
# Deviance: 125.41 on 3 degrees of freedom
#AIC: 407.39 BIC: 419.21
################################################################################
#GOF
farmers_net1P.03.gof<-gof(farmers_net1P.03~distance)
# Figure 4
plot(farmers_net1P.03.gof)
farmers_net1P.02.gof<-gof(farmers_net1P.02~distance)
plot(farmers_net1P.02.gof)
################################################################################
farmers_net1.03.gof<-gof(farmers_net1.03~espartners)
# Figure 5
plot(farmers_net1.03.gof)
################################################################################
Applicability of ERG Models: a hypothetical case
Dr. Luis Alan Navarro Navarro
Centro de Estudios en Gobierno y Asuntos Públicos
El Colegio de Sonora
I wrote this 13 page term paper for an ARL930 class back in 2010. The paper was intended to simulate the data that might be generated on a social network survey of the relationships among a group of irrigators. I consider that it is advisable to run your proposed model on simulated data before conducting the survey.
Survey work is expensive and time consuming, especially for social network data. First, it requires you to approach all the members of a group, this usually leads to a long waiting time to contact and approach the interviewees, additionally in the case or irrigation systems, they used to be busy at the same hours, considering a one-hour interview, there is a limited number of interviews that can be conducted in one day. Second, traditional irrigation systems are almost always linked to remote rural places, implying traveling costs.
Thus, revisiting a community for additional data, because actual data did not fit a statistical model, it is not an option. The wise thing to do is imaging the empirical data that might be generated from the survey (questionnaire). Design an appropriate model (based on the literature reviewed) and run scenarios where the null hypothesis is ruled out, how your ideal database looks like?
As the reader will promptly realize, the irrigation system example is synthetic, nonetheless that a group of irrigators (traditional irrigation systems) is a frequently case of study for social dilemmas, self-organization, and institutional solutions.
Abstract
This paper represents an exploratory research analysis intended to find whether an Exponential Random Graph (ERG) model fits a hypothetical social network (HSN. This HSN forms a graph “G” which mathematically is represented by two subsets G {N, E} where “N” is the number of nodes (actors) N {1,2,3, …, n } and E {1,2,3, …, l } is the number of directed ties (arcs). The adjacency matrix (or Sociomatrix 1) of “G” has binary [ 0, 1 ] off-diagonal entries, where X i, j = 1 if there is a tie i → j from actor “i” to actor “j”, and 0 otherwise. Thus, the HSN is a 20x20 non-symmetric sociomatrix, with row marginals = 5 ∀ "i", and main diagonal (X i, i = 0)not defined but set to 0 by convention. Sociomatrix 1 was manually created resembling egocentric data, where actor (farmer) “i” (called “ego”) was asked to name five other farmers (usually called “alters”) with whom s/he would be willing to collaborate and partner up for a hypothetical (bu possible) project related with the improvement and maintenance of the irrigation system’s infrastructure. Framers were randomly assigned into one out of three categories of an ordinal variable which represents farmer’s socioeconomic status (SES), coded [ 1,2,3 ]from low to high SES, then ties were formed to intentionally match farmer’s SES crating thus homophily ties. Sociomatrix 1 was once randomly permuted to form Sociomatrix 2. We test on both Sociomatrices a single null hypothesis Ho: Irrigation System (IS) users’ willingness to cooperate is less likely to occur within individuals sharing a similar SES (no SES heterophily); Ha: IS user’s willingness to cooperate is more likely to occur within individuals sharing a similar SES (SES homophily).
The goodness of fit of the data to the ERG model proposed provided insights for the analytic potentials of a future empirical social network derived from case studies.
Paper
R code




