Loading, setting up

tmc <- readRDS("tm-c.rds")
tme <- readRDS("tm-e.rds")

library(lme4)
library(igraph)
library(tidyverse)
library(ergm)

Conversing model

s <- tmc %>% 
    select(sender, sender_profile, sender_n_tweets)

r <- tmc %>% 
    select(receiver, receiver_profile, receiver_n_tweets)

names(r) <- names(s)

vertices_c <- bind_rows(s, r) %>% 
    distinct() %>% 
    filter(!is.na(sender_profile))

names(vertices_c) <- c("participant", "profile_code", "n_tweets")

edges_c <- tmc %>% 
    select(sender, receiver, tie, weight_l) %>% 
    filter(tie == 1)

g <- graph_from_data_frame(edges_c, vertices = vertices_c)
V(g)$n_tweets <- scale(V(g)$n_tweets)
g <- igraph::simplify(g, remove.loops=T)
gg <- intergraph::asNetwork(g)

model1 <- ergm(gg ~ edges + mutual + nodecov("n_tweets") + nodemix("profile_code", base = 25),
               control = control.ergm(MCMC.burnin=50000, MCMC.interval=5000, MCMLE.maxit=30)) # unclear/unclear

summary(model1)
## 
## ==========================
## Summary of model fit
## ==========================
## 
## Formula:   gg ~ edges + mutual + nodecov("n_tweets") + nodemix("profile_code", 
##     base = 25)
## 
## Iterations:  6 out of 30 
## 
## Monte Carlo MLE Results:
##                                                   Estimate Std. Error
## edges                                            -5.184600   0.247888
## mutual                                            3.959900   0.056389
## nodecov.n_tweets                                  0.313171   0.004341
## mix.profile_code.Administration.Administration    0.895671   0.254624
## mix.profile_code.Other.Administration             0.932937   0.268038
## mix.profile_code.Policy/Research.Administration   1.079581   0.264026
## mix.profile_code.Teacher.Administration           0.749287   0.254627
## mix.profile_code.Unclear.Administration           0.714088   0.282336
## mix.profile_code.Administration.Other             0.478922   0.274329
## mix.profile_code.Other.Other                      0.467628   0.283149
## mix.profile_code.Policy/Research.Other            1.048855   0.270811
## mix.profile_code.Teacher.Other                    0.425297   0.258580
## mix.profile_code.Unclear.Other                    0.153155   0.335300
## mix.profile_code.Administration.Policy/Research   0.390549   0.272812
## mix.profile_code.Other.Policy/Research            0.404394   0.283248
## mix.profile_code.Policy/Research.Policy/Research  0.791467   0.267401
## mix.profile_code.Teacher.Policy/Research          0.215017   0.260940
## mix.profile_code.Unclear.Policy/Research          0.359864   0.299270
## mix.profile_code.Administration.Teacher           0.641437   0.256569
## mix.profile_code.Other.Teacher                    0.709844   0.261554
## mix.profile_code.Policy/Research.Teacher          0.739890   0.257393
## mix.profile_code.Teacher.Teacher                  0.638359   0.248299
## mix.profile_code.Unclear.Teacher                  0.588830   0.262507
## mix.profile_code.Administration.Unclear          -0.473839   0.309148
## mix.profile_code.Other.Unclear                    0.086665   0.323034
## mix.profile_code.Policy/Research.Unclear          0.058613   0.313227
## mix.profile_code.Teacher.Unclear                  0.052929   0.273030
##                                                  MCMC %  p-value    
## edges                                                 0  < 1e-04 ***
## mutual                                                0  < 1e-04 ***
## nodecov.n_tweets                                      1  < 1e-04 ***
## mix.profile_code.Administration.Administration        0 0.000436 ***
## mix.profile_code.Other.Administration                 0 0.000500 ***
## mix.profile_code.Policy/Research.Administration       0  < 1e-04 ***
## mix.profile_code.Teacher.Administration               0 0.003254 ** 
## mix.profile_code.Unclear.Administration               0 0.011433 *  
## mix.profile_code.Administration.Other                 0 0.080847 .  
## mix.profile_code.Other.Other                          0 0.098633 .  
## mix.profile_code.Policy/Research.Other                0 0.000108 ***
## mix.profile_code.Teacher.Other                        0 0.100024    
## mix.profile_code.Unclear.Other                        0 0.647836    
## mix.profile_code.Administration.Policy/Research       0 0.152269    
## mix.profile_code.Other.Policy/Research                0 0.153378    
## mix.profile_code.Policy/Research.Policy/Research      0 0.003078 ** 
## mix.profile_code.Teacher.Policy/Research              0 0.409935    
## mix.profile_code.Unclear.Policy/Research              0 0.229182    
## mix.profile_code.Administration.Teacher               0 0.012418 *  
## mix.profile_code.Other.Teacher                        0 0.006649 ** 
## mix.profile_code.Policy/Research.Teacher              0 0.004046 ** 
## mix.profile_code.Teacher.Teacher                      0 0.010143 *  
## mix.profile_code.Unclear.Teacher                      0 0.024891 *  
## mix.profile_code.Administration.Unclear               0 0.125345    
## mix.profile_code.Other.Unclear                        0 0.788481    
## mix.profile_code.Policy/Research.Unclear              0 0.851563    
## mix.profile_code.Teacher.Unclear                      0 0.846287    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##      Null Deviance: 329461  on 237656  degrees of freedom
##  Residual Deviance:  35766  on 237629  degrees of freedom
##  
## AIC: 35820    BIC: 36100    (Smaller is better.)
d <- plogis(coef(model1)[['edges']] + coef(model1)[['mutual']] + coef(model1)[['nodecov.n_tweets']] + coef(model1)[4:27]) %>% as.data.frame() %>% 
    rownames_to_column("var") %>% 
    mutate(var = str_sub(var, start = 18))

