A compact, reproducible codebase for the OnlineFPCA project used in the associated paper.
This repository contains R code to run online stochastic-gradient FPCA, baseline batch methods, simulation studies, and real-data analyses for the paper. The README below explains the project structure, how to set up the environment, and how to reproduce tables and figures from the paper.
NOTE: data//output/ directory contains a large number of data/result files, which may slow down repo cloning.
Top-level layout (important files/directories):
R/– Core R implementation filesonlineFPCA.R: OnlineFPCA algorithmonlineFDAlocalpoly.R: OnlineCov algorithm (adapted from the code for Yang & Yao (2023))fpcaReg.R: SOAP algorithm, a batch FPCA method- Other helper utilities.
experiments/– Simulation scripts and analysis helpers (e.g.fpca1d.R,fpca2d.R).run_reps.shis a helper for submitting repeated (seeded) simulation jobs on an HPC cluster.test/–demo-fpca1d.Randdemo-fpca2d.Rare also for 1D and 2D simulation studies, but also easier to be run interactively.application/– Real-data pipelines and plotting scripts (e.g.gfr.R,aqi.R).data/– Data required to reproduce the application results. For this project the subfolders of interest aredata/gfr/anddata/epa-aqs/.data_generation/– Data simulators used by the simulation studies.external_codes/– Third-party algorithms: mOpCov and REML.install_pkgs.R– Script to install required R packages in a fresh environment.output/– Include our result files from simulation studies and real-data analysis. Large files are removed due to size limits.
Note: Plain numbers (1,2,...) are in the main article. Numbers with "S" (S1, S2, ...) are in the supplementary materials.
-
Table 1, 2, S4: Main simulation studies for 1D and 2D data under different noise levels. In
experiments/, Runfpca1d.Randfpca2d.Rfor--seedacross 1~100. Then, runanalyze_fpca1d.Randanalyze_fpca2d.Rto obtain results fromknitr::kable(). Table 1 and 2 are part of Table S4. -
Figure 3: FPC plot for AQI data. First run
application/aqi.Rto obtainfit_aqi_*.Rdata. Then, runapplication/analyze_aqi.Rto produce the plotsimu-aqi-fpc.pdf. -
Figure S1: Use
Matrix::image()to plot the matrixB,GandSin the 1D simulation. -
Figure S2: CI plot for 1D simulation. In
experiments/, runci-simu1d.Rfor--seedacross 1~100. Then, runanalyze_ci1d.Rto produce the plotci1d-ci.pdf. -
Figure S3: CI plot for 2D simulation. In
experiments/, runci-simu2d.Rfor--seedacross 1~100. Then, runanalyze_ci2d.Rto produce the plotci2d-ci.pdf. -
Table S1: RMSEs for different
$(C,W,B)$ . Runcheck_bwc.Rfor--seedacross 1~100. Then,analyze_bwc.Rwill produce the table. -
Figure S4, Table S2: RMSEs for different
$\omega$ and block sizes. Runcheck_abv.Rfor--seedacross 1~100. Then, runanalyze_abv.Rto produce the figureabv_paths.pdfand the table. -
Table S3: RMSEs for different mini-batch sizes. Run
check_nbatch.Rfor--seedacross 1~100. Then, runanalyze_nbatch.Rto produce the table. -
Figure S5, S6: RMSEs of FPCs in simulation studies under different sample sizes. Run
size1d.Randsize2d.Rfor--seedacross 1~100. Then, runanalyze_size1d.Randanalyze_size2d.Rto produce the figuressize1d_rmse.pdfandsize2d_rmse.pdf, respectively. -
Figures S7: RMSEs in simulation studies where the true FPCs are obtained from real data. Run
simu_gfr.Randsimu_aqi.Rfor--seedacross 1~100. Then, runanalyze_simu_gfr.Randanalyze_simu_aqi.Rto produce the figuressimu-gfr-fpc.pdfandsimu-aqi-fpc.pdf, respectively. -
Figure S9: FPC plot for news clicks data. First run
application/news/news.Rto obtainresult_news_*.Rdata. Then, runapplication/news/analyze_news.Rto produce the plotnews_eigfun.pdf. -
Figure S9 and S10: Dynamic tuning path in 1D/2D simulation. The script
fpca1d.Rwill produce the dynamic tuning pathtaupath-sim*d-sgd.pdfwhen the seed is1234. You can setsimpleto1andcompareto0to simplify the workflow and focus on figure plotting.
Rscript experiments/fpca1d.R --seed 1234 --compare 0 --simple 1
Rscript experiments/fpca2d.R --seed 1234 --compare 0 --simple 1-
Figure S11: Subject examples from the AQI data. The script
application/aqi/aqi-eda.Rprovides a complete walk-through of the exploratory data analysis of the AQI data, and the command to generate Figure 4 (aqi-sample.pdf). -
Figure S12: FPC plot for GFR data. First run
application/gfr/gfr.Rto obtainresult_gfr_*.Rdata. Then, runapplication/gfr/analyze_gfr.Rto produce the plotgfr_eigfun.pdf.
-
R (tested): R 4.5.0 (or a recent patch release).
-
Install the packages used in the analyses:
Rscript install_pkgs.R- Make sure
data/contains the required inputs. The (large) application datasets are not stored in the repo; if needed, place the original data files indata/gfr/anddata/epa-aqs/as used byapplication/gfr.Randapplication/aqi.R.
| Package | Version |
|---|---|
| argparse | 2.3.1 |
| cowplot | 1.2.0 |
| doFuture | 1.1.3 |
| dplyr | 1.1.4 |
| face | 0.1-8 |
| fastmatrix | 0.6-4 |
| fda | 6.3.0 |
| fdapace | 0.6.0 |
| foreach | 1.5.2 |
| giscoR | 1.0.0 |
| gslnls | 1.4.2 |
| lubridate | 1.9.4 |
| ManifoldOptim | 1.0.1 |
| Metrics | 0.1.4 |
| mgcv | 1.9-4 |
| Rcpp | 1.1.0 |
| Rdimtools | 1.1.3 |
| readr | 2.1.6 |
| readxl | 1.4.5 |
| remotes | 2.5.0 |
| RSpectra | 0.16-2 |
| rTensor | 1.4.9 |
| sf | 1.0-23 |
| sm | 2.2-6.0 |
| spData | 2.3.4 |
| stringr | 1.6.0 |
| tidyr | 1.3.2 |