### Install and Load fdapace Package Source: https://context7.com/functionaldata/tpace/llms.txt Install the fdapace package from CRAN or the development version from GitHub. Load the package into the R session using library(). ```r install.packages("fdapace") ``` ```r devtools::install_github("functionaldata/tPACE") ``` ```r library(fdapace) ``` -------------------------------- ### Install Development Version of PACE Source: https://github.com/functionaldata/tpace/blob/master/README.md Use this command to install the latest development version of the PACE package from GitHub. Ensure you have the 'devtools' package installed. ```r devtools::install_github("functionaldata/tPACE") ``` -------------------------------- ### Install Latest CRAN Release of PACE Source: https://github.com/functionaldata/tpace/blob/master/README.md Use this command to install the most recent stable release of the PACE package from CRAN. This is the recommended method for most users. ```r install.packages("fdapace") ``` -------------------------------- ### CreatePathPlot Examples Source: https://context7.com/functionaldata/tpace/llms.txt Demonstrates various options for plotting functional data trajectories using CreatePathPlot. Options include showing only observations, including the mean function, specifying the number of components, plotting derivatives, and custom styling. ```r CreatePathPlot(fpca_obj, subset = 1:5, obsOnly = TRUE) ``` ```r CreatePathPlot(fpca_obj, subset = 1:5, showMean = TRUE) ``` ```r CreatePathPlot(fpca_obj, subset = 1:5, K = 2) ``` ```r CreatePathPlot(fpca_obj, subset = 1:5, showObs = FALSE, derOptns = list(p = 1, bw = 0.1, kernelType = 'epan')) ``` ```r CreatePathPlot(fpca_obj, subset = 1:10, col = rainbow(10), lty = 1, pch = 16, xlab = 'Time', ylab = 'Value', main = 'Fitted Trajectories') ``` -------------------------------- ### Load PACE Package in R Source: https://github.com/functionaldata/tpace/blob/master/README.md Load the PACE package into your R session after installation to start using its functionalities for functional data analysis. ```r library(fdapace) ``` -------------------------------- ### fitted.FPCA - Get Fitted Curves Source: https://context7.com/functionaldata/tpace/llms.txt The `fitted` method extracts the fitted mean function and principal component functions from an FPCA object, allowing for estimation of derivatives. ```APIDOC ## fitted.FPCA - Get Fitted Curves ### Description Extracts fitted curves (mean function and principal components) from an FPCA object. It can also compute derivatives of the fitted curves. ### Method `fitted(object, K = NULL, derOptns = NULL)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **object** (FPCA object) - The fitted FPCA model object. - **K** (integer, optional) - The number of principal components to use for reconstructing the fitted curves. If NULL, the number selected by the model is used. - **derOptns** (list, optional) - A list of options for computing derivatives. If NULL, only the original fitted curves are returned. - `p` (integer): The order of the derivative to compute (e.g., 1 for the first derivative, 2 for the second). - `kernelType` (character): Type of kernel for smoothing derivatives (e.g., 'epan', 'gauss'). - `bw` (numeric): Bandwidth for smoothing. ### Request Example ```r library(fdapace) set.seed(1) # Generate and fit data n <- 50 pts <- seq(0, 1, by = 0.02) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 8:12) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt) # Get fitted curves (original scale) fitted_curves <- fitted(fpca_obj) # Returns: n x nWorkGrid matrix # Get fitted curves with specific K fitted_k3 <- fitted(fpca_obj, K = 3) # Get first derivative of fitted curves fitted_deriv1 <- fitted(fpca_obj, derOptns = list( p = 1, # First derivative kernelType = 'epan', # Epanechnikov kernel for smoothing bw = 0.1 # Bandwidth )) # Get second derivative fitted_deriv2 <- fitted(fpca_obj, derOptns = list(p = 2)) ``` ### Response #### Success Response (200) A matrix where rows correspond to subjects and columns correspond to the `workGrid` points. If `derOptns` is specified, the matrix contains the computed derivatives of the fitted curves. #### Response Example ```r # Plot original and derivative for one subject par(mfrow = c(1, 3)) plot(fpca_obj$workGrid, fitted_curves[1, ], type = 'l', main = 'Fitted Curve', xlab = 't', ylab = 'Y(t)') plot(fpca_obj$workGrid, fitted_deriv1[1, ], type = 'l', main = 'First Derivative', xlab = 't', ylab = "Y'(t)") plot(fpca_obj$workGrid, fitted_deriv2[1, ], type = 'l', main = 'Second Derivative', xlab = 't', ylab = "Y''(t)") ``` ``` -------------------------------- ### fitted.FPCA - Get Fitted Curves and Derivatives Source: https://context7.com/functionaldata/tpace/llms.txt Extracts fitted curves from an FPCA object. Can also compute and extract derivatives of the fitted curves by specifying derOptns. Requires a fitted FPCA object. ```R library(fdapace) set.seed(1) # Generate and fit data n <- 50 pts <- seq(0, 1, by = 0.02) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 8:12) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt) # Get fitted curves (original scale) fitted_curves <- fitted(fpca_obj) # Returns: n x nWorkGrid matrix # Get fitted curves with specific K fitted_k3 <- fitted(fpca_obj, K = 3) # Get first derivative of fitted curves fitted_deriv1 <- fitted(fpca_obj, derOptns = list( p = 1, # First derivative kernelType = 'epan', # Epanechnikov kernel for smoothing bw = 0.1 # Bandwidth )) # Get second derivative fitted_deriv2 <- fitted(fpca_obj, derOptns = list(p = 2)) # Plot original and derivative for one subject par(mfrow = c(1, 3)) plot(fpca_obj$workGrid, fitted_curves[1, ], type = 'l', main = 'Fitted Curve', xlab = 't', ylab = 'Y(t)') plot(fpca_obj$workGrid, fitted_deriv1[1, ], type = 'l', main = 'First Derivative', xlab = 't', ylab = "Y'(t)") plot(fpca_obj$workGrid, fitted_deriv2[1, ], type = 'l', main = 'Second Derivative', xlab = 't', ylab = "Y''(t)") ``` -------------------------------- ### Prepare FPCA Inputs and Run FPCA Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html This snippet shows how to prepare input data for FPCA using MakeFPCAInputs and then run the FPCA analysis. Ensure your data is structured correctly for dense datasets. ```R L3 <- MakeFPCAInputs(IDs = rep(1:N, each=M), tVec=rep(s,N), t(yTrue)) FPCAdense <- FPCA(L3$Ly, L3$Lt) ``` -------------------------------- ### Determine Number of Components with SelectK Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Shows how to use the SelectK function to find the optimal number of components based on different criteria like Fraction-of-Variance-Explained (FVE) or AIC. ```R SelectK( FPCAsparse, criterion = 'FVE', FVEthreshold = 0.95) # K = 2 ## $K ## [1] 3 ## ## $criterion ## [1] 0.9876603 ``` ```R SelectK( FPCAsparse, criterion = 'AIC') # K = 2 ## $K ## [1] 2 ## ## $criterion ## [1] 993.4442 ``` -------------------------------- ### Load ks Package in R Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html This snippet shows how to load the `ks` package, which is required for functional data analysis and visualization functions like `CreatePathPlot`. ```r require('ks') ## Loading required package: ks ``` -------------------------------- ### Obtain Fitted Curves and Derivatives Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Illustrates how to obtain smoothed fitted curves and their numerical derivatives using the fitted.FPCA function. Specify the derivative order 'p' and optionally the kernel type for smoothing derivatives. ```R fittedCurvesP0 <- fitted(FPCAsparse) # equivalent: fitted(FPCAsparse, derOptns=list(p = 0)); # Get first order derivatives of fitted curves, smooth using Epanechnikov kernel fittedCurcesP1 <- fitted(FPCAsparse, derOptns=list(p = 1, kernelType = 'epan')) ## Warning in fitted.FPCA(FPCAsparse, derOptns = list(p = 1, kernelType = "epan")): Potentially you use too many components to estimate derivatives. ## Consider using SelectK() to find a more informed estimate for 'K'. ``` -------------------------------- ### Switch FPCA Kernel to Rectangular Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Demonstrates how to change the default Gaussian kernel to a rectangular kernel for smoothing in FPCA. ```R FPCAsparseRect <- FPCA(ySparse$yNoisy, ySparse$Lt, optns = list(kernel = 'rect')) # Use rectangular kernel ``` -------------------------------- ### Visualizing Fitted Trajectories with Different Bandwidths Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Compares fitted trajectories using the default GCV bandwidth and a user-defined bandwidth. This helps in assessing the impact of bandwidth selection on the smoothed mean estimation. ```R par(mfrow=c(1,2)) CreatePathPlot( FPCAsparse, subset = 1:3, main = "GCV bandwidth", pch = 16) CreatePathPlot( FPCAsparseMuBW5, subset = 1:3, main = "User-defined bandwidth", pch = 16) ``` -------------------------------- ### FPCA with User-Defined Bandwidth Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Demonstrates how to use FPCA with a user-defined bandwidth for the smoothed mean. This is useful when the default cross-validation bandwidth does not yield satisfactory results. ```R FPCAsparseMuBW5 <- FPCA(ySparse$yNoisy, ySparse$Lt, optns= list(userBwMu = 5)) ``` -------------------------------- ### CreatePathPlot - Visualize Fitted Trajectories Source: https://context7.com/functionaldata/tpace/llms.txt Generates plots showing fitted sample paths alongside original observations. Useful for visually assessing the fit of the FPCA model. Can optionally hide observations using showObs = FALSE. ```R library(fdapace) set.seed(1) # Generate data n <- 20 pts <- seq(0, 1, by = 0.05) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 10) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt, list(dataType = 'Sparse', error = FALSE, kernel = 'epan')) # Basic path plot for first 5 subjects CreatePathPlot(fpca_obj, subset = 1:5) # Show fitted curves without observations CreatePathPlot(fpca_obj, subset = 1:5, showObs = FALSE) ``` -------------------------------- ### Create Bandwidth Selection Plot Source: https://context7.com/functionaldata/tpace/llms.txt Visualizes the bandwidth selection process for the smoothing of functional data. ```r CreateBWPlot(fpca_flies) ``` -------------------------------- ### CreatePathPlot - Visualize Fitted Trajectories Source: https://context7.com/functionaldata/tpace/llms.txt The `CreatePathPlot` function generates plots to visualize fitted sample paths along with the original observed data points. ```APIDOC ## CreatePathPlot - Visualize Fitted Trajectories ### Description Creates plots showing fitted sample paths from an FPCA model, optionally overlayed with original observations. ### Method `CreatePathPlot(object, subset = NULL, showObs = TRUE)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **object** (FPCA object) - The fitted FPCA model object. - **subset** (integer vector, optional) - A subset of subject indices to plot. If NULL, all subjects are plotted. - **showObs** (logical) - Whether to display the original observed data points on the plot. Defaults to TRUE. ### Request Example ```r library(fdapace) set.seed(1) # Generate data n <- 20 pts <- seq(0, 1, by = 0.05) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 10) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt, list(dataType = 'Sparse', error = FALSE, kernel = 'epan')) # Basic path plot for first 5 subjects CreatePathPlot(fpca_obj, subset = 1:5) # Show fitted curves without observations CreatePathPlot(fpca_obj, subset = 1:5, showObs = FALSE) ``` ### Response #### Success Response (200) Generates plots to the current graphics device. #### Response Example (No explicit return value, plots are generated directly.) ``` -------------------------------- ### Create Sparse Sample for FPCA Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Generates a sparse sample dataset where each subject has a variable number of readings. This is a prerequisite for running FPCA on sparse data. ```R # Create sparse sample # Each subject has one to five readings (median: 3) set.seed(123) ySparse <- Sparsify(yTrue, s, sparsity = c(1:5)) ``` -------------------------------- ### Load and Prepare medfly25 Dataset for FPCA Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Loads the medfly25 dataset and prepares it into the required input format for the FPCA function using MakeFPCAInputs. ```R # load data data(medfly25) # Turn the original data into a list of paired amplitude and timing lists Flies <- MakeFPCAInputs(medfly25$ID, medfly25$Days, medfly25$nEggs) ``` -------------------------------- ### FPCA on Dense Functional Data Source: https://context7.com/functionaldata/tpace/llms.txt Demonstrates fitting an FPCA model to dense functional data. Requires generating sample data and then using the FPCA function with 'smooth' method for mean and covariance estimation. ```R N <- 200 M <- 100 s <- seq(0, 10, length.out = M) meanFunct <- function(s) s + 10 * exp(-(s - 5)^2) eigFunct1 <- function(s) cos(2 * s * pi / 10) / sqrt(5) Ksi <- matrix(rnorm(N), ncol = 1) yTrue <- Ksi %*% t(matrix(eigFunct1(s), ncol = 1)) + t(matrix(rep(meanFunct(s), N), nrow = M)) L3 <- MakeFPCAInputs(IDs = rep(1:N, each = M), tVec = rep(s, N), yVec = t(yTrue)) fpca_dense <- FPCA(L3$Ly, L3$Lt, list(methodMuCovEst = 'smooth')) cat("Number of components:", fpca_dense$selectK, "\n") cat("Variance explained:", fpca_dense$cumFVE[fpca_dense$selectK] * 100, " %\n") ``` -------------------------------- ### Visualize Observed, Completed, and True Trajectories Source: https://github.com/functionaldata/tpace/blob/master/vignettes/dynFPCA.html Visualizes the original discretely observed data, the FPCA-completed trajectory, and the true trajectory for comparison. This helps in assessing the accuracy of the completion. ```R matplot(gr, x[1, ], pch = c(16), col = 1, xlab = "", ylab = "", ylim = c(-2, 2)) lines(grid, PACE.pred, lty = 1, col = 2, xlab = "", ylab = "")# completed functional data lines(densegr, x.full[1, ], col = 3)# discretely observed complete functional data legend("topleft", legend = c("discretedly observed data", "completed trajectory", "true trajectory"), bty = "n", lty = c(NA, 1, 1), pch = c(16, NA, NA), col = 1:3) title(sub = 'Figure 2: PACE completion for functional fragments with 25% of subinterval missing.', line = 3) ``` -------------------------------- ### Prepare FPCA Inputs from Sparse Observations Source: https://github.com/functionaldata/tpace/blob/master/vignettes/dynFPCA.html This code converts observed functional data, potentially with missing segments, into a format suitable for FPCA. It aggregates IDs, observation times, and values from a sparse observation matrix. ```R id <- c() time <- c() y <- c() for(i in 1:n){ id <- append(id, rep(i, sum(x.obs[i, ]))) time <- append(time, grid[x.obs[i, ] == 1]) y <- append(y, x[i, x.obs[i,] == 1]) } L3 <- MakeFPCAInputs(IDs = id, tVec = time, y) ``` -------------------------------- ### SelectK - Select Number of Components Source: https://context7.com/functionaldata/tpace/llms.txt The `SelectK` function determines the optimal number of functional principal components using FVE (Fraction of Variance Explained), AIC, or BIC criteria, or by specifying a fixed number. ```APIDOC ## SelectK - Select Number of Components ### Description Determines the optimal number of functional principal components (K) using FVE, AIC, or BIC criteria, or by specifying a fixed number. ### Method `SelectK(object, criterion = c('FVE', 'AIC', 'BIC'), FVEthreshold = 0.95)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **object** (FPCA object) - The fitted FPCA object from the `FPCA` function. - **criterion** (character or integer) - The criterion to use for selecting K. Options are: - `'FVE'`: Select K based on Fraction of Variance Explained. - `'AIC'`: Select K based on Akaike Information Criterion. - `'BIC'`: Select K based on Bayesian Information Criterion. - An integer: Directly specify the number of components K. - **FVEthreshold** (numeric) - The threshold for FVE if `criterion = 'FVE'`. Defaults to 0.95. ### Request Example ```r library(fdapace) set.seed(1) # Generate data and run FPCA n <- 100 pts <- seq(0, 1, by = 0.05) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 10) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt) # Select K using different criteria k_fve_95 <- SelectK(fpca_obj, criterion = 'FVE', FVEthreshold = 0.95) # Returns: list(K = selected_K, criterion = FVE_value) k_fve_99 <- SelectK(fpca_obj, criterion = 'FVE', FVEthreshold = 0.99) k_aic <- SelectK(fpca_obj, criterion = 'AIC') # Returns: list(K = selected_K, criterion = AIC_value) k_bic <- SelectK(fpca_obj, criterion = 'BIC') # Returns: list(K = selected_K, criterion = BIC_value) # Fixed K selection k_fixed <- SelectK(fpca_obj, criterion = 3) # Returns: list(K = 3, criterion = NULL) ``` ### Response #### Success Response (200) A list containing: - **K** (integer): The selected number of components. - **criterion** (numeric or NULL): The value of the criterion (FVE, AIC, or BIC) for the selected K, or NULL if K was specified directly. #### Response Example ```r cat("K by FVE (95%):", k_fve_95$K, "- FVE:", k_fve_95$criterion, "\n") cat("K by AIC:", k_aic$K, "\n") cat("K by BIC:", k_bic$K, "\n") ``` ``` -------------------------------- ### Simulate Functional Fragments in R Source: https://github.com/functionaldata/tpace/blob/master/vignettes/dynFPCA.html This R code simulates functional fragments by setting parameters for the number of trajectories, observations per curve, and grid points. It generates eigenvalues for cosine and sine bases, defines the proportion of the domain to be missing, and sets up dense and observation grids. The simulation includes generating discretely observed complete functional data based on specified eigenvalues and random normal deviates, then subsetting this data to the observation grid. ```r set.seed(1) # set parameters for simulation n <- 50 # number of sample trajectories m <- 30 # number of observation per curve grid <- seq(0, 1, len = m) # set up regular grid lambda.cos <- 3^(-(2*(1:25)-1)) # eigenvalues lambda.sin <- 3^(-(2*(1:25))) mpct <- 0.25 # proportion of domain missing densegr <- ((1:100) - .5)/100 # dense regular grid to generate the data gr <- densegr[(100/m)*(1:m)] # observation grid sigma2 <- 0.1 # variance for noise term # generate discretely observed complete functional data x.full <- matrix(0, n, length(densegr)) for (j in 1:length(lambda.cos)) { f <- sqrt(lambda.cos[j])*sqrt(2)*cos(2*pi*(2*j - 1)*densegr) x.full <- x.full + rnorm(n, 0, 1)%*%t(f) } for (j in 1:length(lambda.sin)) { f <- sqrt(lambda.sin[j])*sqrt(2)*sin(2*pi*(2*j)*densegr) x.full <- x.full + rnorm(n, 0, 1)%*%t(f) } # subset the original dense true curve to the k-equidistance sampling scheme; x <- x.full[, densegr %in% gr] # generate observation periods by deleting a random subinterval from domain, # with 20% of data is complete # matrix of indicators for inclusion/exclusion of observation x.obs <- matrix(1, n, length(grid)) centers <- c(0.25, 0.5, 0.75) x.obs[1, ] <- grid <= 0.75 for (i in floor(n*0.2):n) { cen <- runif(1) x.obs[i, (cen - mpct/2 < grid)&(grid < cen + mpct/2)] <- FALSE # missing interval = (cen-u,cen+u) } # remove missing periods x[!x.obs] <- NA # distort by error term for(i in 1:dim(x)[1]){ for(j in 1:dim(x)[2]){ x[i,j] <- x[i,j] + rnorm(n = 1, sd = sqrt(sigma2)) } } ``` -------------------------------- ### Generate Wiener Processes with Wiener Source: https://context7.com/functionaldata/tpace/llms.txt Generates samples of Wiener processes (Brownian motions) using the Wiener function. Useful for simulating functional data. Requires fdapace library. ```r library(fdapace) set.seed(1) # Generate Wiener processes (Brownian motions) n <- 100 pts <- seq(0, 1, length.out = 50) # Dense Wiener process (returns matrix) wiener_dense <- Wiener(n = n, pts = pts, K = 50) dim(wiener_dense) # n x length(pts) ``` -------------------------------- ### Create Outlier Plot with KDE Variant Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Use this function to visualize outliers in functional data. The 'KDE' variant is suitable for identifying multiple clusters within the data. Ensure the 'fpcaObjFlies' object is properly initialized. ```R par(mfrow=c(1,1)) CreateOutliersPlot(fpcaObjFlies, optns = list(K = 3, variant = 'KDE')) ``` -------------------------------- ### Create Functional PCA Path Plots Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Generates path plots to visualize functional principal components. Use this to visually inspect the variation explained by different numbers of components (K). The `subset` argument allows focusing on specific data points. ```r par(mfrow=c(1,2)) CreatePathPlot(fpcaObjFlies, subset = c(3,5,135), main = 'K = 11', pch = 4); grid() CreatePathPlot(fpcaObjFlies, subset = c(3,5,135), K = 3, main = 'K = 3', pch = 4) ; grid() ``` -------------------------------- ### SelectK - Optimal Number of Components Source: https://context7.com/functionaldata/tpace/llms.txt Selects the optimal number of functional principal components (K) using FVE, AIC, or BIC criteria. Ensure FPCA object is fitted before using SelectK. The function returns a list containing the selected K and the criterion value. ```R library(fdapace) set.seed(1) # Generate data and run FPCA n <- 100 pts <- seq(0, 1, by = 0.05) sampWiener <- Wiener(n, pts) sparse_data <- Sparsify(sampWiener, pts, 10) fpca_obj <- FPCA(sparse_data$Ly, sparse_data$Lt) # Select K using different criteria k_fve_95 <- SelectK(fpca_obj, criterion = 'FVE', FVEthreshold = 0.95) # Returns: list(K = selected_K, criterion = FVE_value) k_fve_99 <- SelectK(fpca_obj, criterion = 'FVE', FVEthreshold = 0.99) k_aic <- SelectK(fpca_obj, criterion = 'AIC') # Returns: list(K = selected_K, criterion = AIC_value) k_bic <- SelectK(fpca_obj, criterion = 'BIC') # Returns: list(K = selected_K, criterion = BIC_value) # Fixed K selection k_fixed <- SelectK(fpca_obj, criterion = 3) # Returns: list(K = 3, criterion = NULL) cat("K by FVE (95%):", k_fve_95$K, "- FVE:", k_fve_95$criterion, "\n") cat("K by AIC:", k_aic$K, "\n") cat("K by BIC:", k_bic$K, "\n") ``` -------------------------------- ### Generate Dense Functional Dataset in R Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html This code generates a synthetic dense functional dataset for testing purposes. It requires the fdapace library and involves defining the number of subjects, measurements, continuum, mean function, and eigencomponents. ```R library(fdapace) # Set the number of subjects (N) and the # number of measurements per subjects (M) N <- 200; M <- 100; set.seed(123) # Define the continuum s <- seq(0,10,length.out = M) # Define the mean and 2 eigencomponents meanFunct <- function(s) s + 10*exp(-(s-5)^2) eigFunct1 <- function(s) +cos(2*s*pi/10) / sqrt(5) eigFunct2 <- function(s) -sin(2*s*pi/10) / sqrt(5) # Create FPC scores Ksi <- matrix(rnorm(N*2), ncol=2); Ksi <- apply(Ksi, 2, scale) Ksi <- Ksi %*% diag(c(5,2)) # Create Y_true yTrue <- Ksi %*% t(matrix(c(eigFunct1(s),eigFunct2(s)), ncol=2)) + t(matrix(rep(meanFunct(s),N), nrow=M)) ``` -------------------------------- ### Generate Sparse Wiener Process Source: https://context7.com/functionaldata/tpace/llms.txt Generates a sparse Wiener process with specified number of points and sparsification levels. Returns a list containing time points (Lt) and values (Ly). ```r wiener_sparse <- Wiener(n = n, pts = pts, sparsify = 5:10) ``` -------------------------------- ### FClust: Functional Clustering Source: https://context7.com/functionaldata/tpace/llms.txt Performs clustering of functional data using FPC scores with EMCluster or kCFC algorithms. Requires functional data prepared with MakeFPCAInputs. Results include cluster assignments and the FPCA object used. ```r library(fdapace) set.seed(123) # Load medfly data (egg-laying patterns of fruit flies) data(medfly25) # Prepare data Flies <- MakeFPCAInputs(medfly25$ID, medfly25$Days, medfly25$nEggs) # Cluster into 2 groups using EMCluster (default) clust_em <- FClust( Ly = Flies$Ly, Lt = Flies$Lt, k = 2, cmethod = 'EMCluster', optnsFPCA = list( methodMuCovEst = 'smooth', userBwCov = 2, FVEthreshold = 0.90 ) ) # Results # clust_em$cluster - Cluster assignments (1 to k) # clust_em$fpca - FPCA object used for clustering # clust_em$clusterObj - EMCluster or kCFC object table(clust_em$cluster) # Alternative: Use kCFC method (Chiou and Li, 2007) clust_kcfc <- FClust( Ly = Flies$Ly, Lt = Flies$Lt, k = 3, cmethod = 'kCFC', optnsFPCA = list(methodMuCovEst = 'smooth', FVEthreshold = 0.90), optnsCS = list(methodMuCovEst = 'smooth', FVEthreshold = 0.70) ) # Validate clusters against known groups veryLowCount <- ifelse(sapply(unique(medfly25$ID), function(u) sum(medfly25$nEggs[medfly25$ID == u])) < 25, 1, 2) cat("Clustering accuracy:", mean(clust_em$cluster == veryLowCount) * 100, " %\n") ``` -------------------------------- ### Format Data for FPCA with MakeFPCAInputs Source: https://context7.com/functionaldata/tpace/llms.txt Convert raw longitudinal data into the list format required by fdapace functions. Handles subject IDs, time points, and measurements, with options for NA removal, sorting, and deduplication. ```r library(fdapace) # Example 1: Convert long-format data to FPCA inputs # Simulate longitudinal data with subject IDs set.seed(123) n_subjects <- 50 observations_per_subject <- sample(3:8, n_subjects, replace = TRUE) # Create data vectors IDs <- rep(1:n_subjects, times = observations_per_subject) tVec <- unlist(lapply(observations_per_subject, function(m) sort(runif(m, 0, 10)))) yVec <- sin(tVec) + rnorm(length(tVec), sd = 0.3) # Convert to FPCA format fpca_input <- MakeFPCAInputs(IDs = IDs, tVec = tVec, yVec = yVec) # Result structure # fpca_input$Lid - List of subject IDs # fpca_input$Ly - List of observation values per subject # fpca_input$Lt - List of time points per subject ``` ```r # Example 2: Dense matrix format # Each row is a subject, columns are time points dense_data <- matrix(rnorm(200 * 100), nrow = 200, ncol = 100) time_grid <- seq(0, 1, length.out = 100) dense_input <- MakeFPCAInputs(tVec = time_grid, yVec = dense_data) # dense_input$Ly contains 200 lists, each with 100 observations ``` ```r # Example 3: Handle duplicates and sorting fpca_input_clean <- MakeFPCAInputs( IDs = IDs, tVec = tVec, yVec = yVec, na.rm = TRUE, # Remove NA values sort = TRUE, # Ensure time points are sorted deduplicate = TRUE # Average values at duplicate time points ) ``` -------------------------------- ### Create a Basic Box Plot in R Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Use the 'boxplot' function to create a standard box plot. Specify labels for the x and y axes using 'xlab' and 'ylab'. ```r CreateFuncBoxPlot(fpcaObjFlies, xlab = 'Days', ylab = '# of eggs laid', optns = list(K=3, variant='bagplot')) ``` -------------------------------- ### Optimal Experimental Design with FOptDes Source: https://context7.com/functionaldata/tpace/llms.txt Finds optimal time points for functional data measurements using FOptDes. Supports trajectory recovery (isRegression = FALSE) and scalar response prediction (isRegression = TRUE). Requires fdapace library and pre-processed FPCA inputs. ```r library(fdapace) set.seed(1) # Generate functional data n <- 50 pts <- seq(0, 1, by = 0.05) sampWiener <- Wiener(n, pts) fpca_input <- MakeFPCAInputs( IDs = rep(1:n, each = length(pts)), tVec = rep(pts, times = n), yVec = t(sampWiener) ) # Find optimal design for trajectory recovery optdes_traj <- FOptDes( Ly = fpca_input$Ly, Lt = fpca_input$Lt, p = 3, # Number of design points isRegression = FALSE, # Trajectory recovery isSequential = FALSE, # Global optimization RidgeCand = seq(0.02, 0.2, 0.02) # Ridge parameter candidates ) # Results # optdes_traj$OptDes - Optimal design points # optdes_traj$R2 - Coefficient of determination # optdes_traj$R2adj - Adjusted R2 # optdes_traj$OptRidge - Selected ridge parameter cat("Optimal time points:", round(optdes_traj$OptDes, 3), "\n") cat("R-squared:", round(optdes_traj$R2, 3), "\n") # Optimal design for scalar response prediction Resp <- rowMeans(sampWiener) + rnorm(n, sd = 0.5) # Scalar response optdes_resp <- FOptDes( Ly = fpca_input$Ly, Lt = fpca_input$Lt, Resp = Resp, # Scalar response p = 4, # Number of design points isRegression = TRUE, # Scalar prediction RidgeCand = seq(0.02, 0.2, 0.02) ) cat("\nFor response prediction:\n") cat("Optimal time points:", round(optdes_resp$OptDes, 3), "\n") cat("R-squared:", round(optdes_resp$R2, 3), "\n") ``` -------------------------------- ### MakeFPCAInputs - Format Data for FPCA Source: https://context7.com/functionaldata/tpace/llms.txt The MakeFPCAInputs function converts raw vector data into the list format required by FPCA and other fdapace functions, handling subject IDs, time points, and measurements. It supports various input formats and options for data cleaning. ```APIDOC ## MakeFPCAInputs - Format Data for FPCA ### Description Converts raw vector data into the list format required by FPCA and other fdapace functions. Handles subject IDs, time points, and measurements. Supports long-format data, dense matrix format, and includes options for data cleaning like removing NAs, sorting time points, and deduplicating entries at the same time point. ### Method `MakeFPCAInputs` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters for MakeFPCAInputs function: - **IDs** (vector) - Subject identifiers for each observation. - **tVec** (vector) - Time points for each observation. - **yVec** (vector or matrix) - Observed values. If a matrix, it's assumed to be dense data where rows are subjects and columns are time points. - **na.rm** (logical, optional) - If TRUE, removes observations with NA values. - **sort** (logical, optional) - If TRUE, sorts observations by time for each subject. - **deduplicate** (logical, optional) - If TRUE, averages values at duplicate time points for each subject. ### Request Example ```r library(fdapace) # Example 1: Convert long-format data to FPCA inputs set.seed(123) n_subjects <- 50 observations_per_subject <- sample(3:8, n_subjects, replace = TRUE) IDs <- rep(1:n_subjects, times = observations_per_subject) tVec <- unlist(lapply(observations_per_subject, function(m) sort(runif(m, 0, 10)))) yVec <- sin(tVec) + rnorm(length(tVec), sd = 0.3) fpca_input <- MakeFPCAInputs(IDs = IDs, tVec = tVec, yVec = yVec) # Example 2: Dense matrix format dense_data <- matrix(rnorm(200 * 100), nrow = 200, ncol = 100) time_grid <- seq(0, 1, length.out = 100) dense_input <- MakeFPCAInputs(tVec = time_grid, yVec = dense_data) # Example 3: Handle duplicates and sorting fpca_input_clean <- MakeFPCAInputs( IDs = IDs, tVec = tVec, yVec = yVec, na.rm = TRUE, sort = TRUE, deduplicate = TRUE ) ``` ### Response #### Success Response (List) - **Lid** (list) - List of subject IDs. - **Ly** (list) - List of observation values per subject. - **Lt** (list) - List of time points per subject. #### Response Example ```r # For Example 1: # fpca_input$Lid - List of subject IDs # fpca_input$Ly - List of observation values per subject # fpca_input$Lt - List of time points per subject # For Example 2: # dense_input$Ly contains 200 lists, each with 100 observations ``` ``` -------------------------------- ### Fit FPCA Model for Sparse Functional Data Source: https://github.com/functionaldata/tpace/blob/master/vignettes/dynFPCA.html Fits an FPCA model using the prepared inputs. It specifies options for sparse data and a user-defined grid, and uses the 'CE' method for estimating the Xi coefficients. ```R optns <- list(dataType = 'Sparse', usergrid = TRUE, methodXi = 'CE') fit <- FPCA(L3$Ly, L3$Lt, optns = optns) ``` -------------------------------- ### Apply Pandoc Styles to Pre.sourceCode Source: https://github.com/functionaldata/tpace/blob/master/vignettes/dynFPCA.html This JavaScript function modifies CSS rules to apply styles from 'div.sourceCode' to 'pre.sourceCode' if the 'div.sourceCode' rule has color or background-color set. This ensures consistent styling for code blocks. ```javascript (function() { var sheets = document.styleSheets; for (var i = 0; i < sheets.length; i++) { if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; try { var rules = sheets[i].cssRules; } catch (e) { continue; } for (var j = 0; j < rules.length; j++) { var rule = rules[j]; // check if there is a div.sourceCode rule if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue; var style = rule.style.cssText; // check if color or background-color is set if (rule.style.color === '' && rule.style.backgroundColor === '') continue; // replace div.sourceCode by a pre.sourceCode rule sheets[i].deleteRule(j); sheets[i].insertRule('pre.sourceCode{' + style + '}', j); } } })(); ``` -------------------------------- ### Create Diagnostics Plot Source: https://context7.com/functionaldata/tpace/llms.txt Generates a combined diagnostic plot for the functional data analysis results. ```r CreateDiagnosticsPlot(fpca_flies) ``` -------------------------------- ### Create Scree Plot Source: https://context7.com/functionaldata/tpace/llms.txt Generates a scree plot showing the decay of eigenvalues, useful for determining the number of principal components. ```r CreateScreePlot(fpca_flies) ``` -------------------------------- ### FPCA Object Creation in R Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Use this function when you have observed values and their corresponding time points in separate lists. The generated object contains essential FPCA information. ```R FPCAobj <- FPCA(Ly=yList, Lt=tList) ``` -------------------------------- ### Create Design Plot Source: https://context7.com/functionaldata/tpace/llms.txt Generates a plot visualizing the observation patterns for each subject in a functional data object. ```r library(fdapace) set.seed(1) data(medfly25) Flies <- MakeFPCAInputs(medfly25$ID, medfly25$Days, medfly25$nEggs) fpca_flies <- FPCA(Flies$Ly, Flies$Lt, list(plot = FALSE, methodMuCovEst = 'smooth', userBwCov = 2)) CreateDesignPlot(fpca_flies) ``` -------------------------------- ### Plot Singular Functions Source: https://context7.com/functionaldata/tpace/llms.txt Plots singular functions for two samples. Uses matplot for visualization. ```r par(mfrow = c(1, 2)) matplot(fsvd_result$grid1, fsvd_result$sFun1, type = 'l', xlab = 't', ylab = '', main = 'Singular Functions (Sample 1)') matplot(fsvd_result$grid2, fsvd_result$sFun2, type = 'l', xlab = 't', ylab = '', main = 'Singular Functions (Sample 2)') ``` -------------------------------- ### Calculate Dynamic Correlation with DynCorr Source: https://context7.com/functionaldata/tpace/llms.txt Calculates dynamic correlation for paired dense functional data using the DynCorr function. Requires the fdapace library. Includes data simulation and visualization of results. ```r library(fdapace) set.seed(10) # Simulate paired functional data with correlation n <- 200 t <- seq(0, 1, length.out = 100) # Mean functions mu_x <- 8 * t^2 - 4 * t + 5 mu_y <- 8 * t^2 - 12 * t + 6 # Basis functions fun <- rbind(rep(1, length(t)), -t, t^2) # Generate correlated latent scores z <- matrix(0, n, 3) z[, 1] <- rnorm(n, 0, 2) z[, 2] <- rnorm(n, 0, 16/3) z[, 3] <- rnorm(n, 0, 4) # Generate functional data x <- y <- matrix(0, nrow = n, ncol = length(t)) for (i in 1:n) { x[i, ] <- mu_x + z[i, ] %*% fun + rnorm(length(t), 0, 0.01) y[i, ] <- mu_y + 2 * z[i, ] %*% fun + rnorm(length(t), 0, 0.01) } # Calculate dynamic correlation dyn_corr <- DynCorr(x, y, t) # dyn_corr is a vector of individual dynamic correlations # Overall dynamic correlation is the mean cat("Mean dynamic correlation:", round(mean(dyn_corr), 3), "\n") cat("SD of individual correlations:", round(sd(dyn_corr), 3), "\n") # Histogram of individual correlations hist(dyn_corr, breaks = 20, main = 'Distribution of Dynamic Correlations', xlab = 'Dynamic Correlation', col = 'lightblue') abline(v = mean(dyn_corr), col = 'red', lwd = 2) ``` -------------------------------- ### Sparsify Dense Data Source: https://context7.com/functionaldata/tpace/llms.txt Applies different sparsification techniques to existing dense functional data. Options include basic random sparsification, aggressive sparsification with guaranteed spacing, and fragment sparsification within subintervals. ```r dense_data <- matrix(rnorm(100 * 50), nrow = 100, ncol = 50) sparse1 <- Sparsify(dense_data, pts, sparsity = 3:7) sparse2 <- Sparsify(dense_data, pts, sparsity = 3:7, aggressive = TRUE) sparse3 <- Sparsify(dense_data, pts, sparsity = 3:7, fragment = 0.3) ``` -------------------------------- ### Perform Sparse FPCA Source: https://github.com/functionaldata/tpace/blob/master/vignettes/fdapaceVig.html Applies FPCA to the sparse and noisy dataset. Note that sparse FPCA includes internal smoothing, which is computationally intensive and based on the method by Yao et al. (2005). ```R # Do FPCA on this sparse sample # Notice that sparse FPCA will smooth the data internally (Yao et al., 2005) # Smoothing is the main computational cost behind sparse FPCA FPCAsparse <- FPCA(ySparse$yNoisy, ySparse$Lt, list(plot = TRUE)) ```