-
Notifications
You must be signed in to change notification settings - Fork 13
/
EFAshiny_RCodeDemo.Rmd
135 lines (114 loc) · 5.93 KB
/
EFAshiny_RCodeDemo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
title: "EFAshiny : R Code Demo"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Overview
This tutorial aims to demonstrate how to perform the fundamental analyses in [`EFAshiny`](https://psychilin.shinyapps.io/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`](https://psychilin.shinyapps.io/EFAshiny/) APP :)) You can also have a look at our [tutorial](https://github.com/PsyChiLin/EFAshiny) of `EFAshiny` GUI to make an easy comparison while writing the code. Have fun with [`EFAshiny`](https://psychilin.shinyapps.io/EFAshiny/) and `R` !
### Preparation
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.
```{r, message=FALSE, warning=FALSE}
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")
```
### Data Input
Read the demonsration data [`RSE`](https://github.com/PsyChiLin/EFAshiny/blob/master/RSE/RSE.csv) into `R`. We assign it to a object called `dta`. Then using `head` to observe and explore this data. See our [tutorial](https://github.com/PsyChiLin/EFAshiny) for data description.
```{r}
dta <- read.csv(text=getURL("https://raw.githubusercontent.com/PsyChiLin/EFAshiny/master/RSE/RSE.csv"))
head(dta)
```
### Data Summary
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`](https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf).
##### Numeric Statistic
```{r}
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
```
##### Histogram
```{r, message=FALSE}
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
```
##### Density Plot
```{r, message=FALSE}
DensityPlot <- ggplot(dta_long, aes(x = Response, fill = Item))+
geom_density()+
facet_wrap(~Item)+
theme_default()
DensityPlot
```
##### Correlation Matrix
```{r, fig.height=7, fig.width=7}
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))
```
##### ggcorplot
```{r}
ggcorrplot(CorMat, hc.order = T,type = "lower", lab = TRUE,
colors = c("#E46726", "white", "#6D9EC2"))
```
### Factor Retention
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.
##### Scree Plot and Parallel Analysis
```{r, message=FALSE}
PA <- faplot(CorMat,n.obs = 256, quant = 0.95)
PA[[1]]
```
##### Numeric Rules
```{r}
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
```
##### Exploratory Graph Analysis (EGA)
```{r}
EGArst <- bootEGA(data = dta, n = 10, medianStructure = TRUE, plot.MedianStructure = TRUE,
ncores = 4, layout = "spring")
plot(EGArst$plot)
```
### Extarction and Rotation
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.
```{r}
EFArst <- fa(CorMat,2,n.obs=256, rotate = "promax",fm = "pa", n.iter = 200)
EFArst
```
### Visualization
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.
##### Diagram
```{r,message=FALSE}
fa.diagram(EFArst,simple = T,cut = 0.33,
sort = T,errors = T,e.size = 0.05)
```
##### Bootstrapping Factor Loadings
```{r,message=FALSE}
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)
```
##### Factor Loadings and Correlation Matrix
```{r,message=FALSE}
stackbar(CorMat,EFArst,order = order,highcol = "firebrick",lowcol = "chartreuse4")
```