diff --git a/DESCRIPTION b/DESCRIPTION index f99a4e5..ec58a29 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,8 @@ Suggests: pscl, knitr, rmarkdown, - testthat + testthat, + rstudioapi URL: https://github.com/qpsy/nonnest2 VignetteBuilder: knitr RoxygenNote: 7.3.3 diff --git a/R/llcont.R b/R/llcont.R index 5896c33..caa8fa1 100644 --- a/R/llcont.R +++ b/R/llcont.R @@ -51,7 +51,8 @@ llcont.glm <- function(x, ...){ switch(fam, binomial = { if(is.matrix(y)) { - n <- apply(y, 1, sum) + # Optimization: Using rowSums instead of apply for faster vectorized execution + n <- rowSums(y) y <- ifelse(n == 0, 0, y[, 1]/n) } else { n <- rep.int(1, length(y)) @@ -339,7 +340,8 @@ llcont.polr <- function(x, ...) { idx <- matrix(0, nrow=length(y), ncol=length(x$lev)) idx[wherey] <- 1 - model.weights(m) * log(apply(idx * x$fitted.values, 1, sum)) + # Optimization: Using rowSums instead of apply for faster vectorized execution + model.weights(m) * log(rowSums(idx * x$fitted.values)) } ################################################################ @@ -407,7 +409,8 @@ llcont.lavaan <- function(x, ...){ Mu.hat <- unclass(moments$mean) } else { ## set mean structure to sample estimates - Mu.hat <- apply(x@Data@X[[g]], 2, mean) + # Optimization: Using colMeans instead of apply for faster vectorized execution + Mu.hat <- colMeans(x@Data@X[[g]]) } llvec[grpind] <- dmvnorm(x@Data@X[[g]], Mu.hat, Sigma.hat, log=TRUE) diff --git a/tests/testthat.R b/tests/testthat.R index dda98ed..8c36737 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,4 +1,6 @@ -library(testthat) -library(nonnest2) +if (requireNamespace('testthat', quietly = TRUE)) { + library(testthat) + library(nonnest2) -test_check("nonnest2") + test_check('nonnest2') +}