################################################# # Demonstration of the sFPCA package ################################################# # install the package # install these three required packages first. If you cannot install them, please upgrade your R to the latest version install.packages("devtools") install.packages("dplyr") install.packages("fda") # load these three packages library(fda) library(dplyr) library(devtools) # download the sFPCA from github and install it automatically devtools::install_github("YunlongNie/sFPCA") # load the sFPCA package library(sFPCA) # a demo for the first simulation study in the paper (section 5.1) # load the dataset; this data set contains 1000 replicates in total data(binary_dat); # xmat is a 365*1000 matrix with each column representing to the trajectory for one replicate at 365 time points xmat = binary_dat$x; # y is the vector of length 1000, each element represents the binary response for one replicate y=binary_dat$y # input the xmat matrix, the y response vector, the weight parameter theta, the number of sFPCs one is trying to estimate and a new matrix to make predictions on (here I just use the same xmat to do the predictions) # we first set the theta to be 0.1, for the supervised FPC method supervised_res = sFPCA::sfpcs_binary(xmat,y,npc_select=2,theta=0.1,xmat_new = xmat) # the fitted value for 1000 replicates supervised_res$fitted # the proportion that the fitted values match the observed responses mean(y==supervised_res$fitted) # the estimated sFPCAs are stored in the supervised_res$sfpcs supervised_res$sfpcs # a list with each element corresponding to one sFPC # we can visualize them by plot(supervised_res$sfpcs[[1]], main="first supervised FPC") plot(supervised_res$sfpcs[[2]], main="second supervised FPC") # we then set the theta to be 1, for the unsupervised FPC method unsupervised_res = sFPCA::sfpcs_binary(xmat,y,npc_select=2,theta=1,xmat_new = xmat) # the fitted value for 1000 replicates unsupervised_res$fitted # the proportion that the fitted values match the observed responses mean(y==unsupervised_res$fitted) # the estimated FPCAs are stored in the unsupervised_res$sfpcs unsupervised_res$sfpcs # a list with each element corresponding to one FPC # we can visualize them by plot(unsupervised_res$sfpcs[[1]], main="unsupervised FPC") plot(unsupervised_res$sfpcs[[2]], main="unsupervised FPC")