This is an RMarkdown document displaying R code for generating sets of five eigenvalues for three tests, as well as code for producing corresponding scree plots. Five eigenvalues are simumlated for three tests in order to mimic unidimensionality. Strict ordering of eigenvalues was also needed, and this was accomplished by simulating the first two eigenvalues from largely differing normal distributions. The remaining eigenvalues were determined by subtracting a small randomized amount from each prior eigenvalue.
A data frame is constructed based on these simulated values with the purpose of constructing scree plots. Scree plots are then generated. A dashed horizontal line is placed at 1, as some use that as a value of reference in scree plots.
The first block of code accomplished the following:
The first eigenvalue is simulated such that it will greatly exceed the other four values. The second eigenvalue is then simulated to be far less than the first eigenvalue. Then, the third eigenvalue is calculated as the second eigenvalue minus a small randomized value. This process is also used to calculate the last two eigenvalues.
library(ggplot2)
library(dplyr)
eig1 <- rnorm(3, 7, 1)
eig2 <- rnorm(3, 1, .1)
rand1 <- runif(3, 0, .1)
rand2 <- runif(3, 0, .1)
rand3 <- runif(3, 0, .1)
eig3 <- eig2 - rand1
eig4 <- eig3 - rand2
eig5 <- eig4 - rand3
eig1
eig2
eig3
eig4
eig5
The next block of code creates a data frame that houses the simulated eigenvalues, as well as which test they come from and order in terms of value.
Position <- rep(seq(1,5),3)
Test <- c(rep(1,5),rep(2,5),rep(3,5))
eigval <- rbind(t(eig1), t(eig2), t(eig3), t(eig4), t(eig5))
eigmat <- matrix(eigval,ncol=1)
scree <- cbind(Test,Position,eigmat)
scree <- as.data.frame(scree)
names(scree)[3] <- "Eigenvalue"
The item variable is created and combined with the previous data frame to create a finalized data frame for creating the plots of interest. The plot includes points for each eigenvalue, as well as a line plot connecting them. A line at y = 1 is included for reference (as is sometimes done in a factor analysis).
screeplot <- scree %>% ggplot(aes(x = Position, y = Eigenvalue)) + geom_point(col = "red") + geom_line(col = "red") + geom_hline(yintercept = 1, linetype = "dashed") + theme_bw() + facet_wrap(~Test, ncol = 1)
screeplot