unina-sfere / funcharts

The goal of funcharts is to provide control charts for functional data.
2 stars 1 forks source link

Projecting test data #1

Open Ann-Lilian-Wangui opened 1 year ago

Ann-Lilian-Wangui commented 1 year ago

I am doing functional principal component analysis on time series data for fault detection using funcharts, I'm facing difficulties in projecting test data onto the FPCs of the train data.

Kindly assist; Thankyou.

capezza commented 1 year ago

Dear @Ann-Lilian-Wangui

Thank you for your feedback. I have not been notified of this opened issue, which I have discovered only now. Then, I am sorry for replying late.

At the moment, funcharts does not have a predict function for the pca model, it is something that we know we need to implement soon. However, with some code that uses a couple of internal functions in funcharts you can obtain what you want. Here is a simple example.

First, let us load the library and simulate some train and test data,

library(funcharts)
dat_train <- simulate_mfd(nobs = 100)
dat_test <- simulate_mfd(nobs = 25)

Then, we get the corresponding mfd objects as follows:

x_train <- get_mfd_list(dat_train$X_list)
x_test <- get_mfd_list(dat_test$X_list)
pca <- pca_mfd(x_train, scale = TRUE)

Then, we perform multivariate functional principal component analysis on the train data. I set the argument scale = TRUE although it is default. Then, we select the number of components that explains 80% of the variability.

pca <- pca_mfd(x_train, scale = TRUE)
n_pc <- which(cumsum(pca$varprop) > 0.8)[1]

Then, we scale the test data with respect to the mean and standard deviation functions calculated on the training data (they are returned by pca_mfd as center_fd and scale_fd)

x_test_scale <- scale_mfd(x_test, center = pca$center_fd, scale = pca$scale_fd)

Finally, there are a couple of internal functions available in funcharts (use the three colons symbol :::). The first one, get_scores, returns the scores for the test data, the second one get_fit_pca get the projection of the test data onto the subspace spanned by the selected principal components.

scores_test <- funcharts:::get_scores(pca, components = 1:n_pc, newdata_scaled = x_test_scale)
xtest_hat <- funcharts:::get_fit_pca(pca, components = 1:n_pc newdata_scaled = x_test_scale)

I hope this is helpful. Best, Christian