This repository contains the R code for a small simulation study used in my BSc thesis. The script compares Principal Component Analysis (PCA), Factor Analysis (FA), and a simple Autoencoder (AE) on synthetic data generated under linear and non-linear mappings, at different noise levels and dimensions.
TL;DR: run
simulazione.R— it generates data, fits PCA/FA/AE, computes alignment-aware metrics, and saves results for each replication/setting.
-
Data generation: draws latent variables
$Z$ and produces observed data$X$ via a sparse loading matrix.-
Linear:
$X = ZW$ -
Non-linear: blocks based on
$Z^3$ and$\exp(0.5Z)$ , standardized before mixing.
-
Linear:
-
Models:
- PCA (top-$d$ components)
- FA (ML, varimax; scores by regression)
- AE (symmetric MLP with
tanh, linear code layer, early stopping & ReduceLROnPlateau)
-
Metrics (alignment-aware):
- Average squared factor correlation after best permutation/sign
- MSE after best permutation/sign
- MSE after orthogonal Procrustes rotation
-
Grid & replications: loops over
$n \in {1000, 10000}$ ,$d \in {2,5}$ ,$p \in {10,100,1000}$ , noise$\in$ {low, high}, transformation$\in$ {linear, nonlinear}, withn_replications = 50.
simulazione.R— the entire study (single file)- Results are written to
risultati_simulazione/as.RDatafiles (created automatically).
-
R packages:
MASS,psych,keras,tensorflow,dplyr,purrr,pracma,clue,ggplot2,tidyr,knitr,gtools,ks
Install in R with:install.packages(c("MASS","psych","keras","dplyr","purrr","pracma","clue","ggplot2","tidyr","knitr","gtools","ks"))
-
For the autoencoder, you'll also need to install TensorFlow:
keras::install_keras()
The synthetic data follows this general structure:
- Latent factors:
$Z \sim \mathcal{N}(0, I_d)$ where$d$ is the number of true factors - Loading matrix:
$W \in \mathbb{R}^{d \times p}$ (sparse, with blocks of non-zero loadings)
Linear case:
Non-linear case:
-
Factor Correlation:
$\text{corr}(\hat{Z}, Z)^2$ averaged after optimal permutation and sign flips -
Procrustes MSE:
$|\hat{Z} - ZQ|_F^2$ where$Q$ is the optimal orthogonal transformation
Simply run the main script:
source("simulazione.R")The script will:
- Generate synthetic datasets for each parameter combination
- Fit PCA, FA, and AE models to each dataset
- Compute all evaluation metrics
- Save results to
risultati_simulazione/ - Print progress information during execution
Results can be loaded and analyzed using standard R data manipulation tools.