b <- broom::tidy(model1) %>% slice(4:27) %>% filter(p.value < .05) %>% mutate(term = str_sub(term, start = 18)) %>% dplyr::select(term, p.value) %>% arrange(p.value) %>% mutate(char = "*")
names(d) <- c('term', 'probability')

d %>% 
    left_join(b) %>% 
    ggplot(aes(x = reorder(term, probability), y = probability, label = char)) +
    geom_col() +
    geom_text(color = "red", size = 5) +
    coord_flip() +
    theme_bw() +
    labs(title = "Probability of individuals 'conversing' (replying to or mentioning the other) on #NGSSchat",
         caption = "Groups with effects that significantly differ from those in the unclear group conversing are denoted with a red asterisk.") +
    theme(text = element_text(family = "Times")) +
    xlab(NULL) + 
    ylab("Probability")

Endorsing model

s <- tme %>% 
    select(sender, sender_profile, sender_n_tweets)

r <- tme %>% 
    select(receiver, receiver_profile, receiver_n_tweets)

names(r) <- names(s)

vertices_c <- bind_rows(s, r) %>% 
    distinct() %>% 
    filter(!is.na(sender_profile))

names(vertices_c) <- c("participant", "profile_code", "n_tweets")

edges_c <- tme %>% 
    select(sender, receiver, tie) %>% 
    filter(tie == 1)

g <- graph_from_data_frame(edges_c, vertices = vertices_c)
V(g)$n_tweets <- scale(V(g)$n_tweets)
g <- igraph::simplify(g, remove.loops=T)
gg <- intergraph::asNetwork(g)

model1 <- ergm(gg ~ edges + mutual + nodecov("n_tweets") + nodemix("profile_code", base = 25),
               control = control.ergm(MCMC.burnin=50000, MCMC.interval=5000, MCMLE.maxit=20))

