Skip to content

Instantly share code, notes, and snippets.

@Amice13
Last active January 5, 2022 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amice13/c4dec4464383b903d861bb50a6e2bdcd to your computer and use it in GitHub Desktop.
Save Amice13/c4dec4464383b903d861bb50a6e2bdcd to your computer and use it in GitHub Desktop.
Visualization of statistical hypothesis testing by Ruslana Moskotina (Refactoring)
# Include necessary libraries
library(foreign)
library(igraph)
library(ggraph)
# Download data here
# https://www.europeansocialsurvey.org/download.html?file=ESS6UA&c=UA&y=2012
dataESS <- read.spss('ESS6UA.sav', to.data.frame = T)
# Check if person is religious
rlgblg <- dataESS$rlgblg
levels(rlgblg) <- c('Так', 'Ні')
# Recode happiness variable
happy <- cut(as.numeric(dataESS$happy), c(0, 4, 7, 11), c('Ні','Нрм','Так'))
# Recode gender
gndr <- dataESS$gndr
levels(gndr) <- c('Ч', 'Ж')
# Tendency to help
help <- as.numeric(dataESS$prhlppl) - 1
# Create groups
group <- paste(rlgblg, happy, gndr, sep = ".")
group[grepl('NA', group)] <- NA
level <- names(sort(tapply(help, group, mean, na.rm = T), decreasing = T))
group <- factor(group, levels = level)
# Kruskal Test
kruskal.test(help ~ group)
# Pairwise testing
p_val <- pairwise.wilcox.test(help, group,
p.adjust.method = 'BH',
exact = F) [['p.value']]
# Create a directed distance matrix (to make a directed graph)
dim = nrow(p_val) + 1
graph_m <- matrix(rep(0, dim^2), nrow = dim)
graph_m[upper.tri(graph_m)] <- t(p_val)[!lower.tri(p_val)]
colnames(graph_m) <- c(colnames(p_val)[1], rownames(p_val))
l <- layout_(net, as_star())
layout_with_kk(net, weights = E(net)$weight)
layout_with_fr(net, weights = E(net)$weight)
# Create a network
net <- graph_from_adjacency_matrix(graph_m, weighted = T)
ggraph(net, layout = layout_with_fr(net, weights = E(net)$weight)) +
geom_edge_link(aes(alpha = weight, width=weight)) +
scale_edge_width(range = c(0.01, 0.5)) +
geom_node_point(size = 4, color = color) +
geom_node_text(aes(label = level),
size = 3.2,
fontface = 'bold',
) +
xlab('') +
ylab('') +
theme(legend.position = 'none')
# Include necessary libraries
library(foreign)
library(igraph)
library(ggraph)
# Download data here
# https://www.europeansocialsurvey.org/download.html?file=ESS6UA&c=UA&y=2012
dataESS <- read.spss('ESS6UA.sav', to.data.frame = T)
# Check if person is religious
rlgblg <- dataESS$rlgblg
levels(rlgblg) <- c('Так', 'Ні')
# Recode happiness variable
happy <- cut(as.numeric(dataESS$happy), c(0, 4, 7, 11), c('Ні','Нрм','Так'))
# Recode gender
gndr <- dataESS$gndr
levels(gndr) <- c('Ч', 'Ж')
# Tendency to help
help <- as.numeric(dataESS$prhlppl) - 1
# Create groups
group <- paste(rlgblg, happy, gndr, sep = ".")
group[grepl('NA', group)] <- NA
level <- names(sort(tapply(help, group, mean, na.rm = T), decreasing = T))
group <- factor(group, levels = level)
# Kruskal Test
kruskal.test(help ~ group)
# Pairwise testing
p_val <- pairwise.wilcox.test(help, group,
p.adjust.method = 'BH',
exact = F) [['p.value']]
# Save only significant differences
p_val <- apply(p_val, 1:2, function(x) { sum(x <= 0.05) })
# Create a directed distance matrix (to make a directed graph)
dim = nrow(p_val) + 1
graph_m <- matrix(rep(0, dim^2), nrow = dim)
graph_m[upper.tri(graph_m)] <- t(p_val)[!lower.tri(p_val)]
colnames(graph_m) <- c(colnames(p_val)[1], rownames(p_val))
# Create a network
net <- graph_from_adjacency_matrix(graph_m)
# Create a symmetric matrix for MDS
mds_m <- matrix(rep(0, dim^2), nrow = dim)
mds_m[lower.tri(mds_m)] <- p_val[!upper.tri(p_val)]
mds_m[upper.tri(mds_m)] <- t(mds_m)[upper.tri(mds_m)]
# Create MDS layout
l <- layout_with_mds(net, dist = mds_m)
l[,c(1, 2)] <- l[,c(2, 1)]
# Get degrees
in_degree <- degree(net, mode = 'in')
out_degree <- degree(net, mode = 'out')
# Define graph colors
color <- rep('gold', 12)
color[in_degree == 0] <- 'deepskyblue2'
color[out_degree == 0] <- 'coral2'
# Grouping of edges
edges <- as_edgelist(net)
col_from <- rep('gold', gsize(net))
col_from[is.na(match(edges[, 1], edges[, 2]))] <- 'deepskyblue2'
col_to <- rep('gold', gsize(net))
col_to[is.na(match(edges[, 2], edges[, 1]))] <- 'coral2'
edge_gr <- rep('03', gsize(net))
edge_gr[col_from == 'deepskyblue2' & col_to == 'gold'] <- '01'
edge_gr[col_from == 'deepskyblue2' & col_to == 'coral2'] <- '02'
# Plot finally
ggraph(net, layout = l) +
theme_gray() +
xlim(-0.75, 0.5) +
ylim(-0.5, 0.75) +
geom_edge_arc(aes(colour = edge_gr),
strength = c(rep(0, 24), 0.59, 0, 0, 0),
show.legend = F,
arrow = arrow(length = unit(0.1, 'inches')),
end_cap = circle(3.5, 'mm'),
start_cap = circle(3.5, 'mm')) +
scale_edge_colour_manual(values = c('green3', 'purple3', 'orange')) +
geom_node_point(size = 4,
color = color) +
geom_node_text(aes(label = level),
size = 3.2,
fontface = 'bold',
position = position_nudge(x = c(0, 0, 0.03, 0.05, 0, 0.01, -0.02, -0.08, 0.04, -0.04, 0, 0),
y = c(rep(0.05, 4), 0.01, -0.04, -0.04, 0, rep(-0.05, 4)))) +
xlab('') +
ylab('Менша схильність допомагати - Більша схильність допомагати')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment