Skip to content

Commit

Permalink
Refine the resultant plots (#25)
Browse files Browse the repository at this point in the history
* CHG: refine the facet label by adding math expression

* FIX: correct grammar erros; correct the attribute name; update the manual

---------

Co-authored-by: joseph.wangwt <joseph.wangwt@shopee.com>
  • Loading branch information
egpivo and joseph.wangwt authored Jan 28, 2023
1 parent 91b652a commit 5b273be
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Authors@R: c(person(
comment = c(ORCID = "0000-0002-5613-349X")
)
)
Description: Provide regularized principal component analysis incorporating smoothness, sparseness and orthogonality of eigen-functions
Description: Provide regularized principal component analysis incorporating smoothness, sparseness and orthogonality of eigenfunctions
by using the alternating direction method of multipliers algorithm (Wang and Huang, 2017, <DOI:10.1080/10618600.2016.1157483>). The
method can be applied to either regularly or irregularly spaced data, including 1D, 2D, and 3D.
License: GPL-3
Expand Down
71 changes: 48 additions & 23 deletions R/SpatPCA.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# This file was generated by Rcpp::compileAttributes
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#'
#' Internal function: M-fold CV of SpatPCA with selecting K
#' Internal function: M-fold CV of SpatPCA with selected K
#'
#' @keywords internal
#'
#' @param x Location matrix
#' @param Y Data matrix
#' @param M The number of folds for cross validation; default is 5.
#' @param tau1 Vector of a nonnegative smoothness parameter sequence. If NULL, 10 tau1 values in a range are used.
#' @param tau2 Vector of a nonnegative sparseness parameter sequence. If NULL, none of tau2 is used.
#' @param gamma Vector of a nonnegative hyper parameter sequence for tuning eigenvalues. If NULL, 10 values in a range are used.
#' @param tau1 Vector of a non-negative smoothness parameter sequence. If NULL, 10 tau1 values in a range are used.
#' @param tau2 Vector of a non-negative sparseness parameter sequence. If NULL, none of tau2 is used.
#' @param gamma Vector of a non-negative hyper parameter sequence for tuning eigenvalues. If NULL, 10 values in a range are used.
#' @param shuffle_split Vector of indices for random splitting Y into training and test sets
#' @param maxit Maximum number of iterations. Default value is 100.
#' @param thr Threshold for convergence. Default value is \eqn{10^{-4}}.
#' @param l2 Vector of a nonnegative tuning parameter sequence for ADMM use
#' @param l2 Vector of a non-negative tuning parameter sequence for ADMM use
#' @return A list of objects including
#' \item{cv_result}{A list of resultant objects produced by `spatpcaCV`}
#' \item{selected_K}{Selected K based on CV.}
#'
spatpcaCVWithSelectingK <- function(x, Y, M, tau1, tau2, gamma, shuffle_split, maxit, thr, l2) {
spatpcaCVWithSelectedK <- function(x, Y, M, tau1, tau2, gamma, shuffle_split, maxit, thr, l2) {
upper_bound <- fetchUpperBoundNumberEigenfunctions(Y, M)
prev_cv_selection <- spatpcaCV(x, Y, M, 1, tau1, tau2, gamma, shuffle_split, maxit, thr, l2)

Expand All @@ -43,9 +43,9 @@ spatpcaCVWithSelectingK <- function(x, Y, M, tau1, tau2, gamma, shuffle_split, m
#' @param Y Data matrix (\eqn{n \times p}) stores the values at \eqn{p} locations with sample size \eqn{n}.
#' @param K Optional user-supplied number of eigenfunctions; default is NULL. If K is NULL or is_K_selected is TRUE, K is selected automatically.
#' @param is_K_selected If TRUE, K is selected automatically; otherwise, is_K_selected is set to be user-supplied K. Default depends on user-supplied K.
#' @param tau1 Optional user-supplied numeric vector of a nonnegative smoothness parameter sequence. If NULL, 10 tau1 values in a range are used.
#' @param tau2 Optional user-supplied numeric vector of a nonnegative sparseness parameter sequence. If NULL, none of tau2 is used.
#' @param gamma Optional user-supplied numeric vector of a nonnegative tuning parameter sequence. If NULL, 10 values in a range are used.
#' @param tau1 Optional user-supplied numeric vector of a non-negative smoothness parameter sequence. If NULL, 10 tau1 values in a range are used.
#' @param tau2 Optional user-supplied numeric vector of a non-negative sparseness parameter sequence. If NULL, none of tau2 is used.
#' @param gamma Optional user-supplied numeric vector of a non-negative tuning parameter sequence. If NULL, 10 values in a range are used.
#' @param M Optional number of folds for cross validation; default is 5.
#' @param is_Y_detrended If TRUE, center the columns of Y. Default is FALSE.
#' @param maxit Maximum number of iterations. Default value is 100.
Expand Down Expand Up @@ -174,7 +174,7 @@ spatpca <- function(x,


if (is_K_selected) {
cv_with_selected_k <- spatpcaCVWithSelectingK(scaled_x, Y, M, tau1, tau2, gamma, shuffle_split, maxit, thr, l2)
cv_with_selected_k <- spatpcaCVWithSelectedK(scaled_x, Y, M, tau1, tau2, gamma, shuffle_split, maxit, thr, l2)
cv_result <- cv_with_selected_k$cv_result
selected_K <- cv_with_selected_k$selected_K
}
Expand Down Expand Up @@ -246,7 +246,7 @@ predictEigenfunction <- function(spatpca_object, x_new) {

#' @title Spatial predictions on new locations
#'
#' @description Predict on new locations with the estimated spatial structures.
#' @description Predict the response on new locations with the estimated spatial structures.
#'
#' @param spatpca_object An `spatpca` class object
#' @param x_new New location matrix.
Expand All @@ -259,12 +259,12 @@ predictEigenfunction <- function(spatpca_object, x_new) {
#' Phi_1D <- exp(-x_1D^2) / norm(exp(-x_1D^2), "F")
#' set.seed(1234)
#' Y_1D <- rnorm(n = 100, sd = 3) %*% t(Phi_1D) + matrix(rnorm(n = 100 * 10), 100, 10)
#' rm_loc <- sample(1:10, 3)
#' x_1Drm <- x_1D[-rm_loc]
#' Y_1Drm <- Y_1D[, -rm_loc]
#' x_1Dnew <- as.matrix(seq(-5, 5, length = 20))
#' cv_1D <- spatpca(x = x_1Drm, Y = Y_1Drm, tau2 = 1:100, num_cores = 2)
#' predictions <- predict(cv_1D, x_new = x_1Dnew)
#' removed_location <- sample(1:10, 3)
#' removed_x_1D <- x_1D[-removed_location]
#' removed_Y_1D <- Y_1D[, -removed_location]
#' new_x_1D <- as.matrix(seq(-5, 5, length = 20))
#' cv_1D <- spatpca(x = removed_x_1D, Y = removed_Y_1D, tau2 = 1:100, num_cores = 2)
#' predictions <- predict(cv_1D, x_new = new_x_1D)
#'
predict <- function(spatpca_object, x_new, eigen_patterns_on_new_site = NULL) {
checkNewLocationsForSpatpcaObject(spatpca_object, x_new)
Expand All @@ -279,7 +279,7 @@ predict <- function(spatpca_object, x_new, eigen_patterns_on_new_site = NULL) {
spatpca_object$selected_gamma,
eigen_patterns_on_new_site
)
return(spatial_prediction$predict)
return(spatial_prediction$prediction)
}

#'
Expand Down Expand Up @@ -316,30 +316,55 @@ plot.spatpca <- function(x, ...) {
plot.title = element_text(hjust = 0.5)
)
)
tau1_hat_string = paste(
c("hat(tau)[1]==", formatC(x$selected_tau1, format = "f", digits = 3)),
collapse = ""
)
tau2_hat_string = paste(
c("hat(tau)[2]==", formatC(x$selected_tau2, format = "f", digits = 3)),
collapse = ""
)

parameter_types = c(
"tau[1]~'|'~tau[2]==0",
paste(c("tau[2]~'|'~", tau1_hat_string), collapse=""),
paste(c("gamma~'|'~list(", tau1_hat_string, ",", tau2_hat_string, ")"),collapse="")
)


cv_dataframe <- rbind(
data.frame(
type = "tau1 given tau2 = 0",
type = parameter_types[1],
parameter = array(x$tau1),
cv = array(x$cv_score_tau1)
),
data.frame(
type = "tau2 given selected tau1",
type = parameter_types[2],
parameter = array(x$tau2),
cv = array(x$cv_score_tau2)
),
data.frame(
type = "gamma given selected tau1 and tau2",
type = parameter_types[3],
parameter = array(x$gamma),
cv = array(x$cv_score_gamma)
)

)
cv_dataframe$type = factor(cv_dataframe$type, levels=parameter_types)

result <-
ggplot(
cv_dataframe,
aes(x = parameter, y = cv, color = type)
) +
geom_line(linewidth = 1.5) +
facet_grid(scales = "free", . ~ type)
geom_line(linewidth = 1.5)+
facet_grid(
scales = "free",
. ~ type,
labeller = labeller(type=label_parsed)) +
ggtitle("Result of K-fold CV")


return(suppressMessages(print(result)))
}

14 changes: 7 additions & 7 deletions man/predict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/spatpca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions man/spatpcaCVWithSelectingK.Rd → man/spatpcaCVWithSelectedK.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-SpatPCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ tau2 <- setTau2(NULL, M)
l2 <- setL2(tau2)
setCores(2)
cv_with_k_seleted <-
spatpcaCVWithSelectingK(x_1D, Y_1D, M, tau1, tau2, 1, shuffle_split, 10, 1e-04, l2)
spatpcaCVWithSelectedK(x_1D, Y_1D, M, tau1, tau2, 1, shuffle_split, 10, 1e-04, l2)

test_that("auxiliary function for selecting K", {
expect_equal(cv_with_k_seleted$selected_K, 1)
Expand Down

0 comments on commit 5b273be

Please sign in to comment.