This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallelpretty.R
66 lines (57 loc) · 2.69 KB
/
parallelpretty.R
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
#This code was taken and adapted from the blog of jksakaluk. Citation and links given in README.md
parallel = fa.parallel(hc$correlations,n.obs=N,
#fm = 'ml',
fa = 'fa',
n.iter = 50,
#SMC = TRUE,
quant = .95)
#Create data frame obs from observed eigenvalue data
obs = data.frame(parallel$fa.values)
obs$type = c('Observed Data')
obs$num = c(row.names(obs))
obs$num = as.numeric(obs$num)
colnames(obs) = c('eigenvalue', 'type', 'num')
#Calculate quantiles for eigenvalues, but only store those from simulated CF model in percentile1
percentile = apply(parallel$values,2,function(x) quantile(x,.95))
min = as.numeric(nrow(obs))
min = (4*min) - (min-1)
max = as.numeric(nrow(obs))
max = 4*max
percentile1 = percentile[min:max]
#Create data frame called sim with simulated eigenvalue data
sim = data.frame(parallel$fa.sim)
sim$type = c('Simulated Data (95th %ile)')
sim$num = c(row.names(obs))
sim$num = as.numeric(sim$num)
colnames(sim) = c('eigenvalue', 'type', 'num')
#Merge the two data frames (obs and sim) together into data frame called eigendat
eigendat = rbind(obs[1:50,],sim[1:50,])
apatheme=theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
text=element_text(family='Arial'),
legend.title=element_blank(),
legend.position=c(.7,.8),
axis.line.x = element_line(color='black'),
axis.line.y = element_line(color='black'))
#Use data from eigendat. Map number of factors to x-axis, eigenvalue to y-axis, and give different data point shapes depending on whether eigenvalue is observed or simulated
p = ggplot(eigendat, aes(x=num, y=eigenvalue, shape=type)) +
#Add lines connecting data points
geom_line()+
#Add the data points.
geom_point(size=4)+
#Label the y-axis 'Eigenvalue'
scale_y_continuous(name='Eigenvalue')+
#Label the x-axis 'Factor Number', and ensure that it ranges from 1-max # of factors, increasing by one with each 'tick' mark.
scale_x_continuous(name='Factor Number', breaks=seq(0, max(eigendat$num, na.rm=TRUE), by=5))+
#Manually specify the different shapes to use for actual and simulated data, in this case, white and black circles.
scale_shape_manual(values=c(16,1)) +
#Add vertical line indicating parallel analysis suggested max # of factors to retain
geom_vline(xintercept = parallel$nfact, linetype = 'dashed')+
#Apply our apa-formatting theme
apatheme
#Call the plot. Looks pretty!
p
ggsave('parallel.png', width=6, height=6, unit='in', dpi=300)