Skip to content

Commit

Permalink
Changed output to matrix when single polynomial is being used
Browse files Browse the repository at this point in the history
  • Loading branch information
moralapablo committed Nov 20, 2024
1 parent 137592f commit 6fc8845
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions R/eval_monomials.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
#'
#' @inheritParams eval_poly
#'
#' @returns A 3D array where the dimensions are:
#' (n_samples,
#' n_monomial_terms,
#' n_polynomials).
#'
#' If n_polynomials = 1, a single matrix is returned with dimensions
#' (n_samples,
#' n_monomial_terms)
#'
#' @seealso \code{eval_monomials()} is also used in [predict.nn2poly()].
#'
eval_monomials <- function(poly, newdata) {
Expand All @@ -25,8 +34,8 @@ eval_monomials <- function(poly, newdata) {
# polynomials and columns equal to the number of observations evaluated.
n_sample <- nrow(newdata)
n_polynomials <- ncol(poly$values)
n_terms <- length(poly$labels)
response <- array(0,c(n_sample, n_terms, n_polynomials))
n_monomial_terms <- length(poly$labels)
response <- array(0,c(n_sample, n_monomial_terms, n_polynomials))

for (k in 1:n_polynomials){

Expand Down Expand Up @@ -67,5 +76,11 @@ eval_monomials <- function(poly, newdata) {
n_sample)
}

# If single polynomial, the third dimension should be 1 and then we
# can return a matrix instead of a 3D array to simplify its use.
if (n_polynomials==1){
response <- response[,,1]
}

return(response)
}

0 comments on commit 6fc8845

Please sign in to comment.