summary(model1)
## 
## ==========================
## Summary of model fit
## ==========================
## 
## Formula:   gg ~ edges + mutual + nodecov("n_tweets") + nodemix("profile_code", 
##     base = 25)
## 
## Iterations:  8 out of 20 
## 
## Monte Carlo MLE Results:
##                                                   Estimate Std. Error
## edges                                            -4.317123   0.161282
## mutual                                            2.760495   0.039200
## nodecov.n_tweets                                  0.298927   0.004408
## mix.profile_code.Administration.Administration    0.820694   0.164170
## mix.profile_code.Other.Administration             0.658389   0.179474
## mix.profile_code.Policy/Research.Administration   0.701413   0.173678
## mix.profile_code.Teacher.Administration           0.765018   0.165059
## mix.profile_code.Unclear.Administration           0.360331   0.191984
## mix.profile_code.Administration.Other             0.767197   0.175290
## mix.profile_code.Other.Other                      0.483380   0.186299
## mix.profile_code.Policy/Research.Other            0.735126   0.174324
## mix.profile_code.Teacher.Other                    0.680445   0.166009
## mix.profile_code.Unclear.Other                    0.277491   0.206567
## mix.profile_code.Administration.Policy/Research   0.822523   0.175781
## mix.profile_code.Other.Policy/Research            0.690024   0.184777
## mix.profile_code.Policy/Research.Policy/Research  0.748822   0.168633
## mix.profile_code.Teacher.Policy/Research          0.769509   0.167902
## mix.profile_code.Unclear.Policy/Research          0.510383   0.198943
## mix.profile_code.Administration.Teacher           0.617942   0.167860
## mix.profile_code.Other.Teacher                    0.586895   0.170250
## mix.profile_code.Policy/Research.Teacher          0.349516   0.167627
## mix.profile_code.Teacher.Teacher                  0.668027   0.161622
## mix.profile_code.Unclear.Teacher                  0.422050   0.170723
## mix.profile_code.Administration.Unclear           0.121134   0.193555
## mix.profile_code.Other.Unclear                    0.319310   0.200854
## mix.profile_code.Policy/Research.Unclear          0.097968   0.196849
## mix.profile_code.Teacher.Unclear                  0.276962   0.179725
##                                                  MCMC %  p-value    
## edges                                                 0  < 1e-04 ***
## mutual                                                0  < 1e-04 ***
## nodecov.n_tweets                                      1  < 1e-04 ***
## mix.profile_code.Administration.Administration        0  < 1e-04 ***
## mix.profile_code.Other.Administration                 0 0.000244 ***
## mix.profile_code.Policy/Research.Administration       0  < 1e-04 ***
## mix.profile_code.Teacher.Administration               0  < 1e-04 ***
## mix.profile_code.Unclear.Administration               0 0.060536 .  
## mix.profile_code.Administration.Other                 0  < 1e-04 ***
## mix.profile_code.Other.Other                          0 0.009469 ** 
## mix.profile_code.Policy/Research.Other                0  < 1e-04 ***
## mix.profile_code.Teacher.Other                        0  < 1e-04 ***
## mix.profile_code.Unclear.Other                        0 0.179161    
## mix.profile_code.Administration.Policy/Research       0  < 1e-04 ***
## mix.profile_code.Other.Policy/Research                0 0.000188 ***
## mix.profile_code.Policy/Research.Policy/Research      0  < 1e-04 ***
## mix.profile_code.Teacher.Policy/Research              0  < 1e-04 ***
## mix.profile_code.Unclear.Policy/Research              0 0.010304 *  
## mix.profile_code.Administration.Teacher               0 0.000232 ***
## mix.profile_code.Other.Teacher                        0 0.000566 ***
## mix.profile_code.Policy/Research.Teacher              0 0.037063 *  
## mix.profile_code.Teacher.Teacher                      0  < 1e-04 ***
## mix.profile_code.Unclear.Teacher                      0 0.013432 *  
## mix.profile_code.Administration.Unclear               0 0.531421    
## mix.profile_code.Other.Unclear                        0 0.111889    
## mix.profile_code.Policy/Research.Unclear              0 0.618708    
## mix.profile_code.Teacher.Unclear                      0 0.123310    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##      Null Deviance: 341738  on 246512  degrees of freedom
##  Residual Deviance:  67815  on 246485  degrees of freedom
##  
## AIC: 67869    BIC: 68150    (Smaller is better.)
d <- plogis(coef(model1)[['edges']] + coef(model1)[['mutual']] + coef(model1)[['nodecov.n_tweets']] + coef(model1)[4:27]) %>% as.data.frame() %>% 
    rownames_to_column("var") %>% 
    mutate(var = str_sub(var, start = 18))

b <- broom::tidy(model1) %>% slice(4:27) %>% filter(p.value < .05) %>% mutate(term = str_sub(term, start = 18)) %>% dplyr::select(term, p.value) %>% arrange(p.value) %>% mutate(char = "*")
names(d) <- c('term', 'probability')

d %>% 
    left_join(b) %>% 
    # bind_rows(data.frame())
    ggplot(aes(x = reorder(term, probability), y = probability, label = char)) +
    geom_col() +
    geom_text(color = "red", size = 5) +
    coord_flip() +
    theme_bw() +
    labs(title = "Probability of individuals 'endorsing' (favoriting or retweeting the other) on #NGSSchat",
         caption = "Groups with effects that significantly differ from those in the unclear group conversing are denoted with a red asterisk. With mean number of tweets sent to the chat.") +
    theme(text = element_text(family = "Times")) +
    xlab(NULL) + 
    ylab("Probability")