Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (105 sloc) 4.38 KB
#Set working directory
setwd("~/SIENA_Workshop/Exercise")
#Open RSIENA
library(RSiena)
#Create dataframes
#Edgelists over-time (e.g. 3 waves)
A1 <- read.csv("A1file.csv", header=F)
A2 <- read.csv("A2file.csv", header=F)
A3 <- read.csv("A3file.csv", header=F)
#Covariates
cv1 <- as.matrix(read.csv("CVfile1.csv", header=T))
cv2 <- as.matrix(read.csv("CVfile2.csv", header=T))
cv3 <- as.matrix(read.csv("CVfile3.csv", header=T))
##Treating Missing Values
cv1[cv1 %in% c(99999)] <- NA
cv2[cv2 %in% c(99999)] <- NA
cv3[cv3 %in% c(99999)] <- NA
##check the dataset by
table(cv1, useNA = 'always')
table(cv2, useNA = 'always')
table(cv3, useNA = 'always')
#check out the dataframes
dim() #dimension
header() #first 10 of the data
# Transform edgelist into matrix
## Matrix 1
A1.copy <- A1[, 1:3]
A1.copy <- as.matrix(A1.copy)
### create empty squared adjacency matrix (0, # of nodes, # of nodes)
adj1 <- matrix(0, 317, 317)
### put edge values in desired places
adj1[A1.copy[, 1:2]] <- A1.copy[, 3]
##ALS Matrix 2
A2.copy <- A2[, 1:3]
A2.copy <- as.matrix(A2.copy)
### create empty squared adjacency matrix (0, # of nodes, # of nodes)
adj2 <- matrix(0, 317, 317)
### put edge values in desired places
adj2[A2.copy[, 1:2]] <- A2.copy[, 3]
##ALS Matrix 3
A3.copy <- A3[, 1:3]
A3.copy <- as.matrix(A3.copy)
### create empty squared adjacency matrix (0, # of nodes, # of nodes)
adj3 <- matrix(0, 317, 317)
### put edge values in desired places
adj3[A3.copy[, 1:2]] <- A3.copy[, 3]
#Create Network Dependent Variable (NetDV) Object (sienaDependent)
NetDV <- sienaDependent(array(c(adj1, adj2, adj3), dim = c(317,317,3)))
dim(NetDV)
attributes(NetDV)
#Independent Variables
##coCovar: Constant actor covariates
CV1 <- coCovar(cv1[,1])
CV2 <- coCovar(cv2[,1])
CV3 <- coCovar(cv3[,1])
#Composition Change: Space delimited text format (" .prn")
##each line per actor,
## each line a series of space delimited numbers indicating intervals.
compChange <- sienaCompositionChangeFromFile("CompositionChangeFile.prn")
# Now, combine the dependent and independent variable objects.
## The function sienaDataCreate creates a Siena Data Object from
## network, covariates and composition change objects;
## The objects that earlier were created by sienaDependent will have the role
## of dependent variables. Similarly the other roles are predetermined
## by previously using the functions coCovar, varCovar,
# coDyadCovar, varDyadCovar, and sienaCompositionChange.
myData<- sienaDataCreate( NetDV, CV1, CV2, CV3, compChange)
#Initial output
myEff <- getEffects(myData)
print01Report(myData, myEff, modelname = "init_output")
names(myEff)
#Search the all possible effects
effectsDocumentation(myEff)
#Add basic structural effect
myEff <- includeEffects( myEff, egoX, interaction1="CV1")
##Ego effect: RQ: which covariate makes a person more likely to create a tie?
myEff <- includeEffects( myEff, egoX, interaction1="CV1")
myEff <- includeEffects( myEff, egoX, interaction1="CV2")
myEff <- includeEffects( myEff, egoX, interaction1="CV3")
##Homophily: Is "homophily" a dynamic mechanism for tie change?
myEff <- includeEffects( myEff, sameX, interaction1="CV1")
myEff <- includeEffects( myEff, sameX, interaction1="CV2")
myEff <- includeEffects( myEff, sameX, interaction1="CV3")
#Check
myEff$include
#see what's included in my effect specification
myEff
# Define the algorithm settings.
##Estimattion function siena07 (Can take some time)
myAlgo <- sienaAlgorithmCreate( projname = 'Example' )
estimateExample <- siena07(myAlgo , data = myData, effect = myEff)
##Show the brief summary of the results by typing the name of sienaFit object
estimateExample
summary(estimateExample)
#To try better convergence, I will first see if the removal of reciprocity makes the model better
##Remove the recip effect, and also remove the predefined myALSAlgo and estimateALS
myEff <- includeEffects( myEff, recip, include=FALSE)
rm(myAlgo)
rm(estimateExample)
##re-create myAlgo and estimateExample
myAlgo <- sienaAlgorithmCreate( projname = 'estimateExample' )
estimateExample <- siena07(myAlgo , data = myData, effect = myEff)
##Still bad convergence: Rerun the model by adding up the previous results.
estimateExample2 <- siena07(myAlgo , data = myData, effect = myEff, prevAns = estimateExample)
summary(estimateExample2)
You can’t perform that action at this time.