Commit 154563e1 authored by numeroteca's avatar numeroteca
Browse files

first approach to Network Agenda Setting calculation

parent 75b4fd29
# Network Agenda Setting
# ======================
# The goal is to analyse the relationship among answers in baromteres
# if one person answers this to the most important 3 problems:
# 1. La corrupción y el fraude
# 2. Crisis política
# 3. El paro
# We sill stablish links among the issues: 1-2, 2-3, 3-1
# A. Load libraries -------------
library(tidyverse)
library(igraph)
# new libraries
library(tidygraph)
library(ggraph)
# B. Load cleaned data ----
barometros <- read.delim("data/output/barometros-microdatos-ccaa-3problems_clean.csv",sep = ",") %>% mutate(
date = as.Date(date),
CCAA = as.factor(CCAA),
PROV = as.factor(PROV),
MUN = as.factor(MUN),
# p1 = as.factor(p1),
# p2 = as.factor(p2),
# p3 = as.factor(p3)
p1 = ifelse( p1 == "N.C." | p1 == "N.S.", "NS/NC", p1),
p2 = ifelse( p2 == "N.C." | p2 == "N.S.", "NS/NC", p2),
p3 = ifelse( p3 == "N.C." | p3 == "N.S.", "NS/NC", p3),
# algunas p1 estaban vacías de respuestas
# les asigno NS/NC (no sabe no contesta) para facilitar el cálculo
p1 = ifelse( is.na(p1), "NS/NC",p1)
)
# C.1 Calculate network for 1 barometer ------------
# We iterate through all the answers by each person
# Create list of links:
study <- 2978
links1 <- barometros %>% filter( ESTU == study) %>% summarise(
source = p1,
target = p2
)
links2 <- barometros %>% filter( ESTU == study) %>% summarise(
source = p2,
target = p3
) %>% filter( source != "NS/NC" & target != "NS/NC" )
links3 <- barometros %>% filter( ESTU == study) %>% summarise(
source = p3,
target = p1
) %>% filter( source != "NS/NC" & target != "NS/NC" )
links <- rbind(links1, links2, links3)
# create the network object
network <- graph_from_data_frame(d=links, directed=F)
plot(network)
# using ggraph
# https://www.jessesadler.com/post/network-analysis-with-r/
# create the appropriate format
routes_tidy <- as_tbl_graph(network)
# plot 1
ggraph(routes_igraph_tidy) + geom_edge_link() + geom_node_point() + theme_graph()
# plot 2
ggraph(routes_tidy, layout = "graphopt") +
geom_node_point() +
# geom_edge_link(aes(width = weight), alpha = 0.8) +
geom_edge_link( alpha = 0.8) +
scale_edge_width(range = c(0.2, 2)) +
# geom_node_text(aes(label = label), repel = TRUE) +
labs(edge_width = "Letters") +
theme_graph()
# C.2 Calculate for all the barometers ------------
links1 <- barometros %>% summarise(
source = p1,
target = p2
)
links2 <- barometros %>% summarise(
source = p2,
target = p3
) %>% filter( source != "NS/NC" & target != "NS/NC" )
links3 <- barometros %>% summarise(
source = p3,
target = p1
) %>% filter( source != "NS/NC" & target != "NS/NC" )
links <- rbind(links1, links2, links3)
# create the network object
# subset or this gests crazy!
network <- graph_from_data_frame(d=links %>% head(10000), directed=F)
plot(network)
ggraph(network) + geom_edge_link() + geom_node_point() + theme_graph()
#
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment