From 3092f6c3f78b0ee7aee222e02e090d9d2db16552 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 29 Oct 2021 18:16:40 +0200 Subject: [PATCH] wip: convert to R-package --- tensorPredictors/DESCRIPTION | 13 +++++++++++++ tensorPredictors/NAMESPACE | 7 +++++++ tensorPredictors/R/approx_kronecker.R | 1 + tensorPredictors/R/lsir.R | 3 +-- tensorPredictors/R/matpow.R | 1 + tensorPredictors/R/multi_assign.R | 1 + tensorPredictors/R/pca2d.R | 6 ++++-- tensorPredictors/R/poi.R | 4 +++- tensorPredictors/R/tensor_predictors.R | 13 ++++++++++--- tensorPredictors/src/poi.c | 14 +++++++++++++- 10 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 tensorPredictors/DESCRIPTION create mode 100644 tensorPredictors/NAMESPACE diff --git a/tensorPredictors/DESCRIPTION b/tensorPredictors/DESCRIPTION new file mode 100644 index 0000000..7408a70 --- /dev/null +++ b/tensorPredictors/DESCRIPTION @@ -0,0 +1,13 @@ +Package: tensorPredictors +Type: Package +Title: Tensor/Matrix valued predictors for regression and dimension reduction +Version: 0.1 +Date: 2021-10-28 +Author: Daniel Kapla [aut] +Maintainer: Daniel Kapla +Description: Multiple methods using tensor/matrix valued predictors for + regression or sufficient dimension reduction. +License: GPL-3 +Depends: +Encoding: UTF-8 +RoxygenNote: 7.1.1 diff --git a/tensorPredictors/NAMESPACE b/tensorPredictors/NAMESPACE new file mode 100644 index 0000000..054d77d --- /dev/null +++ b/tensorPredictors/NAMESPACE @@ -0,0 +1,7 @@ +# Generated by roxygen2: do not edit by hand + +export(LSIR) +export(PCA2d) +export(POI) +export(approx.kronecker) +useDynLib(tensorPredictors, .registration = TRUE) diff --git a/tensorPredictors/R/approx_kronecker.R b/tensorPredictors/R/approx_kronecker.R index e2bba25..e878fa0 100644 --- a/tensorPredictors/R/approx_kronecker.R +++ b/tensorPredictors/R/approx_kronecker.R @@ -22,6 +22,7 @@ #' #' @imports RSpectra #' +#' @export approx.kronecker <- function(C, dimA, dimB) { dim(C) <- c(dimB[1L], dimA[1L], dimB[2L], dimA[2L]) diff --git a/tensorPredictors/R/lsir.R b/tensorPredictors/R/lsir.R index 6ae22ed..1fef43a 100644 --- a/tensorPredictors/R/lsir.R +++ b/tensorPredictors/R/lsir.R @@ -1,5 +1,3 @@ -source('../tensor_predictors/matpow.R') - #' Longitudinal Sliced Inverse Regression #' #' @param X matrix of dim \eqn{n \times p t} with each row representing a @@ -13,6 +11,7 @@ source('../tensor_predictors/matpow.R') #' #' TODO: finish #' +#' @export LSIR <- function(X, y, p, t, k = 1L, r = 1L) { # the code assumes: # alpha: T x r, beta: p x k, X_i: p x T, for ith observation diff --git a/tensorPredictors/R/matpow.R b/tensorPredictors/R/matpow.R index c057b10..7102876 100644 --- a/tensorPredictors/R/matpow.R +++ b/tensorPredictors/R/matpow.R @@ -55,6 +55,7 @@ #' all.equal(B %*% A, t(B %*% A)) #' all.equal(A %*% B, t(A %*% B)) #' +#' @keywords internal matpow <- function(A, pow, tol = 1e-7) { if (nrow(A) != ncol(A)) { stop("Expected a square matix, but 'A' is ", nrow(A), " by ", ncol(A)) diff --git a/tensorPredictors/R/multi_assign.R b/tensorPredictors/R/multi_assign.R index c46c18c..fa7138a 100644 --- a/tensorPredictors/R/multi_assign.R +++ b/tensorPredictors/R/multi_assign.R @@ -22,6 +22,7 @@ #' c(d1, d2, d3) %<-% 1:10 #' extracting the first three valus from the vector of length 10. #' +#' @keywords internal "%<-%" <- function(lhs, rhs) { var.names <- make.names(as.list(substitute(lhs))[-1]) values <- as.list(rhs) diff --git a/tensorPredictors/R/pca2d.R b/tensorPredictors/R/pca2d.R index c1745c7..1be6e9b 100644 --- a/tensorPredictors/R/pca2d.R +++ b/tensorPredictors/R/pca2d.R @@ -1,4 +1,5 @@ - +#'2-Dimensional Principal Component Analysis +#' #' @param X Matrix of dim (n, p * t) with each row the vectorized (p, t) observation. #' @param p nr. predictors #' @param t nr. timepoints @@ -7,7 +8,8 @@ #' #' @details The `i`th observation is stored in a row such that its matrix equiv #' is given by `matrix(X[i, ], p, t)`. -#' +#' +#' @export PCA2d <- function(X, p, t, ppc, tpc, scale = FALSE) { stopifnot(ncol(X) == p * t, ppc <= p, tpc <= t) diff --git a/tensorPredictors/R/poi.R b/tensorPredictors/R/poi.R index 9971868..3fe1a08 100644 --- a/tensorPredictors/R/poi.R +++ b/tensorPredictors/R/poi.R @@ -4,6 +4,8 @@ #' #' @note use.C required 'poi.so' beeing dynamicaly loaded. #' dyn.load('../tensor_predictors/poi.so') +#' +#' @export POI <- function(A, B, d, lambda = 0.75 * sqrt(max(rowSums(Delta^2))), update.tol = 1e-3, @@ -33,7 +35,7 @@ POI <- function(A, B, d, # The "inner" optimization loop, aka repeated coordinate optimization. if (use.C) { Z <- .Call('FastPOI_C_sub', A, B, Delta, lambda, as.integer(maxit.inner), - PACKAGE = 'poi') + PACKAGE = 'tensorPredictors') } else { p <- nrow(Z) for (iter.inner in 1:maxit.inner) { diff --git a/tensorPredictors/R/tensor_predictors.R b/tensorPredictors/R/tensor_predictors.R index 0d94dd7..e5994a1 100644 --- a/tensorPredictors/R/tensor_predictors.R +++ b/tensorPredictors/R/tensor_predictors.R @@ -1,6 +1,13 @@ -source('../tensor_predictors/matpow.R') -source('../tensor_predictors/multi_assign.R') -source('../tensor_predictors/approx_kronecker.R') +#' Tensor/Matrix valued Predictors for Regression and Dimension Reduction +#' +#' Multiple methods using tensor/matrix valued predictors for regression or +#' sufficient dimension reduction. +#' +#' @author Daniel Kapla +#' +#' @docType package +#' @useDynLib tensorPredictors, .registration = TRUE +"_PACKAGE" log.likelihood <- function(par, X, Fy, Delta.inv, da, db) { alpha <- matrix(par[1:prod(da)], da[1L]) diff --git a/tensorPredictors/src/poi.c b/tensorPredictors/src/poi.c index 0dc836e..b3c7f93 100644 --- a/tensorPredictors/src/poi.c +++ b/tensorPredictors/src/poi.c @@ -3,7 +3,8 @@ #include #include -SEXP FastPOI_C_sub(SEXP in_A, SEXP in_B, SEXP in_Delta, SEXP in_lambda, SEXP in_maxit) { +/* invoced by .Call */ +extern SEXP FastPOI_C_sub(SEXP in_A, SEXP in_B, SEXP in_Delta, SEXP in_lambda, SEXP in_maxit) { int i, j, k, g; int p = nrows(in_Delta); @@ -72,3 +73,14 @@ SEXP FastPOI_C_sub(SEXP in_A, SEXP in_B, SEXP in_Delta, SEXP in_lambda, SEXP in_ UNPROTECT(1); return out_Z; } + +static const R_CallMethodDef CallEntries[] = { + {"FastPOI_C_sub", (DL_FUNC) &FastPOI_C_sub, 5}, + {NULL, NULL, 0} +}; + +/* Restrict C entry points to registered routines. */ +void R_init_CVE(DllInfo *dll) { + R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); + R_useDynamicSymbols(dll, FALSE); +}