This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(threejs)
## Warning: package 'threejs' was built under R version 3.6.3
## Loading required package: igraph
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(networkD3)
## Warning: package 'networkD3' was built under R version 3.6.3
library(readxl)
library(igraph)
library(IsingFit)
## Registered S3 methods overwritten by 'huge':
## method from
## plot.sim BDgraph
## print.sim BDgraph
library(qgraph)
library(bootnet)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.2
## Registered S3 method overwritten by 'R.oo':
## method from
## throw.default R.methodsS3
## Registered S3 methods overwritten by 'car':
## method from
## influence.merMod lme4
## cooks.distance.influence.merMod lme4
## dfbeta.influence.merMod lme4
## dfbetas.influence.merMod lme4
## This is bootnet 1.2.4
## For questions and issues, please see github.com/SachaEpskamp/bootnet.
library(NetworkComparisonTest)
library(mgm)
## This is mgm 1.2-7
## Please report issues on Github: https://github.com/jmbh/mgm/issues
library(foreign)
########################################################## Load Data ############################################################
setwd("C:/Users/glowman/OneDrive - Kennesaw State University/1. Research/1. Projects/35. PNA Projects/Leadership/2. Data/1. Data Sets")
data1 <- read_excel("Ideal Leader Survey_Combined Gender Analysis.xlsx")
######################################################## Generate Graphs ########################################################
##Select Variables for Analysis (Q3=Ideal; Q5=Actual)
data1 <- data1 %>% dplyr::select(Q3.1_1:Q3.1_21, Gender, Sample)
names(data1) =
c("Helpful", "Understanding", "Sincere","Intelligent","Educated","Clever","Knowledgeable","Dedicated","Motivated",
"Hard-working","Energetic","Strong","Dynamic","Domineering","Pushy","Manipulative","Loud","Conceited","Selfish","Male","Masculine", "Gender", "Sample")
#Students = 1, MTurk = 2, Qualtrics = 3#
full <- subset(data1) %>% dplyr::select(1:21)
students <- subset(data1, Sample == "1") %>% dplyr::select(1:21)
mturk <- subset(data1, Sample == "2") %>% dplyr::select(1:21)
qualtrics <- subset(data1, Sample == "3") %>% dplyr::select(1:21)
##Full Sample
dataWeiAdj <- EBICglasso(cor_auto(full), nrow(full), threshold = TRUE)
## Variables detected as ordinal: Helpful; Understanding; Sincere; Intelligent; Educated; Clever; Knowledgeable; Dedicated; Motivated; Hard-working; Energetic; Strong; Dynamic; Domineering; Pushy; Manipulative; Loud; Conceited; Selfish; Male; Masculine
## Note: Network with lowest lambda selected as best network: assumption of sparsity might be violated.
dataqGraph <- qgraph(dataWeiAdj)
dataiGraph <- graph_from_adjacency_matrix(abs(dataWeiAdj), 'undirected', weighted = TRUE, add.colnames = TRUE)
##Student Sample##
data1WeiAdj <- EBICglasso(cor_auto(students), nrow(students), threshold = TRUE)
## Variables detected as ordinal: Helpful; Understanding; Sincere; Intelligent; Educated; Clever; Knowledgeable; Dedicated; Motivated; Hard-working; Energetic; Strong; Dynamic; Domineering; Pushy; Manipulative; Loud; Conceited; Selfish; Male; Masculine
data1qGraph <- qgraph(data1WeiAdj)
data1iGraph <- graph_from_adjacency_matrix(abs(data1WeiAdj), 'undirected', weighted = TRUE, add.colnames = TRUE)
##MTurk Sample##
data2WeiAdj <- EBICglasso(cor_auto(mturk), nrow(mturk), threshold = TRUE)
## Variables detected as ordinal: Helpful; Understanding; Sincere; Intelligent; Educated; Clever; Knowledgeable; Dedicated; Motivated; Hard-working; Energetic; Strong; Dynamic; Domineering; Pushy; Manipulative; Loud; Conceited; Selfish; Male; Masculine
data2qGraph <- qgraph(data2WeiAdj)
data2iGraph <- graph_from_adjacency_matrix(abs(data2WeiAdj), 'undirected', weighted = TRUE, add.colnames = TRUE)
##Qualtrics Sample##
data3WeiAdj <- EBICglasso(cor_auto(qualtrics), nrow(qualtrics), threshold = TRUE)
## Variables detected as ordinal: Helpful; Understanding; Sincere; Intelligent; Educated; Clever; Knowledgeable; Dedicated; Motivated; Hard-working; Energetic; Strong; Dynamic; Domineering; Pushy; Manipulative; Loud; Conceited; Selfish; Male; Masculine
data3qGraph <- qgraph(data3WeiAdj)
data3iGraph <- graph_from_adjacency_matrix(abs(data3WeiAdj), 'undirected', weighted = TRUE, add.colnames = FALSE)
######################################################## Generate Graph Information ########################################################
##Set Names for Nodes
Names <- scan("Names_age.txt",what = "character", sep = "\n")
##Paste Network to Graph
net <- data1iGraph
check <- data1WeiAdj
##Create communities
cluster <- cluster_walktrap(net)
##Set color of nodes match clusters
cluster_colors <- c("lightsalmon1","darkseagreen2","lightyellow2","deepskyblue2","mediumpurple3", "pink1", "coral1")
net <- set_vertex_attr(net, "color", value=cluster_colors [cluster$membership])
##Set node size based on centrality estimate (betweenness, closeness, strength)
sz <- closeness(net, normalized=T)
#sz <- betweenness(net, normalized=T,directed=F)
#sz <- strength(net)
V(net)$size <- 8*(sz/max(sz)) #sizing nodes by closeness
#E(net)$color <- "#e6e6e6" #change edge color
##Create Graph
set.seed(123)
graphjs(net, vertex.label = Names, bg="black", brush = TRUE, main = "Psychological Network of the Ideal Leader")
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.