-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.2_Results_null.R
145 lines (120 loc) · 6.43 KB
/
3.2_Results_null.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
########################################################################################################################
## GOSSIP IN HUNGARIAN HIGH SCHOOLS
## Display of results (3.2)
## R script written by Jose Luis Estevez (Linkoping University)
## Date: September 2nd, 2021
########################################################################################################################
# R PACKAGES REQUIRED
library(ggplot2)
########################################################################################################################
# BERGM RESULTS
rm(list=ls())
load("bergm_results_null.RData")
rm(list=setdiff(ls(), c('bergm_results')))
########################################################################################################################
# Extraction of BERGM information (all posteriors)
bergm_info <- vector('list',length(bergm_results))
names(bergm_info) <- names(bergm_results)
for(wave in seq_along(bergm_results)){
for(room in seq_along(bergm_results[[wave]])){
bergm_info[[wave]][[room]] <- as.data.frame(matrix(NA,nrow=nrow(bergm_results[[wave]][[room]]$Theta),
ncol=ncol(bergm_results[[wave]][[room]]$Theta)))
names(bergm_info[[wave]][[room]]) <- names(bergm_results[[wave]][[room]]$ess)
bergm_info[[wave]][[room]]$classroom <- paste(names(bergm_results[[wave]])[[room]],' (time ',wave,')',sep='')
for(i in 1:ncol(bergm_results[[wave]][[room]]$Theta)){
bergm_info[[wave]][[room]][,i] <- bergm_results[[wave]][[room]]$Theta[,i]
}
}
names(bergm_info[[wave]]) <- names(bergm_results[[wave]])
}
# For special model specfications
bergm_info[[1]][['7100']]$mutual <- NA
bergm_info[[2]][['5400']]$mutual <- NA
bergm_info[[2]][['6300']]$mutual <- NA
bergm_info[[2]][['7600']]$odegree0 <- NA
bergm_info[[3]][['2100']]$odegree0 <- NA
# Creation of a single dataset containing all the posteriors
for(wave in seq_along(bergm_info)){
for(room in seq_along(bergm_info[[wave]])){
bergm_info[[wave]][[room]] <- bergm_info[[wave]][[room]][,order(names(bergm_info[[wave]][[room]]),decreasing=FALSE)]
names(bergm_info[[wave]][[room]]) <- c('classroom','dislike','2nd_dislike',
'shared_dislike','edges','gwdsp','gwesp',
'pop_spread','act_spread','mutual','sinks')
}
bergm_info[[wave]] <- do.call('rbind',bergm_info[[wave]])
}
bergm_info <- do.call('rbind',bergm_info)
# Conversion into a summary table
# Extraction of the mean of posteriors and the Bayes p
bergm_mean <- matrix(NA,length(unique(bergm_info$classroom)),length(names(bergm_info[-1])))
rownames(bergm_mean) <- unique(bergm_info$classroom)
colnames(bergm_mean) <- names(bergm_info[-1])
bergm_pval <- bergm_mean
for(i in rownames(bergm_mean)){
for(j in colnames(bergm_mean)){
bergm_mean[i,j] <- mean(bergm_info[bergm_info$classroom == i,j])
bergm_pval[i,j] <- ifelse(mean(bergm_info[bergm_info$classroom == i,j])>0,
sum(bergm_info[bergm_info$classroom == i,j]<0)/length(bergm_info[bergm_info$classroom == i,j]),
sum(bergm_info[bergm_info$classroom == i,j]>0)/length(bergm_info[bergm_info$classroom == i,j]))
}
}
# Difference in prob
bergm_diff <- bergm_mean
for(i in rownames(bergm_diff)){
for(j in colnames(bergm_diff)){
if(j == "edges"){
# exp(b_1) / (1 + exp(b_1))
bergm_diff[i,j] <- exp(bergm_diff[i,j])/(1+exp(bergm_diff[i,j]))
}else{
# {exp(b_1+b_x)/(1+ exp(b_1+b_x))} - {exp(b_1)/(1+ exp(b_1))}
bergm_diff[i,j] <- exp(bergm_diff[i,j]+bergm_diff[i,"edges"])/(1+exp(bergm_diff[i,j]+bergm_diff[i,"edges"])) -
exp(bergm_diff[i,"edges"])/(1+exp(bergm_diff[i,"edges"]))
}
}
}
# Data set with all information
bergm_info <- data.frame(predictor=rep(colnames(bergm_mean),each=nrow(bergm_mean)),
unit=rep(rownames(bergm_mean),times=ncol(bergm_mean)),
par=as.vector(bergm_mean),
pval=as.vector(bergm_pval),
difference=as.vector(bergm_diff))
bergm_info$classroom <- substr(bergm_info$unit,1,4)
bergm_info$time <- substr(bergm_info$unit,12,12)
# VISUALISATION OF RESULTS
bergm_info$`Bayesian p-value` <- ifelse(bergm_info$pval < .05,'<.05','>=.05')
set.seed(290691)
bergm_info$time2 <- jitter(as.numeric(bergm_info$time),factor=.1)
bergm_info$predictor <- factor(bergm_info$predictor,
levels=c('edges','mutual','act_spread','pop_spread','sinks','gwdsp','gwesp',
'dislike','shared_dislike','2nd_dislike'))
levels(bergm_info$predictor) <- c('Edges/Density','Mutual','Act. spread','Pop. spread','Sinks','Multiple two-paths',
'GWESP','Direct antipathy','Shared antipathy','Indirect antipathy')
grid.background <- theme_bw()+
theme(plot.background=element_blank(),panel.grid.minor=element_blank(),panel.border=element_blank())+
theme(axis.line=element_line(color='black'))+
theme(strip.text.x=element_text(colour='white',face='bold'))+
theme(strip.background=element_rect(fill='black'))
jpeg(filename='BERGM_results.jpeg',width=11,height=8,units='in',res=500)
ggplot(data=bergm_info)+
geom_hline(yintercept=0,color='blue',alpha=.5)+
geom_line(aes(x=time2,y=difference,group=classroom),alpha=.5,linetype='dashed')+
geom_point(aes(x=time2,y=difference),colour='black',size=3)+
geom_point(aes(x=time2,y=difference,colour=`Bayesian p-value`),size=2,alpha=.9)+
scale_colour_manual(values = c('red','darkgrey'))+
facet_wrap(~predictor,nrow=2,ncol=5)+
xlab('Time')+
ylab('Difference in probability')+
scale_x_continuous(breaks=c(1,2,3))+
grid.background
dev.off()
########################################################################################################################
# TABLE OF RESULTS (APPENDIX)
# Significance levels
bergm_info$sign <- ifelse(bergm_info$pval < .001,'***',
ifelse(bergm_info$pval < .01,'**',
ifelse(bergm_info$pval < .05,'*','')))
bergm_info$par <- round(bergm_info$par,2)
bergm_info$pval <- round(bergm_info$pval,3)
bergm_info <- bergm_info[order(bergm_info$predictor),]
bergm_info <- bergm_info[order(bergm_info$unit),]
write.table(bergm_info,'bergm_results.csv',row.names=FALSE,sep=';')