knitr::opts_chunk$set(echo = TRUE)
This tutorial aims to demonstrate how to perform the fundamental analyses in EFAshiny
using R code. By following each line in this tutorial, you can learn basic R programming and can implement automated processing script in future analyses. Also, you can further use the code in EFAshiny
within a script pupeline without launching the app. However, we still suggest you to use EFAshiny
APP :)) You can also have a look at our tutorial of EFAshiny
GUI to make an easy comparison while writing the code. Have fun with EFAshiny
and R
!
First of all, we should load all the packages and fucntions in EFAshiny
into R
session. If you do not have these package installed, please use install.package("packagename")
to perfrom installations.
require(ggplot2);require(psych);require(corrplot);require(reshape2);require(moments);require(gridExtra) require(qgraph);require(bootnet);require(igraph);require(ggcorrplot);require(RCurl) source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/my_summary.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/faplot.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/bootEGA.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/bargraph.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/stackbar.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/printLoadings.R") source("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/inst/efas/functions/theme_default.R")
Read the demonsration data RSE
into R
. We assign it to a object called dta
. Then using head
to observe and explore this data. See our tutorial for data description.
dta <- read.csv(text=getURL("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/RSE/RSE.csv")) head(dta)
Several kinds of data summary can be perform in EFAshiny
. We demonstrate them using basic code. The package ggplot2
is a useful package for plotting. We use it to plot the histograms, the density plots, and the correlation matrix. You can also check the documentation of ggplot2
.
NumericStatistic <- apply(dta,2,my_summary) row.names(NumericStatistic) <- c("Mean","SD","Skewness","Kurtosis","Median","MAD") NumericStatistic <- as.data.frame(t(NumericStatistic)) NumericStatistic <- round(NumericStatistic,3) NumericStatistic
dta_long <- melt(dta) colnames(dta_long) <- c("Item", "Response") Histogram <- ggplot(dta_long, aes(x = Response, fill = Item))+ geom_histogram(bins = 10)+ facet_wrap(~Item)+ theme_default() Histogram
DensityPlot <- ggplot(dta_long, aes(x = Response, fill = Item))+ geom_density()+ facet_wrap(~Item)+ theme_default() DensityPlot
CorMat <- cor(as.matrix(dta)) corrplot(CorMat,order="hclust",type="upper",method="ellipse", tl.pos = "lt",mar = c(2,2,2,2)) corrplot(CorMat,order="hclust",type="lower",method="number", diag=FALSE,tl.pos="n", cl.pos="n",add=TRUE,mar = c(2,2,2,2))
ggcorrplot(CorMat, hc.order = T,type = "lower", lab = TRUE, colors = c("#E46726", "white", "#6D9EC2"))
We provide several factor retention methods in EFAshiny
. Those analyses can also be performed using R code. You can adopt these methods for your own use.
PA <- faplot(CorMat,n.obs = 256, quant = 0.95) PA[[1]]
NumericRule <- VSS(CorMat,n = 4, plot = F, n.obs = 256) temp1 <- data.frame(nFactor = row.names(NumericRule$vss.stats), VSS1 = NumericRule$cfit.1, VSS2 = NumericRule$cfit.2, MAP = NumericRule$map) temp2 <- NumericRule$vss.stats[,c(6:8,11)] NumericRule <- cbind(temp1,temp2) NumericRule
EGArst <- bootEGA(data = dta, n = 10, medianStructure = TRUE, plot.MedianStructure = TRUE, ncores = 4, layout = "spring") plot(EGArst$plot)
The key analyses in EFA is factor extarction and rotation. We teach you to perform the analyses using psych
package. Using these lines, you can obtain basic results of EFA. Can also see the code in EFAshiny
server and psych
package for details.
EFArst <- fa(CorMat,2,n.obs=256, rotate = "promax",fm = "pa", n.iter = 200) EFArst
By passing the results of EFA from aforementioned analyses into the well-established functions, you can easily visualize these results from EFA. Definitely, EFAshiny
can help you to do these automatically.
fa.diagram(EFArst,simple = T,cut = 0.33, sort = T,errors = T,e.size = 0.05)
order <- rev(row.names(as.data.frame(printLoadings(EFArst$cis$means,sort = T,cutoff = 0)))) # define the order of the variable bargraph(EFArst,order = order,nf = 2,highcol = "firebrick",lowcol = "chartreuse4",ci = T)
stackbar(CorMat,EFArst,order = order,highcol = "firebrick",lowcol = "chartreuse4")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.