Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The plot from n_factors not working : plot the number of factors with percentage of variance explained and eigenvalues #117

Open
DominiqueMakowski opened this issue Feb 21, 2021 · 0 comments

Comments

@DominiqueMakowski
Copy link
Member

DominiqueMakowski commented Feb 21, 2021

Because the n_factors function got moved (and improved) to the parameters package, some of you realized that the figure featured here, showing the eigenvalues, the percentage of variance explained etc. was not available.

It turns out that this plot wasn't the best way to represent the results of n_factors; as the explained variance and the eigenvalues that is shows are extracted from one particular "dimension-reduction technique" (such as PCA or FA). But n_factors actually tries to estimate the optimal number of factors across different dimension reduction methods, so the two aren't really part of the same "step".

That said, here's a way of plotting them together with ggplot with multiple y axes:

library(dplyr)
library(ggplot2)
library(parameters)
library(effectsize)
library(see)

data <- summary(parameters::n_factors(attitude))
rez_pca <- summary(parameters::principal_components(attitude, n=4))

data$Eigenvalues <- as.numeric(rez_pca[1, -1]) # First row
data$VarianceExplained <- as.numeric(rez_pca[3, -1]) # Third row

# Rescale everything to eigenvalue scale - as this will our principal y-axis
newrange <- c(min(data$Eigenvalues), max(data$Eigenvalues))
data$Z_VarianceExplained <- effectsize::change_scale(data$VarianceExplained, to=newrange, range = c(0, 1))
data$Z_n_Methods <- effectsize::change_scale(data$n_Methods, to=newrange) 

# Plot
data %>% 
  ggplot(aes(x=n_Factors, y=Eigenvalues)) +
  geom_area(aes(y=Z_n_Methods), fill="orange", alpha=0.5) + 
  geom_line(aes(y=Z_VarianceExplained), color="red") +
  geom_line(aes(y=Eigenvalues), color="blue") +
  geom_hline(aes(yintercept = 1), linetype="dotted") +
  scale_y_continuous("Eingenvalue (blue)", 
                     sec.axis = sec_axis(~ . / max(data$Eigenvalues), 
                                         name = "Proportion of Explained Variance (red)", 
                                         labels=scales::percent,)) +
  see::theme_modern()

Created on 2021-02-21 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant