## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", cache = FALSE, fig.width = 7, fig.height = 4, out.width = "100%" ) ## ----install, eval = FALSE---------------------------------------------------- # # install.packages("devtools") # devtools::install_github("steviek16/fda.vi") ## ----quickstart--------------------------------------------------------------- library(fda.vi) data(toy_curves) # Fit at a single K fit <- vem_fit( y = toy_curves$y, Xt = toy_curves$Xt, K = 8, center = FALSE, scale = FALSE ) summary(fit) ## ----data--------------------------------------------------------------------- data(toy_curves) str(toy_curves) ## ----plot-toy----------------------------------------------------------------- plot(toy_curves$Xt, toy_curves$y[[1]], type = "p", pch = 16, cex = 0.6, col = "steelblue", xlab = "t", ylab = "y(t)", main = "Toy Curves Dataset") for (i in 2:3) { points(toy_curves$Xt, toy_curves$y[[i]], pch = 16, cex = 0.6, col = c("firebrick", "forestgreen")[i - 1]) } legend("topright", legend = paste("Curve", 1:3), col = c("steelblue", "firebrick", "forestgreen"), pch = 16, bty = "n") ## ----single-k----------------------------------------------------------------- fit <- vem_fit( y = toy_curves$y, Xt = toy_curves$Xt, K = 8, center = FALSE, scale = FALSE ) ## ----gcv-k-------------------------------------------------------------------- fit_gcv <- vem_fit( y = toy_curves$y, Xt = toy_curves$Xt, K = c(6, 8, 10, 15) ) fit_gcv$best_K fit_gcv$tuning$gcv_matrix ## ----per-curve---------------------------------------------------------------- fit_pc <- vem_fit( y = toy_curves$y, Xt = toy_curves$Xt, K = c(6, 8, 10), selection_metric = "per_curve" ) fit_pc$selected_K fit_pc$is_composite ## ----fourier------------------------------------------------------------------ fit_f <- vem_fit( y = toy_curves$y, Xt = toy_curves$Xt, K = 10, basis_type = "fourier" ) summary(fit_f) ## ----summary------------------------------------------------------------------ summary(fit) ## ----coef--------------------------------------------------------------------- coef(fit) ## ----coef-check--------------------------------------------------------------- coefs <- coef(fit) coefs[c(2, 5), ] # should be zero ## ----pips--------------------------------------------------------------------- K <- fit$best_K m <- length(toy_curves$y) pip_mat <- matrix(fit$model$prob, nrow = K, ncol = m) rownames(pip_mat) <- paste0("B", 1:K) colnames(pip_mat) <- paste0("Curve_", 1:m) round(pip_mat, 3) ## ----predict------------------------------------------------------------------ # Predictions at original evaluation points preds <- predict(fit) length(preds) # one vector per curve length(preds[[1]]) # same length as Xt # Predictions at a denser grid Xt_new <- seq(0, 1, length.out = 200) preds_new <- predict(fit, newdata = Xt_new) ## ----plot--------------------------------------------------------------------- # Fitted curve with 95% credible band for curve 1 plot(fit, curve_idx = 1) ## ----plot-all----------------------------------------------------------------- # All three curves for (i in 1:3) plot(fit, curve_idx = i) ## ----citation----------------------------------------------------------------- citation("fda.vi")