2021-12-06 00:00:13
This R markdown script logs the policy actor network analyses by year (2017-2020). The analyses are based on two pre-made data frames: (1) df_actor
that includes actor attributes and (2) df
, a 32-column data frame that includes activities only by policy actors.
library(rtweet)
library(igraph)
library(tm)
library(ngram)
library(dplyr)
library(DT)
library(stringr)
library(stopwords)
library(readr)
library(tibble)
library(ngram)
library(ggplot2) # for longitudinal plot
library(scales)
library(tidyr) # separate_rows
library(png) # test for creative shapes in network
library(rasterImage) # test
library(jpeg)
df_actor = read_csv("actor_edit.csv")
names(df_actor)
## [1] "name" "referenced_user_description"
## [3] "referenced_user_followers_count" "referenced_user_verified"
## [5] "value" "congress"
## [7] "position" "type"
df = read_twitter_csv("Twitter Actor Networks.csv") # from original 1036802*32 data frame
2021-12-06 00:00:15
Only 2.35% of total tweets were posted by policy actors. The rest were from the public.
df_actor = unique(df_actor[,c("name","position","congress")])
df = df[df$username%in%df_actor$name,]
#save_as_csv(df,"Twitter Actor Networks.csv")
table(df$type)
##
## quoted replied_to retweeted
## 2006 5774 13745
df$type[which(is.na(df$type)==TRUE)] <- "initial" # how to index NAs
df_pro = df[df$username%in%df_actor$name[df_actor$position=="support"],] # extract only by FROM, not TO
df_con = df[df$username%in%df_actor$name[df_actor$position=="oppose"],]
nrow(df_pro)+nrow(df_con)
## [1] 24369
con_time=as.data.frame(ts_data(df_con, by = "weeks"))
con_time$position = "oppose"
pro_time=as.data.frame(ts_data(df_pro, by = "weeks"))
pro_time$position = "support"
timetable = data.frame(rbind(pro_time, con_time))
timetable = timetable[order(timetable$time),]
timetable$time <- as.Date(timetable$time)
head(timetable)
## time n position
## 1 2011-10-06 1 support
## 2 2011-10-13 1 support
## 3 2011-10-20 0 support
## 4 2011-10-27 6 support
## 5 2011-11-03 0 support
## 6 2011-11-10 2 support
ggplot(timetable, aes(x = time, y = n, colour = position, group = position)) +
geom_point(size = 0.4) +
geom_line(size = 0.25) +
scale_y_continuous(limits = c(-1, max(timetable$n)*1.05), name = "Tweets")+
scale_x_date(date_breaks = "3 months", date_labels = "%b-%Y", limits = c(min(timetable$time), max(timetable$time)), name = "Time") +
labs(title = "Number of Tweets by Supporters and Opponents")+
theme_bw()+
theme(legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 12), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) # when vjust = 0.5, the 45 degree x-axis labels overlap with time, when vjust = 1, the axis-labels move up
According to the Zoom meeting on November 29, 2021, the network analyses of policy actors should be separated by network types. Retweet, reply to, and mention are analyzed separately. Mention network is the main object.
The igraph
package does not have a native algorithm for network connectedness and fragmentation. According to Borgatti (2006), connectedness is defined as the proportion of pairs of nodes that can reach each other by a path of any length. \[\frac{\Sigma_{i \neq j}r_{ij}}{n(n-1)}\]
To specify:
"2017-01-03T06:00:00.000Z"
"2018-01-03T06:00:00.000Z"
"support"
congress=="115"
The bill supporters were much more likely to retweet. In 2017, the opponent’s coalition did not have any messages. Thus, using the aggregate command will trigger error message and halt the execution. One way to solve it is to discard the edge weight but to keep every original tie with the value of 1. This way, the visualization gives more contrast. Also, it is important to simplify the network when calculating centralization and density. The modularity returns the same value for simplified and the original network, when the work is not weighted at the very beginning.
rt_pro_2017 = df_pro[df_pro$created_at > "2017-01-03T06:00:00.000Z" & df_pro$created_at <= "2018-01-03T06:00:00.000Z" & df_pro$type=="retweeted" & df_pro$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(rt_pro_2017) <- c("from","to")
rt_pro_2017 = rt_pro_2017[rt_pro_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
rt_pro_2017$value = 1
#rt_pro_2017 <- aggregate(rt_pro_2017[,3], rt_pro_2017[,-3], sum)
rt_pro_2017 <- rt_pro_2017[rev(order(rt_pro_2017$value)),]
head(rt_pro_2017)
## # A tibble: 6 × 3
## from to value
## <chr> <chr> <dbl>
## 1 iSquaredAct SIIA_US 1
## 2 iSquaredAct lauradfrancis 1
## 3 gsiskind lauradfrancis 1
## 4 SIIA_US lauradfrancis 1
## 5 iSquaredAct SIIA_US 1
## 6 iSquaredAct SIIA_US 1
g.rt_pro_2017 = graph_from_data_frame(rt_pro_2017)
E(g.rt_pro_2017)$weight = rt_pro_2017$value
vcount(g.rt_pro_2017)
## [1] 6
ecount(g.rt_pro_2017)
## [1] 39
centralization.degree(g.rt_pro_2017, mode = "in", normalized = T)
## $res
## [1] 0 0 32 2 0 5
##
## $centralization
## [1] 5.1
##
## $theoretical_max
## [1] 30
centralization.degree(g.rt_pro_2017, mode = "out", normalized = T)
## $res
## [1] 31 2 4 1 1 0
##
## $centralization
## [1] 4.9
##
## $theoretical_max
## [1] 30
mean(degree(g.rt_pro_2017, V(g.rt_pro_2017),"all"))
## [1] 13
ecount(g.rt_pro_2017)/vcount(g.rt_pro_2017)
## [1] 6.5
ecount(g.rt_pro_2017)/(vcount(g.rt_pro_2017)*(vcount(g.rt_pro_2017)-1))
## [1] 1.3
edge.betweenness.community(g.rt_pro_2017, weights = E(g.rt_pro_2017)$weight, directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "gsiskind" "SIIA_US" "David_J_Bier"
## [5] "TheToddSchulte" "lauradfrancis"
##
modularity_rt_pro_2017 = modularity(g.rt_pro_2017, cluster_edge_betweenness(g.rt_pro_2017)$membership)
transitivity(g.rt_pro_2017)
## [1] 0.5
coreness(g.rt_pro_2017)
## iSquaredAct gsiskind SIIA_US David_J_Bier TheToddSchulte
## 28 2 28 3 1
## lauradfrancis
## 4
reciprocity(g.rt_pro_2017)
## [1] 0.05263158
# manual calculation of fragmentation
distance_matrix_rt_pro_2017 <- matrix(data = NA, nrow = vcount(g.rt_pro_2017),ncol = vcount(g.rt_pro_2017))
for (i in 1:vcount(g.rt_pro_2017))
{distance_matrix_rt_pro_2017[i,]<-distances(g.rt_pro_2017, v = V(g.rt_pro_2017)[i], to = V(g.rt_pro_2017))}
fragmentation_rt_pro_2017 = length(which(distance_matrix_rt_pro_2017=="Inf"))/(vcount(g.rt_pro_2017)*(vcount(g.rt_pro_2017)-1))
#dichotomous
indegree_rt_pro_2017=centralization.degree(simplify(g.rt_pro_2017), mode = "in", normalized = T)
outdegree_rt_pro_2017=centralization.degree(simplify(g.rt_pro_2017), mode = "out", normalized = T)
edge_density(simplify(g.rt_pro_2017)) # native algorithym
## [1] 0.3
ecount(simplify(g.rt_pro_2017))/(vcount(g.rt_pro_2017)*(vcount(g.rt_pro_2017)-1)) # manual calculation
## [1] 0.3
edge.betweenness.community(simplify(g.rt_pro_2017), directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "gsiskind" "SIIA_US" "David_J_Bier"
## [5] "TheToddSchulte" "lauradfrancis"
##
transitivity(simplify(g.rt_pro_2017))
## [1] 0.5
cohesion(simplify(g.rt_pro_2017))
## [1] 0
graph.cohesion(simplify(g.rt_pro_2017))
## [1] 0
reciprocity(simplify(g.rt_pro_2017))
## [1] 0.2222222
par(mar=c(2,1,2,1))
V(g.rt_pro_2017)$color = "orange"
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.rt_pro_2017)$color <- alpha(tail_of(g.rt_pro_2017,E(g.rt_pro_2017))$color, 0.7)
V(g.rt_pro_2017)$shape = "circle"
plot(g.rt_pro_2017, vertex.size = degree(g.rt_pro_2017, V(g.rt_pro_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_rt_pro_2017 <- c(vcount(g.rt_pro_2017), ecount(g.rt_pro_2017), mean(degree(g.rt_pro_2017)), modularity_rt_pro_2017,fragmentation_rt_pro_2017,reciprocity(g.rt_pro_2017), ecount(simplify(g.rt_pro_2017)), indegree_rt_pro_2017$centralization, outdegree_rt_pro_2017$centralization,edge_density(simplify(g.rt_pro_2017)), reciprocity(simplify(g.rt_pro_2017)))
names(property_rt_pro_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_rt_pro_2017,2)
Optional plot with icons
# try image
# consider unique image for government actors
# if use image, set the shape of vertices for each type of actors
img1 <- readPNG("icon3.png", native = F, info = F)
img3 <- readPNG("gov.png", native = F, info = F)
V(g.rt_pro_2017)$color = "orange"
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.rt_pro_2017)$color <- alpha(tail_of(g.rt_pro_2017,E(g.rt_pro_2017))$color, 0.7)
V(g.rt_pro_2017)$shape = "circle"
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$type!="government"]$raster = list(img1)
## Warning: Unknown or uninitialised column: `type`.
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$type=="government"]$raster = list(img3)
## Warning: Unknown or uninitialised column: `type`.
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$type!="government"]$shape = "raster"
## Warning: Unknown or uninitialised column: `type`.
V(g.rt_pro_2017)[V(g.rt_pro_2017)$name%in%df_actor$type=="government"]$shape = "raster"
## Warning: Unknown or uninitialised column: `type`.
plot(g.rt_pro_2017, vertex.size = 20, edge.arrow.size = .5, vertex.label = NA, vertex.frame.color = "gray60")
copied from pro
rt_con_2017 = df_con[df_con$created_at > "2017-01-03T06:00:00.000Z" & df_con$created_at <= "2018-01-03T06:00:00.000Z" & df_con$type=="retweeted" & df_con$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(rt_con_2017) <- c("from","to")
rt_con_2017 = rt_con_2017[rt_con_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
rt_con_2017$value = 1
#rt_con_2017 <- aggregate(rt_con_2017[,3], rt_con_2017[,-3], sum)
rt_con_2017 <- rt_con_2017[rev(order(rt_con_2017$value)),]
head(rt_con_2017)
g.rt_con_2017 = graph_from_data_frame(rt_con_2017)
E(g.rt_con_2017)$weight = rt_con_2017$value
vcount(g.rt_con_2017)
ecount(g.rt_con_2017)
centralization.degree(g.rt_con_2017, mode = "in", normalized = T)
centralization.degree(g.rt_con_2017, mode = "out", normalized = T)
mean(degree(g.rt_con_2017, V(g.rt_con_2017),"all"))
ecount(g.rt_con_2017)/vcount(g.rt_con_2017)
ecount(g.rt_con_2017)/(vcount(g.rt_con_2017)*(vcount(g.rt_con_2017)-1))
edge.betweenness.community(g.rt_con_2017, weights = E(g.rt_con_2017)$weight, directed = F)
modularity_rt_con_2017 = modularity(g.rt_con_2017, cluster_edge_betweenness(g.rt_con_2017)$membership)
transitivity(g.rt_con_2017)
coreness(g.rt_con_2017)
reciprocity(g.rt_con_2017)
# manual calculation of fragmentation
distance_matrix_rt_con_2017 <- matrix(data = NA, nrow = vcount(g.rt_con_2017),ncol = vcount(g.rt_con_2017))
for (i in 1:vcount(g.rt_con_2017))
{distance_matrix_rt_con_2017[i,]<-distances(g.rt_con_2017, v = V(g.rt_con_2017)[i], to = V(g.rt_con_2017))}
fragmentation_rt_con_2017 = length(which(distance_matrix_rt_con_2017=="Inf"))/(vcount(g.rt_con_2017)*(vcount(g.rt_con_2017)-1))
#dichotomous
indegree_rt_con_2017=centralization.degree(simplify(g.rt_con_2017), mode = "in", normalized = T)
outdegree_rt_con_2017=centralization.degree(simplify(g.rt_con_2017), mode = "out", normalized = T)
edge_density(simplify(g.rt_con_2017)) # native algorithym
ecount(simplify(g.rt_con_2017))/(vcount(g.rt_con_2017)*(vcount(g.rt_con_2017)-1)) # manual calculation
edge.betweenness.community(simplify(g.rt_con_2017), directed = F)
transitivity(simplify(g.rt_con_2017))
cohesion(simplify(g.rt_con_2017))
graph.cohesion(simplify(g.rt_con_2017))
reciprocity(simplify(g.rt_con_2017))
par(mar=c(2,1,2,1))
V(g.rt_con_2017)$color = "orange"
V(g.rt_con_2017)[V(g.rt_con_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.rt_con_2017)$color <- alpha(tail_of(g.rt_con_2017,E(g.rt_con_2017))$color, 0.7)
V(g.rt_con_2017)$shape = "circle"
plot(g.rt_con_2017, vertex.size = degree(g.rt_con_2017, V(g.rt_con_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_rt_con_2017 <- c(vcount(g.rt_con_2017), ecount(g.rt_con_2017), mean(degree(g.rt_con_2017)), modularity_rt_con_2017,fragmentation_rt_con_2017,reciprocity(g.rt_con_2017), ecount(simplify(g.rt_con_2017)), indegree_rt_con_2017$centralization, outdegree_rt_con_2017$centralization,edge_density(simplify(g.rt_con_2017)), reciprocity(simplify(g.rt_con_2017)))
names(property_rt_con_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
round(property_rt_con_2017,2)
rt_2017 = df[df$created_at > "2017-01-03T06:00:00.000Z" & df$created_at <= "2018-01-03T06:00:00.000Z" & df$type=="retweeted" & df$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(rt_2017) <- c("from","to")
rt_2017 = rt_2017[rt_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
rt_2017$value = 1
g.rt_2017 = graph_from_data_frame(rt_2017) # don't rbind the supporters and opponents, because if one coalition doesn't have observations, it'll trigger errors.
vcount(g.rt_2017)
## [1] 6
ecount(g.rt_2017)
## [1] 39
centralization.degree(g.rt_2017, mode = "in", normalized = T)
## $res
## [1] 0 0 32 2 0 5
##
## $centralization
## [1] 5.1
##
## $theoretical_max
## [1] 30
centralization.degree(g.rt_2017, mode = "out", normalized = T)
## $res
## [1] 31 1 4 1 2 0
##
## $centralization
## [1] 4.9
##
## $theoretical_max
## [1] 30
mean(degree(g.rt_2017, V(g.rt_2017),"all"))
## [1] 13
ecount(g.rt_2017)/vcount(g.rt_2017)
## [1] 6.5
ecount(g.rt_2017)/(vcount(g.rt_2017)*(vcount(g.rt_2017)-1))
## [1] 1.3
edge.betweenness.community(g.rt_2017, weights = E(g.rt_2017)$weight, directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "SIIA_US" "David_J_Bier"
## [5] "gsiskind" "lauradfrancis"
##
modularity_rt_2017 = modularity(g.rt_2017, cluster_edge_betweenness(g.rt_2017)$membership)
transitivity(g.rt_2017)
## [1] 0.5
coreness(g.rt_2017)
## iSquaredAct TheToddSchulte SIIA_US David_J_Bier gsiskind
## 28 1 28 3 2
## lauradfrancis
## 4
reciprocity(g.rt_2017)
## [1] 0.05263158
# manual calculation of fragmentation
distance_matrix_rt_2017 <- matrix(data = NA, nrow = vcount(g.rt_2017),ncol = vcount(g.rt_2017))
for (i in 1:vcount(g.rt_2017))
{distance_matrix_rt_2017[i,]<-distances(g.rt_2017, v = V(g.rt_2017)[i], to = V(g.rt_2017))}
fragmentation_rt_2017 = length(which(distance_matrix_rt_2017=="Inf"))/(vcount(g.rt_2017)*(vcount(g.rt_2017)-1))
#dichotomous
indegree_rt_2017=centralization.degree(simplify(g.rt_2017), mode = "in", normalized = T)
outdegree_rt_2017=centralization.degree(simplify(g.rt_2017), mode = "out", normalized = T)
edge_density(simplify(g.rt_2017)) # native algorithym
## [1] 0.3
ecount(simplify(g.rt_2017))/(vcount(g.rt_2017)*(vcount(g.rt_2017)-1)) # manual calculation
## [1] 0.3
edge.betweenness.community(simplify(g.rt_2017), directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "SIIA_US" "David_J_Bier"
## [5] "gsiskind" "lauradfrancis"
##
transitivity(simplify(g.rt_2017))
## [1] 0.5
cohesion(simplify(g.rt_2017))
## [1] 0
graph.cohesion(simplify(g.rt_2017))
## [1] 0
reciprocity(simplify(g.rt_2017))
## [1] 0.2222222
par(mar=c(2,1,2,1))
V(g.rt_2017)$color = "orange"
V(g.rt_2017)[V(g.rt_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.rt_2017)$color <- alpha(tail_of(g.rt_2017,E(g.rt_2017))$color, 0.7)
plot(g.rt_2017, vertex.size = degree(g.rt_2017, V(g.rt_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_rt_2017 <- c(vcount(g.rt_2017), ecount(g.rt_2017), mean(degree(g.rt_2017)), modularity_rt_2017,fragmentation_rt_2017,reciprocity(g.rt_2017), ecount(simplify(g.rt_2017)), indegree_rt_2017$centralization, outdegree_rt_2017$centralization,edge_density(simplify(g.rt_2017)), reciprocity(simplify(g.rt_2017)))
names(property_rt_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
round(property_rt_2017,2)
## node count edge count (raw)
## 6.00 39.00
## average degree (weighted density) modularity
## 13.00 0.00
## fragmentation reciprocity (weighted)
## 0.00 0.05
## edge count (dichotomized) indegree centralization (dichotomized)
## 9.00 0.50
## outdegree centralization (dichotomized) density (dichotomized)
## 0.30 0.30
## reciprocity (dichotomized)
## 0.22
property_rt_all_2017 = data.frame(rbind(property_rt_pro_2017,property_rt_2017))
#property_rt_all_2017 = data.frame(rbind(property_rt_pro_2017, property_rt_con_2017, property_rt_2017))
names(property_rt_all_2017) <- c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#property_rt_all_2017
reply_pro_2017 = df_pro[df_pro$created_at > "2017-01-03T06:00:00.000Z" & df_pro$created_at <= "2018-01-03T06:00:00.000Z" & df_pro$type=="replied_to" & df_pro$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(reply_pro_2017) <- c("from","to")
reply_pro_2017 = reply_pro_2017[reply_pro_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
reply_pro_2017$value = 1
#reply_pro_2017 <- aggregate(reply_pro_2017[,3], reply_pro_2017[,-3], sum)
reply_pro_2017 <- reply_pro_2017[rev(order(reply_pro_2017$value)),]
head(reply_pro_2017)
## # A tibble: 2 × 3
## from to value
## <chr> <chr> <dbl>
## 1 SIIA_US lauradfrancis 1
## 2 iSquaredAct SIIA_US 1
g.reply_pro_2017 = graph_from_data_frame(reply_pro_2017)
E(g.reply_pro_2017)$weight = reply_pro_2017$value
vcount(g.reply_pro_2017)
## [1] 3
ecount(g.reply_pro_2017)
## [1] 2
centralization.degree(g.reply_pro_2017, mode = "in", normalized = T)
## $res
## [1] 1 0 1
##
## $centralization
## [1] 0.1666667
##
## $theoretical_max
## [1] 6
centralization.degree(g.reply_pro_2017, mode = "out", normalized = T)
## $res
## [1] 1 1 0
##
## $centralization
## [1] 0.1666667
##
## $theoretical_max
## [1] 6
mean(degree(g.reply_pro_2017, V(g.reply_pro_2017),"all"))
## [1] 1.333333
ecount(g.reply_pro_2017)/vcount(g.reply_pro_2017)
## [1] 0.6666667
ecount(g.reply_pro_2017)/(vcount(g.reply_pro_2017)*(vcount(g.reply_pro_2017)-1))
## [1] 0.3333333
edge.betweenness.community(g.reply_pro_2017, weights = E(g.reply_pro_2017)$weight, directed = F)
## Warning in edge.betweenness.community(g.reply_pro_2017, weights =
## E(g.reply_pro_2017)$weight, : At community.c:460 :Membership vector will be
## selected based on the lowest modularity score.
## Warning in edge.betweenness.community(g.reply_pro_2017, weights =
## E(g.reply_pro_2017)$weight, : At community.c:467 :Modularity calculation with
## weighted edge betweenness community detection might not make sense -- modularity
## treats edge weights as similarities while edge betwenness treats them as
## distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "SIIA_US" "iSquaredAct" "lauradfrancis"
##
modularity_reply_pro_2017 = modularity(g.reply_pro_2017, cluster_edge_betweenness(g.reply_pro_2017)$membership)
## Warning in cluster_edge_betweenness(g.reply_pro_2017): At
## community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in cluster_edge_betweenness(g.reply_pro_2017): At
## community.c:467 :Modularity calculation with weighted edge betweenness community
## detection might not make sense -- modularity treats edge weights as similarities
## while edge betwenness treats them as distances
transitivity(g.reply_pro_2017)
## [1] 0
coreness(g.reply_pro_2017)
## SIIA_US iSquaredAct lauradfrancis
## 1 1 1
reciprocity(g.reply_pro_2017)
## [1] 0
# manual calculation of fragmentation
distance_matrix_reply_pro_2017 <- matrix(data = NA, nrow = vcount(g.reply_pro_2017),ncol = vcount(g.reply_pro_2017))
for (i in 1:vcount(g.reply_pro_2017))
{distance_matrix_reply_pro_2017[i,]<-distances(g.reply_pro_2017, v = V(g.reply_pro_2017)[i], to = V(g.reply_pro_2017))}
fragmentation_reply_pro_2017 = length(which(distance_matrix_reply_pro_2017=="Inf"))/(vcount(g.reply_pro_2017)*(vcount(g.reply_pro_2017)-1))
#dichotomous
indegree_reply_pro_2017=centralization.degree(simplify(g.reply_pro_2017), mode = "in", normalized = T)
outdegree_reply_pro_2017=centralization.degree(simplify(g.reply_pro_2017), mode = "out", normalized = T)
edge_density(simplify(g.reply_pro_2017)) # native algorithym
## [1] 0.3333333
ecount(simplify(g.reply_pro_2017))/(vcount(g.reply_pro_2017)*(vcount(g.reply_pro_2017)-1)) # manual calculation
## [1] 0.3333333
edge.betweenness.community(simplify(g.reply_pro_2017), directed = F)
## Warning in edge.betweenness.community(simplify(g.reply_pro_2017), directed =
## F): At community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in edge.betweenness.community(simplify(g.reply_pro_2017), directed =
## F): At community.c:467 :Modularity calculation with weighted edge betweenness
## community detection might not make sense -- modularity treats edge weights as
## similarities while edge betwenness treats them as distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "SIIA_US" "iSquaredAct" "lauradfrancis"
##
transitivity(simplify(g.reply_pro_2017))
## [1] 0
cohesion(simplify(g.reply_pro_2017))
## [1] 0
graph.cohesion(simplify(g.reply_pro_2017))
## [1] 0
reciprocity(simplify(g.reply_pro_2017))
## [1] 0
par(mar=c(2,1,2,1))
V(g.reply_pro_2017)$color = "orange"
V(g.reply_pro_2017)[V(g.reply_pro_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.reply_pro_2017)$color <- alpha(tail_of(g.reply_pro_2017,E(g.reply_pro_2017))$color, 0.7)
V(g.reply_pro_2017)$shape = "circle"
plot(g.reply_pro_2017, vertex.size = degree(g.reply_pro_2017, V(g.reply_pro_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_reply_pro_2017 <- c(vcount(g.reply_pro_2017), ecount(g.reply_pro_2017), mean(degree(g.reply_pro_2017)), modularity_reply_pro_2017,fragmentation_reply_pro_2017,reciprocity(g.reply_pro_2017), ecount(simplify(g.reply_pro_2017)), indegree_reply_pro_2017$centralization, outdegree_reply_pro_2017$centralization,edge_density(simplify(g.reply_pro_2017)), reciprocity(simplify(g.reply_pro_2017)))
names(property_reply_pro_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_reply_pro_2017,2)
reply_con_2017 = df_con[df_con$created_at > "2017-01-03T06:00:00.000Z" & df_con$created_at <= "2018-01-03T06:00:00.000Z" & df_con$type=="replied_to" & df_con$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(reply_con_2017) <- c("from","to")
reply_con_2017 = reply_con_2017[reply_con_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
reply_con_2017$value = 1
#reply_con_2017 <- aggregate(reply_con_2017[,3], reply_con_2017[,-3], sum)
reply_con_2017 <- reply_con_2017[rev(order(reply_con_2017$value)),]
head(reply_con_2017)
g.reply_con_2017 = graph_from_data_frame(reply_con_2017)
E(g.reply_con_2017)$weight = reply_con_2017$value
vcount(g.reply_con_2017)
ecount(g.reply_con_2017)
centralization.degree(g.reply_con_2017, mode = "in", normalized = T)
centralization.degree(g.reply_con_2017, mode = "out", normalized = T)
mean(degree(g.reply_con_2017, V(g.reply_con_2017),"all"))
ecount(g.reply_con_2017)/vcount(g.reply_con_2017)
ecount(g.reply_con_2017)/(vcount(g.reply_con_2017)*(vcount(g.reply_con_2017)-1))
edge.betweenness.community(g.reply_con_2017, weights = E(g.reply_con_2017)$weight, directed = F)
modularity_reply_con_2017 = modularity(g.reply_con_2017, cluster_edge_betweenness(g.reply_con_2017)$membership)
transitivity(g.reply_con_2017)
coreness(g.reply_con_2017)
reciprocity(g.reply_con_2017)
# manual calculation of fragmentation
distance_matrix_reply_con_2017 <- matrix(data = NA, nrow = vcount(g.reply_con_2017),ncol = vcount(g.reply_con_2017))
for (i in 1:vcount(g.reply_con_2017))
{distance_matrix_reply_con_2017[i,]<-distances(g.reply_con_2017, v = V(g.reply_con_2017)[i], to = V(g.reply_con_2017))}
fragmentation_reply_con_2017 = length(which(distance_matrix_reply_con_2017=="Inf"))/(vcount(g.reply_con_2017)*(vcount(g.reply_con_2017)-1))
#dichotomous
indegree_reply_con_2017=centralization.degree(simplify(g.reply_con_2017), mode = "in", normalized = T)
outdegree_reply_con_2017=centralization.degree(simplify(g.reply_con_2017), mode = "out", normalized = T)
edge_density(simplify(g.reply_con_2017)) # native algorithym
ecount(simplify(g.reply_con_2017))/(vcount(g.reply_con_2017)*(vcount(g.reply_con_2017)-1)) # manual calculation
edge.betweenness.community(simplify(g.reply_con_2017), directed = F)
transitivity(simplify(g.reply_con_2017))
cohesion(simplify(g.reply_con_2017))
graph.cohesion(simplify(g.reply_con_2017))
reciprocity(simplify(g.reply_con_2017))
par(mar=c(2,1,2,1))
V(g.reply_con_2017)$color = "orange"
V(g.reply_con_2017)[V(g.reply_con_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.reply_con_2017)$color <- alpha(tail_of(g.reply_con_2017,E(g.reply_con_2017))$color, 0.7)
V(g.reply_con_2017)$shape = "circle"
plot(g.reply_con_2017, vertex.size = degree(g.reply_con_2017, V(g.reply_con_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_reply_con_2017 <- c(vcount(g.reply_con_2017), ecount(g.reply_con_2017), mean(degree(g.reply_con_2017)), modularity_reply_con_2017,fragmentation_reply_con_2017,reciprocity(g.reply_con_2017), ecount(simplify(g.reply_con_2017)), indegree_reply_con_2017$centralization, outdegree_reply_con_2017$centralization,edge_density(simplify(g.reply_con_2017)), reciprocity(simplify(g.reply_con_2017)))
names(property_reply_con_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_reply_con_2017,2)
reply_2017 = df[df$created_at > "2017-01-03T06:00:00.000Z" & df$created_at <= "2018-01-03T06:00:00.000Z" & df$type=="replied_to" & df$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(reply_2017) <- c("from","to")
reply_2017 = reply_2017[reply_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
reply_2017$value = 1
g.reply_2017 = graph_from_data_frame(reply_2017)
vcount(g.reply_2017)
## [1] 3
ecount(g.reply_2017)
## [1] 2
centralization.degree(g.reply_2017, mode = "in", normalized = T)
## $res
## [1] 0 1 1
##
## $centralization
## [1] 0.1666667
##
## $theoretical_max
## [1] 6
centralization.degree(g.reply_2017, mode = "out", normalized = T)
## $res
## [1] 1 1 0
##
## $centralization
## [1] 0.1666667
##
## $theoretical_max
## [1] 6
mean(degree(g.reply_2017, V(g.reply_2017),"all"))
## [1] 1.333333
ecount(g.reply_2017)/vcount(g.reply_2017)
## [1] 0.6666667
ecount(g.reply_2017)/(vcount(g.reply_2017)*(vcount(g.reply_2017)-1))
## [1] 0.3333333
edge.betweenness.community(g.reply_2017, weights = E(g.reply_2017)$weight, directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "SIIA_US" "lauradfrancis"
##
modularity_reply_2017 = modularity(g.reply_2017, cluster_edge_betweenness(g.reply_2017)$membership)
transitivity(g.reply_2017)
## [1] 0
coreness(g.reply_2017)
## iSquaredAct SIIA_US lauradfrancis
## 1 1 1
reciprocity(g.reply_2017)
## [1] 0
# manual calculation of fragmentation
distance_matrix_reply_2017 <- matrix(data = NA, nrow = vcount(g.reply_2017),ncol = vcount(g.reply_2017))
for (i in 1:vcount(g.reply_2017))
{distance_matrix_reply_2017[i,]<-distances(g.reply_2017, v = V(g.reply_2017)[i], to = V(g.reply_2017))}
fragmentation_reply_2017 = length(which(distance_matrix_reply_2017=="Inf"))/(vcount(g.reply_2017)*(vcount(g.reply_2017)-1))
#dichotomous
indegree_reply_2017=centralization.degree(simplify(g.reply_2017), mode = "in", normalized = T)
outdegree_reply_2017=centralization.degree(simplify(g.reply_2017), mode = "out", normalized = T)
edge_density(simplify(g.reply_2017)) # native algorithym
## [1] 0.3333333
ecount(simplify(g.reply_2017))/(vcount(g.reply_2017)*(vcount(g.reply_2017)-1)) # manual calculation
## [1] 0.3333333
edge.betweenness.community(simplify(g.reply_2017), directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "SIIA_US" "lauradfrancis"
##
transitivity(simplify(g.reply_2017))
## [1] 0
cohesion(simplify(g.reply_2017))
## [1] 0
graph.cohesion(simplify(g.reply_2017))
## [1] 0
reciprocity(simplify(g.reply_2017))
## [1] 0
par(mar=c(2,1,2,1))
V(g.reply_2017)$color = "orange"
V(g.reply_2017)[V(g.reply_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.reply_2017)$color <- alpha(tail_of(g.reply_2017,E(g.reply_2017))$color, 0.7)
plot(g.reply_2017, vertex.size = degree(g.reply_2017, V(g.reply_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_reply_2017 <- c(vcount(g.reply_2017), ecount(g.reply_2017), mean(degree(g.reply_2017)), modularity_reply_2017,fragmentation_reply_2017,reciprocity(g.reply_2017), ecount(simplify(g.reply_2017)), indegree_reply_2017$centralization, outdegree_reply_2017$centralization,edge_density(simplify(g.reply_2017)), reciprocity(simplify(g.reply_2017)))
names(property_reply_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
round(property_reply_2017,2)
## node count edge count (raw)
## 3.00 2.00
## average degree (weighted density) modularity
## 1.33 0.00
## fragmentation reciprocity (weighted)
## 0.00 0.00
## edge count (dichotomized) indegree centralization (dichotomized)
## 2.00 0.17
## outdegree centralization (dichotomized) density (dichotomized)
## 0.17 0.33
## reciprocity (dichotomized)
## 0.00
property_reply_all_2017 = data.frame(rbind(property_reply_pro_2017,property_reply_2017))
#property_reply_all_2017 = data.frame(rbind(property_reply_pro_2017, property_reply_con_2017, property_reply_2017))
names(property_reply_all_2017) <- c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#property_reply_all_2017
Quoted tweets is like a comment and feedback on another tweet.
quote_pro_2017 = df_pro[df_pro$created_at > "2017-01-03T06:00:00.000Z" & df_pro$created_at <= "2018-01-03T06:00:00.000Z" & df_pro$type=="quoted" & df_pro$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(quote_pro_2017) <- c("from","to")
quote_pro_2017 = quote_pro_2017[quote_pro_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
quote_pro_2017$value = 1
#quote_pro_2017 <- aggregate(quote_pro_2017[,3], quote_pro_2017[,-3], sum)
quote_pro_2017 <- quote_pro_2017[rev(order(quote_pro_2017$value)),]
head(quote_pro_2017)
## # A tibble: 1 × 3
## from to value
## <chr> <chr> <dbl>
## 1 David_J_Bier David_J_Bier 1
g.quote_pro_2017 = graph_from_data_frame(quote_pro_2017)
E(g.quote_pro_2017)$weight = quote_pro_2017$value
vcount(g.quote_pro_2017)
## [1] 1
ecount(g.quote_pro_2017)
## [1] 1
centralization.degree(g.quote_pro_2017, mode = "in", normalized = T)
## $res
## [1] 1
##
## $centralization
## [1] NaN
##
## $theoretical_max
## [1] 0
centralization.degree(g.quote_pro_2017, mode = "out", normalized = T)
## $res
## [1] 1
##
## $centralization
## [1] NaN
##
## $theoretical_max
## [1] 0
mean(degree(g.quote_pro_2017, V(g.quote_pro_2017),"all"))
## [1] 2
ecount(g.quote_pro_2017)/vcount(g.quote_pro_2017)
## [1] 1
ecount(g.quote_pro_2017)/(vcount(g.quote_pro_2017)*(vcount(g.quote_pro_2017)-1))
## [1] Inf
edge.betweenness.community(g.quote_pro_2017, weights = E(g.quote_pro_2017)$weight, directed = F)
## Warning in edge.betweenness.community(g.quote_pro_2017, weights =
## E(g.quote_pro_2017)$weight, : At community.c:460 :Membership vector will be
## selected based on the lowest modularity score.
## Warning in edge.betweenness.community(g.quote_pro_2017, weights =
## E(g.quote_pro_2017)$weight, : At community.c:467 :Modularity calculation with
## weighted edge betweenness community detection might not make sense -- modularity
## treats edge weights as similarities while edge betwenness treats them as
## distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "David_J_Bier"
##
modularity_quote_pro_2017 = modularity(g.quote_pro_2017, cluster_edge_betweenness(g.quote_pro_2017)$membership)
## Warning in cluster_edge_betweenness(g.quote_pro_2017): At
## community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in cluster_edge_betweenness(g.quote_pro_2017): At
## community.c:467 :Modularity calculation with weighted edge betweenness community
## detection might not make sense -- modularity treats edge weights as similarities
## while edge betwenness treats them as distances
transitivity(g.quote_pro_2017)
## [1] NaN
coreness(g.quote_pro_2017)
## David_J_Bier
## 2
reciprocity(g.quote_pro_2017)
## [1] NaN
# manual calculation of fragmentation
distance_matrix_quote_pro_2017 <- matrix(data = NA, nrow = vcount(g.quote_pro_2017),ncol = vcount(g.quote_pro_2017))
for (i in 1:vcount(g.quote_pro_2017))
{distance_matrix_quote_pro_2017[i,]<-distances(g.quote_pro_2017, v = V(g.quote_pro_2017)[i], to = V(g.quote_pro_2017))}
fragmentation_quote_pro_2017 = length(which(distance_matrix_quote_pro_2017=="Inf"))/(vcount(g.quote_pro_2017)*(vcount(g.quote_pro_2017)-1))
#dichotomous
indegree_quote_pro_2017=centralization.degree(simplify(g.quote_pro_2017), mode = "in", normalized = T)
outdegree_quote_pro_2017=centralization.degree(simplify(g.quote_pro_2017), mode = "out", normalized = T)
edge_density(simplify(g.quote_pro_2017)) # native algorithym
## [1] NaN
ecount(simplify(g.quote_pro_2017))/(vcount(g.quote_pro_2017)*(vcount(g.quote_pro_2017)-1)) # manual calculation
## [1] NaN
edge.betweenness.community(simplify(g.quote_pro_2017), directed = F)
## Warning in edge.betweenness.community(simplify(g.quote_pro_2017), directed =
## F): At community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in edge.betweenness.community(simplify(g.quote_pro_2017), directed =
## F): At community.c:467 :Modularity calculation with weighted edge betweenness
## community detection might not make sense -- modularity treats edge weights as
## similarities while edge betwenness treats them as distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "David_J_Bier"
##
transitivity(simplify(g.quote_pro_2017))
## [1] NaN
cohesion(simplify(g.quote_pro_2017))
## [1] 0
graph.cohesion(simplify(g.quote_pro_2017))
## [1] 0
reciprocity(simplify(g.quote_pro_2017))
## [1] NaN
par(mar=c(2,1,2,1))
V(g.quote_pro_2017)$color = "orange"
V(g.quote_pro_2017)[V(g.quote_pro_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.quote_pro_2017)$color <- alpha(tail_of(g.quote_pro_2017,E(g.quote_pro_2017))$color, 0.7)
V(g.quote_pro_2017)$shape = "circle"
plot(g.quote_pro_2017, vertex.size = degree(g.quote_pro_2017, V(g.quote_pro_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_quote_pro_2017 <- c(vcount(g.quote_pro_2017), ecount(g.quote_pro_2017), mean(degree(g.quote_pro_2017)), modularity_quote_pro_2017,fragmentation_quote_pro_2017,reciprocity(g.quote_pro_2017), ecount(simplify(g.quote_pro_2017)), indegree_quote_pro_2017$centralization, outdegree_quote_pro_2017$centralization,edge_density(simplify(g.quote_pro_2017)), reciprocity(simplify(g.quote_pro_2017)))
names(property_quote_pro_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_quote_pro_2017,2)
quote_con_2017 = df_con[df_con$created_at > "2017-01-03T06:00:00.000Z" & df_con$created_at <= "2018-01-03T06:00:00.000Z" & df_con$type=="quoted" & df_con$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(quote_con_2017) <- c("from","to")
quote_con_2017 = quote_con_2017[quote_con_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
quote_con_2017$value = 1
#quote_con_2017 <- aggregate(quote_con_2017[,3], quote_con_2017[,-3], sum)
quote_con_2017 <- quote_con_2017[rev(order(quote_con_2017$value)),]
head(quote_con_2017)
g.quote_con_2017 = graph_from_data_frame(quote_con_2017)
E(g.quote_con_2017)$weight = quote_con_2017$value
vcount(g.quote_con_2017)
ecount(g.quote_con_2017)
centralization.degree(g.quote_con_2017, mode = "in", normalized = T)
centralization.degree(g.quote_con_2017, mode = "out", normalized = T)
mean(degree(g.quote_con_2017, V(g.quote_con_2017),"all"))
ecount(g.quote_con_2017)/vcount(g.quote_con_2017)
ecount(g.quote_con_2017)/(vcount(g.quote_con_2017)*(vcount(g.quote_con_2017)-1))
edge.betweenness.community(g.quote_con_2017, weights = E(g.quote_con_2017)$weight, directed = F)
modularity_quote_con_2017 = modularity(g.quote_con_2017, cluster_edge_betweenness(g.quote_con_2017)$membership)
transitivity(g.quote_con_2017)
coreness(g.quote_con_2017)
reciprocity(g.quote_con_2017)
# manual calculation of fragmentation
distance_matrix_quote_con_2017 <- matrix(data = NA, nrow = vcount(g.quote_con_2017),ncol = vcount(g.quote_con_2017))
for (i in 1:vcount(g.quote_con_2017))
{distance_matrix_quote_con_2017[i,]<-distances(g.quote_con_2017, v = V(g.quote_con_2017)[i], to = V(g.quote_con_2017))}
fragmentation_quote_con_2017 = length(which(distance_matrix_quote_con_2017=="Inf"))/(vcount(g.quote_con_2017)*(vcount(g.quote_con_2017)-1))
#dichotomous
indegree_quote_con_2017=centralization.degree(simplify(g.quote_con_2017), mode = "in", normalized = T)
outdegree_quote_con_2017=centralization.degree(simplify(g.quote_con_2017), mode = "out", normalized = T)
edge_density(simplify(g.quote_con_2017)) # native algorithym
ecount(simplify(g.quote_con_2017))/(vcount(g.quote_con_2017)*(vcount(g.quote_con_2017)-1)) # manual calculation
edge.betweenness.community(simplify(g.quote_con_2017), directed = F)
transitivity(simplify(g.quote_con_2017))
cohesion(simplify(g.quote_con_2017))
graph.cohesion(simplify(g.quote_con_2017))
reciprocity(simplify(g.quote_con_2017))
par(mar=c(2,1,2,1))
V(g.quote_con_2017)$color = "orange"
V(g.quote_con_2017)[V(g.quote_con_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.quote_con_2017)$color <- alpha(tail_of(g.quote_con_2017,E(g.quote_con_2017))$color, 0.7)
V(g.quote_con_2017)$shape = "circle"
plot(g.quote_con_2017, vertex.size = degree(g.quote_con_2017, V(g.quote_con_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_quote_con_2017 <- c(vcount(g.quote_con_2017), ecount(g.quote_con_2017), mean(degree(g.quote_con_2017)), modularity_quote_con_2017,fragmentation_quote_con_2017,reciprocity(g.quote_con_2017), ecount(simplify(g.quote_con_2017)), indegree_quote_con_2017$centralization, outdegree_quote_con_2017$centralization,edge_density(simplify(g.quote_con_2017)), reciprocity(simplify(g.quote_con_2017)))
names(property_quote_con_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_quote_con_2017,2)
quote_2017 = df[df$created_at > "2017-01-03T06:00:00.000Z" & df$created_at <= "2018-01-03T06:00:00.000Z" & df$type=="quoted" & df$username%in%df_actor$name[df_actor$congress=="115"],c("username","referenced_username")] # tweets within the specific YEAR, actors with the specific position and the congressional term.
names(quote_2017) <- c("from","to")
quote_2017 = quote_2017[quote_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
quote_2017$value = 1
g.quote_2017 = graph_from_data_frame(quote_2017)
vcount(g.quote_2017)
## [1] 1
ecount(g.quote_2017)
## [1] 1
centralization.degree(g.quote_2017, mode = "in", normalized = T)
## $res
## [1] 1
##
## $centralization
## [1] NaN
##
## $theoretical_max
## [1] 0
centralization.degree(g.quote_2017, mode = "out", normalized = T)
## $res
## [1] 1
##
## $centralization
## [1] NaN
##
## $theoretical_max
## [1] 0
mean(degree(g.quote_2017, V(g.quote_2017),"all"))
## [1] 2
ecount(g.quote_2017)/vcount(g.quote_2017)
## [1] 1
ecount(g.quote_2017)/(vcount(g.quote_2017)*(vcount(g.quote_2017)-1))
## [1] Inf
edge.betweenness.community(g.quote_2017, weights = E(g.quote_2017)$weight, directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "David_J_Bier"
##
modularity_quote_2017 = modularity(g.quote_2017, cluster_edge_betweenness(g.quote_2017)$membership)
transitivity(g.quote_2017)
## [1] NaN
coreness(g.quote_2017)
## David_J_Bier
## 2
reciprocity(g.quote_2017)
## [1] NaN
# manual calculation of fragmentation
distance_matrix_quote_2017 <- matrix(data = NA, nrow = vcount(g.quote_2017),ncol = vcount(g.quote_2017))
for (i in 1:vcount(g.quote_2017))
{distance_matrix_quote_2017[i,]<-distances(g.quote_2017, v = V(g.quote_2017)[i], to = V(g.quote_2017))}
fragmentation_quote_2017 = length(which(distance_matrix_quote_2017=="Inf"))/(vcount(g.quote_2017)*(vcount(g.quote_2017)-1))
#dichotomous
indegree_quote_2017=centralization.degree(simplify(g.quote_2017), mode = "in", normalized = T)
outdegree_quote_2017=centralization.degree(simplify(g.quote_2017), mode = "out", normalized = T)
edge_density(simplify(g.quote_2017)) # native algorithym
## [1] NaN
ecount(simplify(g.quote_2017))/(vcount(g.quote_2017)*(vcount(g.quote_2017)-1)) # manual calculation
## [1] NaN
edge.betweenness.community(simplify(g.quote_2017), directed = F)
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "David_J_Bier"
##
transitivity(simplify(g.quote_2017))
## [1] NaN
cohesion(simplify(g.quote_2017))
## [1] 0
graph.cohesion(simplify(g.quote_2017))
## [1] 0
reciprocity(simplify(g.quote_2017))
## [1] NaN
par(mar=c(2,1,2,1))
V(g.quote_2017)$color = "orange"
V(g.quote_2017)[V(g.quote_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.quote_2017)$color <- alpha(tail_of(g.quote_2017,E(g.quote_2017))$color, 0.7)
plot(g.quote_2017, vertex.size = degree(g.quote_2017, V(g.quote_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_quote_2017 <- c(vcount(g.quote_2017), ecount(g.quote_2017), mean(degree(g.quote_2017)), modularity_quote_2017,fragmentation_quote_2017,reciprocity(g.quote_2017), ecount(simplify(g.quote_2017)), indegree_quote_2017$centralization, outdegree_quote_2017$centralization,edge_density(simplify(g.quote_2017)), reciprocity(simplify(g.quote_2017)))
names(property_quote_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
round(property_quote_2017,2)
## node count edge count (raw)
## 1 1
## average degree (weighted density) modularity
## 2 0
## fragmentation reciprocity (weighted)
## NaN NaN
## edge count (dichotomized) indegree centralization (dichotomized)
## 0 NaN
## outdegree centralization (dichotomized) density (dichotomized)
## NaN NaN
## reciprocity (dichotomized)
## NaN
property_quote_all_2017 = data.frame(rbind(property_quote_pro_2017,property_quote_2017))
#property_quote_all_2017 = data.frame(rbind(property_quote_pro_2017, property_quote_con_2017, property_quote_2017))
names(property_quote_all_2017) <- c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#property_quote_all_2017
The first step is to substract the retweet, reply to, and quoted from actual mentions. The following is a step-by-step process to remove false mentions from the actual mentions.
Use mutate
to reverse aggregate
df_mention_2017=df_mention_2017[,c("from","to")] %>%
mutate(count = df_mention_2017$value) %>%
uncount(count) # this works
df_mention_2017$value = 1
table(df$type)
##
## initial quoted replied_to retweeted
## 2845 2006 5774 13745
length(which(is.na(df$mention)==FALSE))
## [1] 22323
df_solo = df[which(is.na(df$mention)==TRUE),] # tweets that didn't refer to anyone
df_interact = df[which(is.na(df$mention)==FALSE),] # mention, quote, replied, or retweet
edgelist_interact = df_interact[df_interact$created_at > "2017-01-03T06:00:00.000Z" & df_interact$created_at<= "2018-01-03T06:00:00.000Z",c("username", "referenced_username")] # single out the columns of referenced users AND the time
edgelist_interact = edgelist_interact[which(is.na(edgelist_interact$referenced_username)==FALSE),];nrow(edgelist_interact) # remove NAs from referenced tweets
## [1] 246
edgelist_interact$value = -1 # These are to be substracted from the default mention edgelist that will be created next
names(edgelist_interact) <- c("from","to","value")
edgelist_interact <- aggregate(edgelist_interact[,3], edgelist_interact[,-3], sum) # aggregation
edgelist_interact <- edgelist_interact[edgelist_interact$to%in%df_actor$name,] # only from and to actors
df_mention_raw = separate_rows(df_interact[df_interact$created_at > "2017-01-03T06:00:00.000Z" & df_interact$created_at <= "2018-01-03T06:00:00.000Z",],mention) # FILTER TIME and SEPARA ROWS
df_mention_gross = df_mention_raw[,c("username","mention")]
names(df_mention_gross) <- c("from","to")
df_mention_gross$value = 1
df_mention_gross <- aggregate(df_mention_gross[,3], df_mention_gross[,-3], sum)
df_mention_gross <- df_mention_gross[which(is.na(df_mention_gross$to)==FALSE),] # It doesn't really remove the blank cells. However, I kept it here out of caution.
df_mention_gross <- df_mention_gross[df_mention_gross$to%in%df_actor$name,]
df_mention_net = data.frame(rbind(df_mention_gross, edgelist_interact)) # the value column has 1s and -1s, need aggregate values to get the net mention list
df_mention = aggregate(df_mention_net[,3], df_mention_net[,-3], sum) # don't worry about the resultant negative values. These negative values indicate other types of ties
names(df_mention) <- c("from","to","value")
df_mention_2017 <- df_mention[which(df_mention$value>0),]
df_mention_2017 = df_mention_2017[rev(order(df_mention_2017$value)),]
df_mention_2017=df_mention_2017[,c("from","to")] %>%
mutate(count = df_mention_2017$value) %>%
uncount(count) # this works
# split by positions
df_mention_pro_2017 = df_mention_2017[df_mention_2017$from%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"],] # supporters
df_mention_pro_2017 = df_mention_pro_2017[df_mention_pro_2017$to%in%df_actor$name[df_actor$congress=="115"],]
df_mention_con_2017 = df_mention_2017[df_mention_2017$from%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="oppose"],] # opponents
df_mention_con_2017 = df_mention_con_2017[df_mention_con_2017$to%in%df_actor$name[df_actor$congress=="115"],]
rm(edgelist_interact);rm(df_mention_raw);rm(df_mention_gross);rm(df_mention_net);rm(df_mention)
head(df_mention_pro_2017)
## from to
## 9 iSquaredAct lauradfrancis
## 10 iSquaredAct lauradfrancis
## 11 iSquaredAct lauradfrancis
## 12 iSquaredAct lauradfrancis
## 13 iSquaredAct lauradfrancis
## 14 iSquaredAct lauradfrancis
head(df_mention_con_2017)
## [1] from to
## <0 rows> (or 0-length row.names)
mtn_pro_2017 = df_mention_pro_2017
names(mtn_pro_2017) <- c("from","to")
mtn_pro_2017 = mtn_pro_2017[mtn_pro_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
mtn_pro_2017$value = 1
#mtn_pro_2017 <- aggregate(mtn_pro_2017[,3], mtn_pro_2017[,-3], sum)
mtn_pro_2017 <- mtn_pro_2017[rev(order(mtn_pro_2017$value)),]
head(mtn_pro_2017)
## from to value
## 55 iSquaredAct cyrusmehta 1
## 48 iSquaredAct RepMikeCoffman 1
## 44 TheToddSchulte TheToddSchulte 1
## 42 iSquaredAct immivoice 1
## 41 iSquaredAct immivoice 1
## 36 iSquaredAct TheToddSchulte 1
g.mtn_pro_2017 = graph_from_data_frame(mtn_pro_2017)
E(g.mtn_pro_2017)$weight = mtn_pro_2017$value
vcount(g.mtn_pro_2017)
## [1] 6
ecount(g.mtn_pro_2017)
## [1] 14
centralization.degree(g.mtn_pro_2017, mode = "in", normalized = T)
## $res
## [1] 0 4 1 1 2 6
##
## $centralization
## [1] 0.7333333
##
## $theoretical_max
## [1] 30
centralization.degree(g.mtn_pro_2017, mode = "out", normalized = T)
## $res
## [1] 13 1 0 0 0 0
##
## $centralization
## [1] 2.133333
##
## $theoretical_max
## [1] 30
mean(degree(g.mtn_pro_2017, V(g.mtn_pro_2017),"all"))
## [1] 4.666667
ecount(g.mtn_pro_2017)/vcount(g.mtn_pro_2017)
## [1] 2.333333
ecount(g.mtn_pro_2017)/(vcount(g.mtn_pro_2017)*(vcount(g.mtn_pro_2017)-1))
## [1] 0.4666667
edge.betweenness.community(g.mtn_pro_2017, weights = E(g.mtn_pro_2017)$weight, directed = F)
## Warning in edge.betweenness.community(g.mtn_pro_2017, weights =
## E(g.mtn_pro_2017)$weight, : At community.c:460 :Membership vector will be
## selected based on the lowest modularity score.
## Warning in edge.betweenness.community(g.mtn_pro_2017, weights =
## E(g.mtn_pro_2017)$weight, : At community.c:467 :Modularity calculation with
## weighted edge betweenness community detection might not make sense -- modularity
## treats edge weights as similarities while edge betwenness treats them as
## distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "cyrusmehta" "RepMikeCoffman"
## [5] "immivoice" "lauradfrancis"
##
modularity_mtn_pro_2017 = modularity(g.mtn_pro_2017, cluster_edge_betweenness(g.mtn_pro_2017)$membership)
## Warning in cluster_edge_betweenness(g.mtn_pro_2017): At
## community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in cluster_edge_betweenness(g.mtn_pro_2017): At
## community.c:467 :Modularity calculation with weighted edge betweenness community
## detection might not make sense -- modularity treats edge weights as similarities
## while edge betwenness treats them as distances
transitivity(g.mtn_pro_2017)
## [1] 0
coreness(g.mtn_pro_2017)
## iSquaredAct TheToddSchulte cyrusmehta RepMikeCoffman immivoice
## 6 5 1 1 2
## lauradfrancis
## 6
reciprocity(g.mtn_pro_2017)
## [1] 0
# manual calculation of fragmentation
distance_matrix_mtn_pro_2017 <- matrix(data = NA, nrow = vcount(g.mtn_pro_2017),ncol = vcount(g.mtn_pro_2017))
for (i in 1:vcount(g.mtn_pro_2017))
{distance_matrix_mtn_pro_2017[i,]<-distances(g.mtn_pro_2017, v = V(g.mtn_pro_2017)[i], to = V(g.mtn_pro_2017))}
fragmentation_mtn_pro_2017 = length(which(distance_matrix_mtn_pro_2017=="Inf"))/(vcount(g.mtn_pro_2017)*(vcount(g.mtn_pro_2017)-1))
#dichotomous
indegree_mtn_pro_2017=centralization.degree(simplify(g.mtn_pro_2017), mode = "in", normalized = T)
outdegree_mtn_pro_2017=centralization.degree(simplify(g.mtn_pro_2017), mode = "out", normalized = T)
edge_density(simplify(g.mtn_pro_2017)) # native algorithym
## [1] 0.1666667
ecount(simplify(g.mtn_pro_2017))/(vcount(g.mtn_pro_2017)*(vcount(g.mtn_pro_2017)-1)) # manual calculation
## [1] 0.1666667
edge.betweenness.community(simplify(g.mtn_pro_2017), directed = F)
## Warning in edge.betweenness.community(simplify(g.mtn_pro_2017), directed =
## F): At community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in edge.betweenness.community(simplify(g.mtn_pro_2017), directed =
## F): At community.c:467 :Modularity calculation with weighted edge betweenness
## community detection might not make sense -- modularity treats edge weights as
## similarities while edge betwenness treats them as distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "cyrusmehta" "RepMikeCoffman"
## [5] "immivoice" "lauradfrancis"
##
transitivity(simplify(g.mtn_pro_2017))
## [1] 0
cohesion(simplify(g.mtn_pro_2017))
## [1] 0
graph.cohesion(simplify(g.mtn_pro_2017))
## [1] 0
reciprocity(simplify(g.mtn_pro_2017))
## [1] 0
par(mar=c(2,1,2,1))
V(g.mtn_pro_2017)$color = "orange"
V(g.mtn_pro_2017)[V(g.mtn_pro_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.mtn_pro_2017)$color <- alpha(tail_of(g.mtn_pro_2017,E(g.mtn_pro_2017))$color, 0.7)
V(g.mtn_pro_2017)$shape = "circle"
plot(g.mtn_pro_2017, vertex.size = degree(g.mtn_pro_2017, V(g.mtn_pro_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_mtn_pro_2017 <- c(vcount(g.mtn_pro_2017), ecount(g.mtn_pro_2017), mean(degree(g.mtn_pro_2017)), modularity_mtn_pro_2017,fragmentation_mtn_pro_2017,reciprocity(g.mtn_pro_2017), ecount(simplify(g.mtn_pro_2017)), indegree_mtn_pro_2017$centralization, outdegree_mtn_pro_2017$centralization,edge_density(simplify(g.mtn_pro_2017)), reciprocity(simplify(g.mtn_pro_2017)))
names(property_mtn_pro_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_mtn_pro_2017,2)
mtn_con_2017 = df_mention_con_2017
names(mtn_con_2017) <- c("from","to")
mtn_con_2017 = mtn_con_2017[mtn_con_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
mtn_con_2017$value = 1
#mtn_con_2017 <- aggregate(mtn_con_2017[,3], mtn_con_2017[,-3], sum)
mtn_con_2017 <- mtn_con_2017[rev(order(mtn_con_2017$value)),]
head(mtn_con_2017)
g.mtn_con_2017 = graph_from_data_frame(mtn_con_2017)
E(g.mtn_con_2017)$weight = mtn_con_2017$value
vcount(g.mtn_con_2017)
ecount(g.mtn_con_2017)
centralization.degree(g.mtn_con_2017, mode = "in", normalized = T)
centralization.degree(g.mtn_con_2017, mode = "out", normalized = T)
mean(degree(g.mtn_con_2017, V(g.mtn_con_2017),"all"))
ecount(g.mtn_con_2017)/vcount(g.mtn_con_2017)
ecount(g.mtn_con_2017)/(vcount(g.mtn_con_2017)*(vcount(g.mtn_con_2017)-1))
edge.betweenness.community(g.mtn_con_2017, weights = E(g.mtn_con_2017)$weight, directed = F)
modularity_mtn_con_2017 = modularity(g.mtn_con_2017, cluster_edge_betweenness(g.mtn_con_2017)$membership)
transitivity(g.mtn_con_2017)
coreness(g.mtn_con_2017)
reciprocity(g.mtn_con_2017)
# manual calculation of fragmentation
distance_matrix_mtn_con_2017 <- matrix(data = NA, nrow = vcount(g.mtn_con_2017),ncol = vcount(g.mtn_con_2017))
for (i in 1:vcount(g.mtn_con_2017))
{distance_matrix_mtn_con_2017[i,]<-distances(g.mtn_con_2017, v = V(g.mtn_con_2017)[i], to = V(g.mtn_con_2017))}
fragmentation_mtn_con_2017 = length(which(distance_matrix_mtn_con_2017=="Inf"))/(vcount(g.mtn_con_2017)*(vcount(g.mtn_con_2017)-1))
#dichotomous
indegree_mtn_con_2017=centralization.degree(simplify(g.mtn_con_2017), mode = "in", normalized = T)
outdegree_mtn_con_2017=centralization.degree(simplify(g.mtn_con_2017), mode = "out", normalized = T)
edge_density(simplify(g.mtn_con_2017)) # native algorithym
ecount(simplify(g.mtn_con_2017))/(vcount(g.mtn_con_2017)*(vcount(g.mtn_con_2017)-1)) # manual calculation
edge.betweenness.community(simplify(g.mtn_con_2017), directed = F)
transitivity(simplify(g.mtn_con_2017))
cohesion(simplify(g.mtn_con_2017))
graph.cohesion(simplify(g.mtn_con_2017))
reciprocity(simplify(g.mtn_con_2017))
par(mar=c(2,1,2,1))
V(g.mtn_con_2017)$color = "orange"
V(g.mtn_con_2017)[V(g.mtn_con_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.mtn_con_2017)$color <- alpha(tail_of(g.mtn_con_2017,E(g.mtn_con_2017))$color, 0.7)
V(g.mtn_con_2017)$shape = "circle"
plot(g.mtn_con_2017, vertex.size = degree(g.mtn_con_2017, V(g.mtn_con_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_mtn_con_2017 <- c(vcount(g.mtn_con_2017), ecount(g.mtn_con_2017), mean(degree(g.mtn_con_2017)), modularity_mtn_con_2017,fragmentation_mtn_con_2017,reciprocity(g.mtn_con_2017), ecount(simplify(g.mtn_con_2017)), indegree_mtn_con_2017$centralization, outdegree_mtn_con_2017$centralization,edge_density(simplify(g.mtn_con_2017)), reciprocity(simplify(g.mtn_con_2017)))
names(property_mtn_con_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#round(property_mtn_con_2017,2)
mtn_2017 = df_mention_2017
names(mtn_2017) <- c("from","to")
mtn_2017 = mtn_2017[mtn_2017$to%in%df_actor$name[df_actor$congress=="115"],] # the recipients can be enemies, as long as they are policy actors.
mtn_2017$value = 1
#mtn_2017 <- aggregate(mtn_2017[,3], mtn_2017[,-3], sum)
mtn_2017 <- mtn_2017[rev(order(mtn_2017$value)),]
head(mtn_2017)
## from to value
## 55 iSquaredAct cyrusmehta 1
## 48 iSquaredAct RepMikeCoffman 1
## 44 TheToddSchulte TheToddSchulte 1
## 42 iSquaredAct immivoice 1
## 41 iSquaredAct immivoice 1
## 36 iSquaredAct TheToddSchulte 1
g.mtn_2017 = graph_from_data_frame(mtn_2017)
E(g.mtn_2017)$weight = mtn_2017$value
vcount(g.mtn_2017)
## [1] 6
ecount(g.mtn_2017)
## [1] 14
centralization.degree(g.mtn_2017, mode = "in", normalized = T)
## $res
## [1] 0 4 1 1 2 6
##
## $centralization
## [1] 0.7333333
##
## $theoretical_max
## [1] 30
centralization.degree(g.mtn_2017, mode = "out", normalized = T)
## $res
## [1] 13 1 0 0 0 0
##
## $centralization
## [1] 2.133333
##
## $theoretical_max
## [1] 30
mean(degree(g.mtn_2017, V(g.mtn_2017),"all"))
## [1] 4.666667
ecount(g.mtn_2017)/vcount(g.mtn_2017)
## [1] 2.333333
ecount(g.mtn_2017)/(vcount(g.mtn_2017)*(vcount(g.mtn_2017)-1))
## [1] 0.4666667
edge.betweenness.community(g.mtn_2017, weights = E(g.mtn_2017)$weight, directed = F)
## Warning in edge.betweenness.community(g.mtn_2017, weights =
## E(g.mtn_2017)$weight, : At community.c:460 :Membership vector will be selected
## based on the lowest modularity score.
## Warning in edge.betweenness.community(g.mtn_2017, weights =
## E(g.mtn_2017)$weight, : At community.c:467 :Modularity calculation with weighted
## edge betweenness community detection might not make sense -- modularity treats
## edge weights as similarities while edge betwenness treats them as distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "cyrusmehta" "RepMikeCoffman"
## [5] "immivoice" "lauradfrancis"
##
modularity_mtn_2017 = modularity(g.mtn_2017, cluster_edge_betweenness(g.mtn_2017)$membership)
## Warning in cluster_edge_betweenness(g.mtn_2017): At community.c:460 :Membership
## vector will be selected based on the lowest modularity score.
## Warning in cluster_edge_betweenness(g.mtn_2017): At community.c:467 :Modularity
## calculation with weighted edge betweenness community detection might not make
## sense -- modularity treats edge weights as similarities while edge betwenness
## treats them as distances
transitivity(g.mtn_2017)
## [1] 0
coreness(g.mtn_2017)
## iSquaredAct TheToddSchulte cyrusmehta RepMikeCoffman immivoice
## 6 5 1 1 2
## lauradfrancis
## 6
reciprocity(g.mtn_2017)
## [1] 0
# manual calculation of fragmentation
distance_matrix_mtn_2017 <- matrix(data = NA, nrow = vcount(g.mtn_2017),ncol = vcount(g.mtn_2017))
for (i in 1:vcount(g.mtn_2017))
{distance_matrix_mtn_2017[i,]<-distances(g.mtn_2017, v = V(g.mtn_2017)[i], to = V(g.mtn_2017))}
fragmentation_mtn_2017 = length(which(distance_matrix_mtn_2017=="Inf"))/(vcount(g.mtn_2017)*(vcount(g.mtn_2017)-1))
#dichotomous
indegree_mtn_2017=centralization.degree(simplify(g.mtn_2017), mode = "in", normalized = T)
outdegree_mtn_2017=centralization.degree(simplify(g.mtn_2017), mode = "out", normalized = T)
edge_density(simplify(g.mtn_2017)) # native algorithym
## [1] 0.1666667
ecount(simplify(g.mtn_2017))/(vcount(g.mtn_2017)*(vcount(g.mtn_2017)-1)) # manual calculation
## [1] 0.1666667
edge.betweenness.community(simplify(g.mtn_2017), directed = F)
## Warning in edge.betweenness.community(simplify(g.mtn_2017), directed = F):
## At community.c:460 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in edge.betweenness.community(simplify(g.mtn_2017), directed = F): At
## community.c:467 :Modularity calculation with weighted edge betweenness community
## detection might not make sense -- modularity treats edge weights as similarities
## while edge betwenness treats them as distances
## IGRAPH clustering edge betweenness, groups: 1, mod: 0
## + groups:
## $`1`
## [1] "iSquaredAct" "TheToddSchulte" "cyrusmehta" "RepMikeCoffman"
## [5] "immivoice" "lauradfrancis"
##
transitivity(simplify(g.mtn_2017))
## [1] 0
cohesion(simplify(g.mtn_2017))
## [1] 0
graph.cohesion(simplify(g.mtn_2017))
## [1] 0
reciprocity(simplify(g.mtn_2017))
## [1] 0
par(mar=c(2,1,2,1))
V(g.mtn_2017)$color = "orange"
V(g.mtn_2017)[V(g.mtn_2017)$name%in%df_actor$name[df_actor$congress=="115" & df_actor$position=="support"]]$color="steelblue"
E(g.mtn_2017)$color <- alpha(tail_of(g.mtn_2017,E(g.mtn_2017))$color, 0.7)
V(g.mtn_2017)$shape = "circle"
plot(g.mtn_2017, vertex.size = degree(g.mtn_2017, V(g.mtn_2017),"in"), edge.arrow.size = .5, vertex.label.color = "black", vertex.frame.color = "gray60")
## Organize Network Properties
property_mtn_2017 <- c(vcount(g.mtn_2017), ecount(g.mtn_2017), mean(degree(g.mtn_2017)), modularity_mtn_2017,fragmentation_mtn_2017,reciprocity(g.mtn_2017), ecount(simplify(g.mtn_2017)), indegree_mtn_2017$centralization, outdegree_mtn_2017$centralization,edge_density(simplify(g.mtn_2017)), reciprocity(simplify(g.mtn_2017)))
names(property_mtn_2017) <-c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
round(property_mtn_2017,2)
## node count edge count (raw)
## 6.00 14.00
## average degree (weighted density) modularity
## 4.67 0.00
## fragmentation reciprocity (weighted)
## 0.00 0.00
## edge count (dichotomized) indegree centralization (dichotomized)
## 5.00 0.03
## outdegree centralization (dichotomized) density (dichotomized)
## 0.83 0.17
## reciprocity (dichotomized)
## 0.00
property_mtn_all_2017 = data.frame(rbind(property_mtn_pro_2017,property_mtn_2017))
#property_mtn_all_2017 = data.frame(rbind(property_mtn_pro_2017, property_mtn_con_2017, property_mtn_2017))
names(property_mtn_all_2017) <- c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
#property_mtn_all_2017
library(DT)
library(knitr)
property_2017 <- data.frame(rbind(property_mtn_all_2017, property_rt_all_2017, property_reply_all_2017, property_quote_all_2017))
names(property_2017) <- c("node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")
rownames(property_2017) <- gsub("property_","",rownames(property_2017))
#datatable(round(property_2017,3))
kable(round(property_2017,3))
node count | edge count (raw) | average degree (weighted density) | modularity | fragmentation | reciprocity (weighted) | edge count (dichotomized) | indegree centralization (dichotomized) | outdegree centralization (dichotomized) | density (dichotomized) | reciprocity (dichotomized) | |
---|---|---|---|---|---|---|---|---|---|---|---|
mtn_pro_2017 | 6 | 14 | 4.667 | 0 | 0 | 0.000 | 5 | 0.033 | 0.833 | 0.167 | 0.000 |
mtn_2017 | 6 | 14 | 4.667 | 0 | 0 | 0.000 | 5 | 0.033 | 0.833 | 0.167 | 0.000 |
rt_pro_2017 | 6 | 39 | 13.000 | 0 | 0 | 0.053 | 9 | 0.500 | 0.300 | 0.300 | 0.222 |
rt_2017 | 6 | 39 | 13.000 | 0 | 0 | 0.053 | 9 | 0.500 | 0.300 | 0.300 | 0.222 |
reply_pro_2017 | 3 | 2 | 1.333 | 0 | 0 | 0.000 | 2 | 0.167 | 0.167 | 0.333 | 0.000 |
reply_2017 | 3 | 2 | 1.333 | 0 | 0 | 0.000 | 2 | 0.167 | 0.167 | 0.333 | 0.000 |
quote_pro_2017 | 1 | 1 | 2.000 | 0 | NaN | NaN | 0 | NaN | NaN | NaN | NaN |
quote_2017 | 1 | 1 | 2.000 | 0 | NaN | NaN | 0 | NaN | NaN | NaN | NaN |
write.csv(round(property_2017,3),"property_2017.csv", row.names = T)
library(writexl)
property_2017$name <- gsub("property_","",rownames(property_2017)) # the next command won't include row names, then add a temporary column
property_2017 <- property_2017[,c("name","node count","edge count (raw)","average degree (weighted density)","modularity", "fragmentation", "reciprocity (weighted)","edge count (dichotomized)", "indegree centralization (dichotomized)", "outdegree centralization (dichotomized)", "density (dichotomized)","reciprocity (dichotomized)")]
write_xlsx(list("property_2017" = property_2017, "mtn_2017" = mtn_2017, "rt_2017" = rt_2017, "reply_2017" = reply_2017, "quote_2017" = quote_2017),"actor_net_2017.xlsx") # round the numeric columns
property_2017 <- property_2017[!colnames(property_2017)%in%"name"] # drop the temporary column
2021-12-06 00:00:21