diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 028b4635..55e14a7b 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -22,7 +22,7 @@ jobs: permissions: contents: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -30,25 +30,26 @@ jobs: with: use-public-rspm: true - - name: Install dependencies Ubuntu - if: runner.os == 'Linux' - run: | - install.packages('remotes') - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("knitr") - remotes::install_cran("rmarkdown") - remotes::install_cran("rcmdcheck") - install.packages("Matrix", type = "source") - install.packages("TMB", type = "source") - shell: Rscript {0} - - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: any::pkgdown, local::. needs: website + cache-version: 2 + + - name: Reinstall TMB/Matrix from source + run: | + install.packages(c("Matrix", "TMB"), type = "source") + shell: Rscript {0} + + - name: Check systemfonts + run: | + print(.libPaths()) + print(find.package("systemfonts")) + print(packageVersion("systemfonts")) + shell: Rscript {0} - name: Build site - run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) + run: pkgdown::build_site_github_pages(new_process = TRUE, install = FALSE) shell: Rscript {0} - name: Deploy to GitHub pages 🚀 @@ -57,4 +58,4 @@ jobs: with: clean: false branch: gh-pages - folder: docs + folder: docs \ No newline at end of file diff --git a/.github/workflows/san-check.yaml b/.github/workflows/san-check.yaml index 37f07c49..38e258ff 100644 --- a/.github/workflows/san-check.yaml +++ b/.github/workflows/san-check.yaml @@ -21,14 +21,41 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Show compiler configuration + run: | + for v in CXX CXX17 CXX17FLAGS CXX17STD CXXFLAGS LDFLAGS; do + echo "$v = $(R CMD config $v)" + done + + - name: Compile dependencies against libstdc++ (matches system GDAL) + run: | + mkdir -p ~/.R + : > ~/.R/Makevars + strip_stdlib() { sed -e 's/-stdlib=[A-Za-z+]*//g'; } + for s in "" 11 14 17 20; do + cxx=$(R CMD config CXX$s 2>/dev/null | strip_stdlib || true) + flags=$(R CMD config CXX${s}FLAGS 2>/dev/null | strip_stdlib || true) + if [ -n "$cxx" ]; then + echo "CXX$s = $cxx -stdlib=libstdc++" >> ~/.R/Makevars + echo "CXX${s}FLAGS = $flags" >> ~/.R/Makevars + fi + done + echo "LDFLAGS = $(R CMD config LDFLAGS | strip_stdlib)" >> ~/.R/Makevars + echo "---- ~/.R/Makevars ----" + cat ~/.R/Makevars + - name: Install dependencies run: | R -q -e 'install.packages("pak")' R -q -e 'pak::pkg_install(c("local::.", "rcmdcheck", "knitr", "rmarkdown", "AER", "MARSS", "testthat", "RANN"))' - # R -q -e 'pak::pkg_install(c("phylopath", "reshape", "dynlm", "ggpubr", "vars", "DHARMa"))' # Oher SUGGESTS packages - - name: Run check + - name: Check that sf loads run: | - # R -q -e 'rcmdcheck::rcmdcheck(args = c("--no-manual", "--no-vignettes", "--no-build-vignettes", "--no-examples", "--no-tests"), build_args = c("--no-build-vignettes", "--no-build-vignettes"), error_on = "error")' - R -q -e 'rcmdcheck::rcmdcheck(args = c("--no-manual", "--no-vignettes", "--no-build-vignettes"), build_args = c("--no-build-vignettes", "--no-build-vignettes"), error_on = "error")' + R -q -e 'library(sf); print(sf::sf_extSoftVersion())' + + - name: Restore sanitizer compiler settings + run: rm -f ~/.R/Makevars + - name: Run check + run: | + R -q -e 'rcmdcheck::rcmdcheck(args = c("--no-manual", "--no-vignettes", "--no-build-vignettes"), build_args = c("--no-build-vignettes", "--no-build-vignettes"), error_on = "error")' \ No newline at end of file diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 6e560b38..888548b5 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -46,6 +46,8 @@ jobs: install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") ) shell: Rscript {0} + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - name: Show testthat output if: always() @@ -59,4 +61,4 @@ jobs: uses: actions/upload-artifact@v4 with: name: coverage-test-failures - path: ${{ runner.temp }}/package + path: ${{ runner.temp }}/package \ No newline at end of file diff --git a/.gitignore b/.gitignore index eefd9515..db28ed84 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ inst/doc docs *.pdf *.Rhistory +scratch/*.tar.gz vignettes\.Rhistory diff --git a/DESCRIPTION b/DESCRIPTION index 9282feb1..644a2e10 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: tinyVAST Title: Multivariate Spatio-Temporal Models using Structural Equations -Version: 1.6.3 -Date: 2026-09-19 +Version: 1.6.4 +Date: 2026-11-21 Authors@R: c( person(c("James", "T."), "Thorson", , "James.Thorson@noaa.gov", role = c("aut", "cre"), @@ -41,11 +41,11 @@ Imports: dsem (>= 3.0.0), insight, cv, - sparseinv, gstat, cli, GpGp, - GPvecchia + GPvecchia, + sparseinv Suggests: ggplot2, knitr, @@ -77,4 +77,4 @@ LazyDataCompression: xz Roxygen: list(markdown = TRUE) URL: https://vast-lib.github.io/tinyVAST/ BugReports: https://github.com/vast-lib/tinyVAST/issues/ -Config/roxygen2/version: 8.0.0 +Config/roxygen2/version: 8.1.1 diff --git a/NAMESPACE b/NAMESPACE index 4424a552..5bc4f7f3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,91 +45,119 @@ export(tweedie) import(methods) importFrom(GPvecchia,order_maxmin_exact) importFrom(GpGp,find_ordered_nn) -importFrom(Matrix,Cholesky) -importFrom(Matrix,Diagonal) -importFrom(Matrix,Matrix) -importFrom(Matrix,diag) -importFrom(Matrix,mat2triplet) -importFrom(Matrix,rowSums) -importFrom(Matrix,solve) -importFrom(Matrix,sparseMatrix) -importFrom(Matrix,t) -importFrom(TMB,MakeADFun) -importFrom(TMB,config) -importFrom(TMB,runSymbolicAnalysis) -importFrom(TMB,sdreport) +importFrom(Matrix, + Cholesky, + Diagonal, + Matrix, + diag, + mat2triplet, + rowSums, + solve, + sparseMatrix, + t +) +importFrom(TMB, + MakeADFun, + config, + runSymbolicAnalysis, + sdreport +) importFrom(abind,abind) -importFrom(checkmate,assertClass) -importFrom(checkmate,assertDataFrame) -importFrom(checkmate,assertInteger) -importFrom(checkmate,assertNumeric) -importFrom(cli,cli_abort) -importFrom(cli,cli_alert_danger) -importFrom(cli,cli_alert_info) -importFrom(cli,cli_alert_success) -importFrom(cli,cli_inform) -importFrom(cli,cli_warn) +importFrom(checkmate, + assertClass, + assertDataFrame, + assertInteger, + assertNumeric +) +importFrom(cli, + cli_abort, + cli_alert_danger, + cli_alert_info, + cli_alert_success, + cli_inform, + cli_warn +) importFrom(corpcor,pseudoinverse) -importFrom(cv,GetResponse) -importFrom(cv,cv) +importFrom(cv, + GetResponse, + cv +) importFrom(dsem,make_matrices) -importFrom(fmesher,fm_evaluator) -importFrom(fmesher,fm_fem) -importFrom(fmesher,fm_mesh_2d) -importFrom(igraph,as_adjacency_matrix) -importFrom(igraph,ecount) -importFrom(insight,get_data) -importFrom(insight,get_response) -importFrom(methods,as) -importFrom(methods,is) -importFrom(sdmTMB,lognormal) -importFrom(sdmTMB,nbinom1) -importFrom(sdmTMB,nbinom2) -importFrom(sdmTMB,student) -importFrom(sdmTMB,tweedie) -importFrom(sf,st_as_sf) -importFrom(sf,st_centroid) -importFrom(sf,st_coordinates) -importFrom(sf,st_crs) -importFrom(sf,st_distance) -importFrom(sf,st_geometry) -importFrom(sf,st_geometry_type) -importFrom(sf,st_length) -importFrom(sf,st_nearest_feature) -importFrom(sf,st_relate) -importFrom(sf,st_within) +importFrom(fmesher, + fm_evaluator, + fm_fem, + fm_mesh_2d +) +importFrom(igraph, + as_adjacency_matrix, + ecount +) +importFrom(insight, + get_data, + get_response +) +importFrom(methods, + as, + is +) +importFrom(sdmTMB, + lognormal, + nbinom1, + nbinom2, + student, + tweedie +) +importFrom(sf, + st_as_sf, + st_centroid, + st_coordinates, + st_crs, + st_distance, + st_geometry, + st_geometry_type, + st_length, + st_nearest_feature, + st_relate, + st_within +) importFrom(sfnetworks,activate) -importFrom(sparseinv,Takahashi_Davis) -importFrom(sparseinv,cholPermute) -importFrom(stats,.getXlevels) -importFrom(stats,Gamma) -importFrom(stats,as.formula) -importFrom(stats,binomial) -importFrom(stats,dist) -importFrom(stats,fitted) -importFrom(stats,gaussian) -importFrom(stats,lm) -importFrom(stats,logLik) -importFrom(stats,model.frame) -importFrom(stats,model.matrix) -importFrom(stats,model.offset) -importFrom(stats,model.response) -importFrom(stats,na.omit) -importFrom(stats,na.pass) -importFrom(stats,nlminb) -importFrom(stats,optim) -importFrom(stats,optimHess) -importFrom(stats,pnorm) -importFrom(stats,poisson) -importFrom(stats,predict) -importFrom(stats,rnorm) -importFrom(stats,sd) -importFrom(stats,simulate) -importFrom(stats,terms) -importFrom(stats,update) -importFrom(stats,update.formula) -importFrom(stats,vcov) +importFrom(sparseinv, + Takahashi_Davis, + cholPermute +) +importFrom(stats, + .getXlevels, + Gamma, + as.formula, + binomial, + dist, + fitted, + gaussian, + lm, + logLik, + model.frame, + model.matrix, + model.offset, + model.response, + na.omit, + na.pass, + nlminb, + optim, + optimHess, + pnorm, + poisson, + predict, + rnorm, + sd, + simulate, + terms, + update, + update.formula, + vcov +) importFrom(units,drop_units) -importFrom(utils,capture.output) -importFrom(utils,packageVersion) +importFrom(utils, + capture.output, + packageVersion +) useDynLib(tinyVAST, .registration = TRUE) diff --git a/NEWS.md b/NEWS.md index e5c7cf7d..0a27de6b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# tinyVAST 1.6.3.9999 + +* Add an experimental option for cAIC to avoid constructing a dense matrix prior to extracting its diagonal using sparseinv::Takahashi_Davis, and use spHess(random=TRUE) to efficiently access sparse Hessians +* Add what = "EDF" option to cAIC(.) +* Remove ti and te mgcv tests from CRAN +* Speed up `project` by adding argument `nsim` and using more efficient linear algebra +* Speed up TMB code on C++ side, e.g., by making DATA_UPDATE optional, making weights_i optional, using SCALE for scaling a precision matrix, and other smaller improvements + # tinyVAST 1.6.3 * Move most vignettes to web_only vignettes directory, and update interface for visreg in mgcv.Rmd vignette diff --git a/R/cAIC.R b/R/cAIC.R index 80c8e62b..e530b49e 100644 --- a/R/cAIC.R +++ b/R/cAIC.R @@ -3,6 +3,12 @@ #' Calculates the conditional Akaike Information criterion (cAIC). #' #' @param object Output from [tinyVAST()]. +#' @param what whether to get cAIC or the EDF (the latter grouped by type of random effect). +#' @param method whether to compute effective degrees of freedom using +#' the stable method \code{diag(solve(Hess, Hess_new))} +#' or a faster and experimental method +#' \code{-rowSums(sparseinv::Takahashi_Davis(-Hess) * t(Hess_new))}, +#' where the latter avoids constructing a dense matrix prior to extracting its diagonal #' #' @details cAIC is designed to optimize the expected out-of-sample predictive #' performance for new data that share the same random effects as the in-sample @@ -16,7 +22,7 @@ #' generative parameters. #' #' Both cAIC and EDF are calculated using Eq. 6 of Zheng, Cadigan, and Thorson -#' (2024). +#' (2026). #' #' For models that include profiled fixed effects, these profiles are turned #' off. @@ -25,12 +31,13 @@ #' cAIC value #' #' @references -#' Zheng, N., Cadigan, N., & Thorson, J. T. (2024). -#' A note on numerical evaluation of conditional Akaike information for -#' nonlinear mixed-effects models (arXiv:2411.14185). arXiv. -#' \doi{10.48550/arXiv.2411.14185} +#' Zheng, N., Cadigan, N., & Thorson, J. T. (2026). +#' A unifying theory for the evaluation of conditional Akaike information for +#' mixed-effects models. Journal of Statistical Computation and Simulation. +#' \doi{10.1080/00949655.2026.2736799} #' #' @examples +#' \donttest{ #' data( red_snapper ) #' red_snapper = droplevels(subset(red_snapper, Data_type=="Biomass_KG")) #' @@ -57,11 +64,16 @@ #' #' cAIC(fit) # conditional AIC #' AIC(fit) # marginal AIC +#' } #' #' @export cAIC <- -function( object ){ +function( object, + what = c("cAIC", "EDF"), + method = c("fast", "stable") ){ + what = match.arg(what) + method = match.arg(method) check_tinyVAST_version( object$internal$packageVersion ) #what = match.arg(what) #requireNamespace(Matrix) @@ -71,51 +83,71 @@ function( object ){ if( is.null(object$internal$control$profile) ){ obj = object$obj }else{ - obj = TMB::MakeADFun( data = data, - parameters = object$internal$parlist, - map = object$tmb_inputs$tmb_map, - random = object$tmb_inputs$tmb_random, - DLL = "tinyVAST", - profile = NULL ) + obj = TMB::MakeADFun( + data = data, + parameters = object$internal$parlist, + map = object$tmb_inputs$tmb_map, + random = object$tmb_inputs$tmb_random, + DLL = "tinyVAST", + profile = NULL + ) } # Make obj_new data$weights_i[] = 0 - obj_new = TMB::MakeADFun( data = data, - parameters = object$internal$parlist, - map = object$tmb_inputs$tmb_map, - random = object$tmb_inputs$tmb_random, - DLL = "tinyVAST", - profile = NULL ) + obj_new = TMB::MakeADFun( + data = data, + parameters = object$internal$parlist, + map = object$tmb_inputs$tmb_map, + random = object$tmb_inputs$tmb_random, + DLL = "tinyVAST", + profile = NULL + ) # par = obj$env$parList() parDataMode <- obj$env$last.par indx = obj$env$lrandom() + random_names = names(parDataMode)[indx] q = sum(indx) p = length(object$opt$par) - ## use - for Hess because model returns negative loglikelihood; - #cov_Psi_inv = -Hess_new[indx,indx]; ## this is the marginal prec mat of REs; - Hess_new = -Matrix::Matrix(obj_new$env$f(parDataMode,order=1,type="ADGrad"),sparse = TRUE) - Hess_new = Hess_new[indx,indx] + if( method == "stable" ){ + ## use - for Hess because model returns negative loglikelihood; + #cov_Psi_inv = -Hess_new[indx,indx]; ## this is the marginal prec mat of REs; + Hess_new = -Matrix::Matrix(obj_new$env$f(parDataMode,order=1,type="ADGrad"),sparse = TRUE) + Hess_new = Hess_new[indx,indx] - ## Joint hessian etc - Hess = -Matrix::Matrix(obj$env$f(parDataMode,order=1,type="ADGrad"),sparse = TRUE) - Hess = Hess[indx,indx] - negEDF = Matrix::diag(Matrix::solve(Hess, Hess_new)) + # Joint hessian etc + Hess = -Matrix::Matrix(obj$env$f(parDataMode,order=1,type="ADGrad"),sparse = TRUE) + Hess = Hess[indx,indx] - #if(what == "cAIC"){ + negEDF = Matrix::diag(Matrix::solve(Hess, Hess_new)) + }else{ + #if( !requireNamespace("sparseinv", quietly = TRUE) ){ + # stop("method = 'fast' requires the 'sparseinv' package; use method = 'stable' or install it") + #} + # h/t Jie Cao for suggesting spHess to avoid sparsifying a dense Hessian as done previously + # Also dropping negative signs (which all cancel) + Hess_new = obj_new$env$spHess(parDataMode, random = TRUE) + Hess = obj$env$spHess(parDataMode, random = TRUE) + #S = TMB:::solveSubset(Hess) + S = sparseinv::Takahashi_Davis(Hess) + negEDF = Matrix::rowSums(S * Hess_new) + } + + if(what == "cAIC"){ jnll = obj$env$f(parDataMode) cnll = jnll - obj_new$env$f(parDataMode) cAIC = 2*cnll + 2*(p+q) - 2*sum(negEDF) return(cAIC) - #} - #if(what == "EDF"){ - # group = colnames(object$tmb_inputs$tmb_data$Z_ik) - # group = sapply(group, FUN=\(char)strsplit(char,split=".",fixed=TRUE)[[1]][1]) - # group = factor(group) - # EDF = tapply(negEDF,INDEX=group,FUN=length) - tapply(negEDF,INDEX=group,FUN=sum) - # return(EDF) - #} + } + if(what == "EDF"){ + #group = colnames(object$tmb_inputs$tmb_data$Z_ik) + #group = sapply(group, FUN=\(char)strsplit(char,split=".",fixed=TRUE)[[1]][1]) + #group = factor(group) + group = factor(random_names) + EDF = tapply(negEDF, INDEX=group, FUN=length) - tapply(negEDF, INDEX=group, FUN=sum) + return(EDF) + } } diff --git a/R/fit.R b/R/fit.R index a5e21e8c..0f7293e9 100644 --- a/R/fit.R +++ b/R/fit.R @@ -154,6 +154,7 @@ #' @importFrom cli cli_abort cli_warn cli_inform cli_alert_success cli_alert_danger cli_alert_info #' @importFrom GpGp find_ordered_nn #' @importFrom GPvecchia order_maxmin_exact +#' @importFrom sparseinv Takahashi_Davis #' #' @seealso Details section of [make_dsem_ram()] for a summary of the math involved with constructing the DSEM, and \doi{10.1111/2041-210X.14289} for more background on math and inference #' @seealso \doi{10.48550/arXiv.2401.10193} for more details on how GAM, SEM, and DSEM components are combined from a statistical and software-user perspective @@ -604,7 +605,7 @@ function( formula, # Aepsilon_zz = cbind(Atriplet$i, Atriplet$j, t_i[Atriplet$i], c_i[Atriplet$i]) - which_Arows = which(apply( Aepsilon_zz, MARGIN=1, FUN=function(x) all(!is.na(x)) & any(x>0) )) + which_Arows = which( rowSums(is.na(Aepsilon_zz))==0 & rowSums(Aepsilon_zz>0, na.rm=TRUE)>0 ) which_Arows = which_Arows[ which(Atriplet$x[which_Arows] > 0) ] if( (nrow(spacetime_term_ram$output$ram)==0) & (nrow(delta_spacetime_term_ram$output$ram)==0) ){ which_Arows = numeric(0) @@ -614,7 +615,7 @@ function( formula, # Aomega_zz = cbind(Atriplet$i, Atriplet$j, c_i[Atriplet$i]) - which_Arows = which(apply( Aomega_zz, MARGIN=1, FUN=function(x) all(!is.na(x)) )) + which_Arows = which( rowSums(is.na(Aomega_zz))==0 ) which_Arows = which_Arows[ which(Atriplet$x[which_Arows] > 0) ] if( (nrow(space_term_ram$output$ram)==0) & (nrow(delta_space_term_ram$output$ram)==0) ){ which_Arows = numeric(0) @@ -799,7 +800,8 @@ function( formula, ifelse( control$gmrf_parameterization=="separable", 0, 1), ifelse( isFALSE(control$get_rsr), 0, 1), ifelse( isFALSE(control$extra_reporting), 0, 1), - 0 + 0, + ifelse( isTRUE(control$update_weights), 1, 0) ) # make dat @@ -1307,6 +1309,9 @@ function( formula, #' in the SPDE method when using \code{add_mesh_covariates}. The #' default \code{barrier_stiffness = 0.01} is the value from Bakka et al. #' 2019. +#' @param update_weights Whether to allow \code{weights_i} to be changed via +#' \code{obj$env$data} without re-taping (e.g., for stochastic gradient +#' descent). This slows down fitting for large datasets. #' #' @references #' Bakka, H., Vanhatalo, J., Illian, J., Simpson, D., Rue, H. (2019). Non-stationary Gaussian models with physical barriers. Spatial Statistics, 29, 268-288. \doi{10.1016/j.spasta.2019.01.002} @@ -1343,7 +1348,8 @@ function( opt_loops = 1, extra_reporting = FALSE, use_anisotropy = FALSE, sar_adjacency = "queen", - barrier_stiffness = 0.01 ){ + barrier_stiffness = 0.01, + update_weights = FALSE ){ gmrf_parameterization = match.arg(gmrf_parameterization) #optimizer = match.arg(optimizer) @@ -1383,7 +1389,8 @@ function( opt_loops = 1, extra_reporting = extra_reporting, use_anisotropy = use_anisotropy, sar_adjacency = sar_adjacency, - barrier_stiffness = barrier_stiffness + barrier_stiffness = barrier_stiffness, + update_weights = update_weights ), class = "tinyVASTcontrol" ) } diff --git a/R/predict.R b/R/predict.R index defa5c5b..eddda794 100644 --- a/R/predict.R +++ b/R/predict.R @@ -629,7 +629,7 @@ function( object, # AepsilonG_zz = cbind(predAtriplet$i, predAtriplet$j, t_g[predAtriplet$i], c_g[predAtriplet$i]) - which_Arows = which(apply( AepsilonG_zz, MARGIN=1, FUN=\(x) all(!is.na(x)) & any(x>0) )) + which_Arows = which( rowSums(is.na(AepsilonG_zz))==0 & rowSums(AepsilonG_zz>0, na.rm=TRUE)>0 ) which_Arows = which_Arows[ which(predAtriplet$x[which_Arows] > 0) ] if( (nrow(object$internal$spacetime_term_ram$output$ram)==0) & (nrow(object$internal$delta_spacetime_term_ram$output$ram)==0) ){ which_Arows = numeric(0) @@ -639,7 +639,7 @@ function( object, # AomegaG_zz = cbind(predAtriplet$i, predAtriplet$j, c_g[predAtriplet$i]) - which_Arows = which(apply( AomegaG_zz, MARGIN=1, FUN=\(x) all(!is.na(x)) )) + which_Arows = which( rowSums(is.na(AomegaG_zz))==0 ) which_Arows = which_Arows[ which(predAtriplet$x[which_Arows] > 0) ] if( (nrow(object$internal$space_term_ram$output$ram)==0) & (nrow(object$internal$delta_space_term_ram$output$ram)==0) ){ which_Arows = numeric(0) diff --git a/R/project.R b/R/project.R index 8edbc1e7..0b801949 100644 --- a/R/project.R +++ b/R/project.R @@ -87,9 +87,11 @@ function( Q, #' condition of the forecast #' @param parm_var logical indicating whether to re-sample fixed effects from their #' predictive distribution, thus changing the GMRF for future process errors +#' @param nsim number of samples #' #' @return -#' A vector of values corresponding to rows in \code{newdata} +#' A vector of values corresponding to rows in \code{newdata}, or a matrix +#' with a column for each sample when \code{nsim > 1} #' #' @examples #' # Convert to long-form @@ -139,18 +141,15 @@ function( Q, #' extra_times = length(x) + 1:100 #' n_sims = 10 #' newdata = data.frame( "time" = c(seq_along(x),extra_times), "var" = "y" ) -#' Y = NULL -#' for(i in seq_len(n_sims) ){ -#' tmp = project( -#' mytiny, -#' newdata = newdata, -#' extra_times = extra_times, -#' future_var = TRUE, -#' past_var = TRUE, -#' parm_var = TRUE -#' ) -#' Y = cbind(Y, tmp) -#' } +#' Y = project( +#' mytiny, +#' newdata = newdata, +#' extra_times = extra_times, +#' future_var = TRUE, +#' past_var = TRUE, +#' parm_var = TRUE, +#' nsim = n_sims +#' ) #' matplot( x = row(Y), #' y = Y, #' type = "l", lty = "solid", col = "black" ) @@ -164,31 +163,31 @@ function( object, what = "mu_g", future_var = TRUE, past_var = FALSE, - parm_var = FALSE ){ + parm_var = FALSE, + nsim = 1 ){ ############## # Step 1: Generate uncertainty from parm_var and past_var, - # and load into parlist + # and load into columns of parmat ############## if( isFALSE(parm_var) & isFALSE(past_var) ){ - parlist = object$internal$parlist + parmat = object$obj$env$last.par.best %o% rep(1, nsim) } if( isTRUE(parm_var) & isFALSE(past_var) ){ stop("option not available") } if( isFALSE(parm_var) & isTRUE(past_var) ){ - parvec = object$obj$env$last.par.best - MC = object$obj$env$MC( keep=TRUE, n=1, antithetic=FALSE ) - parvec[object$obj$env$lrandom()] = attr(MC, "samples") - parlist = object$obj$env$parList( par = parvec ) + parmat = object$obj$env$last.par.best %o% rep(1, nsim) + MC = object$obj$env$MC( keep=TRUE, n=nsim, antithetic=FALSE ) + parmat[object$obj$env$lrandom(),] = attr(MC, "samples") } if( isTRUE(parm_var) & isTRUE(past_var) ){ if(is.null(object$sdrep$jointPrecision)) stop("Rerun with `getJointPrecision=TRUE`") - parvec = rmvnorm_prec( mu = object$obj$env$last.par.best, - prec = object$sdrep$jointPrecision ) - parlist = object$obj$env$parList( par = parvec ) + parmat = rmvnorm_prec( mu = object$obj$env$last.par.best, + prec = object$sdrep$jointPrecision, + n = nsim ) } ############## @@ -202,15 +201,15 @@ function( object, ############## new_control = object$internal$control - new_control$run_model = TRUE - new_control$nlminb_loops = 0 + new_control$run_model = FALSE + new_control$opt_loops = 0 new_control$newton_loops = 0 new_control$getsd = FALSE new_control$calculate_deviance_explained = FALSE new_control$suppress_user_warnings = TRUE new_control$extra_reporting = TRUE - newobj = tinyVAST( + new_inputs = tinyVAST( formula = object$formula, data = object$data, time_term = object$internal$time_term, @@ -235,12 +234,15 @@ function( object, development = object$internal$development ) - # - newpar = newobj$obj$env$last.par - oldpar = object$obj$env$last.par.best - tmb_fixed = setdiff(unique(names(newpar)), newobj$tmb_inputs$tmb_random) - newpar[ (names(newpar) %in% tmb_fixed) ] = oldpar[ (names(oldpar) %in% tmb_fixed) ] - newrep = newobj$obj$report( newpar ) + # REPORT uses expanded parameter lists, so omit both maps and random effects. + # parList() on the fitted object restores fixed and shared mapped values. + newobj = object + newobj$tmb_inputs = new_inputs + newobj$internal$times = all_times + newobj$obj = MakeADFun( data = new_inputs$tmb_data, + parameters = new_inputs$tmb_par, + silent = TRUE, + DLL = "tinyVAST" ) ############## # Step 4: Merge ParList and ParList1 @@ -270,27 +272,24 @@ function( object, IminusRho_hh = Matrix::Diagonal(n=nrow(newrep$Rho_hh)) - newrep$Rho2_hh Q_kk = Matrix::t(IminusRho_hh) %*% Matrix::solve(Matrix::t(newrep$Gamma2_hh) %*% newrep$Gamma2_hh) %*% IminusRho_hh } - Q_hh = Matrix::kronecker( Q_kk, Q_ss ) # - grid = expand.grid( s = seq_len(dim(neweps_stc)[1]), - t = all_times, + grid = expand.grid( t = all_times, c = object$internal$variables ) - grid$num = seq_len(prod(dim(neweps_stc))) + grid$num = seq_len(nrow(grid)) observed_idx = subset( grid, t %in% object$internal$times )$num - - # - tmp = conditional_gmrf( - Q = Q_hh, - observed_idx = observed_idx, - x_obs = as.vector( eps_stc ), - n_sims = 1, - what = ifelse(future_var, "simulate", "predict") - ) + unobserved_idx = setdiff( grid$num, observed_idx ) + + # Precision is kronecker( Q_kk, Q_ss ) and past times include all sites, so the + # conditional mean only needs Q_kk and the conditional precision is kronecker( Q_uu, Q_ss ) + Q_uu = Q_kk[unobserved_idx, unobserved_idx, drop = FALSE] + Q_uo = Q_kk[unobserved_idx, observed_idx, drop = FALSE] + eps_so = matrix( eps_stc, nrow = dim(eps_stc)[1] ) + simeps_h = -1 * as.matrix( eps_so %*% Matrix::t(Matrix::solve(Q_uu, Q_uo)) ) if( isTRUE(future_var) ){ - simeps_h = tmp[,1] - }else{ - simeps_h = tmp$mean + # Columns have precision Q_ss, and are then correlated to have precision Q_uu among columns + z_su = rmvnorm_prec( prec = Q_ss, n = length(unobserved_idx) ) + simeps_h = simeps_h + t( backsolve(chol(as.matrix(Q_uu)), t(z_su)) ) } # Compile @@ -363,105 +362,74 @@ function( object, return(newdelta_tc) } - # - new_parlist = newobj$tmb_inputs$tmb_par - #new_parlist = newobj$tmb_par - Q_ss = object$rep$Q_ss - - # Replace epsilon - new_parlist$epsilon_stc = augment_epsilon( - #beta_z = parlist$beta_z, - eps_stc = parlist$epsilon_stc, - neweps_stc = new_parlist$epsilon_stc, - #model = object$internal$spacetime_term_ram$output$model, - linpred = 1 - ) - new_parlist$epsilon2_stc = augment_epsilon( - #beta_z = parlist$beta2_z, - eps_stc = parlist$epsilon2_stc, - neweps_stc = new_parlist$epsilon2_stc, - #model = object$internal$delta_spacetime_term_ram$output$model, - linpred = 2 - ) - - # Replace delta - new_parlist$delta_tc = augment_delta( - #nu_z = parlist$nu_z, - delta_tc = parlist$delta_tc, - newdelta_tc = new_parlist$delta_tc, - #model = object$internal$time_term_ram$output$model, - linpred = 1 - ) - new_parlist$delta2_tc = augment_delta( - #nu_z = parlist$nu2_z, - delta_tc = parlist$delta2_tc, - newdelta_tc = new_parlist$delta2_tc, - #model = object$internal$delta_time_term_ram$output$model, - linpred = 2 - ) + ############## + # Step 5: Re-build model + ############## - # Replace other variables that are not changed - same_vars = setdiff( names(new_parlist), c("epsilon_stc","epsilon2_stc","delta_tc","delta2_tc") ) - new_parlist[same_vars] = parlist[same_vars] + # Build prediction object once, and then REPORT for each sample + predobj = MakeADFun( data = add_predictions( object = newobj, newdata = newdata ), + parameters = newobj$tmb_inputs$tmb_par, + type = "Fun", + silent = TRUE, + DLL = "tinyVAST" ) ############## - # Step 5: Re-build model + # Step 6: simulate samples ############## - #new_control$run_model = TRUE - new_control$tmb_par = new_parlist - new_control$extra_reporting = FALSE - #new_control$nlminb_loops = 0 - #new_control$newton_loops = 0 - #new_control$getsd = FALSE - #new_control$calculate_deviance_explained = FALSE + # Use TMB's parameter order when flattening lists for REPORT. + par_template = newobj$obj$env$parList() + same_vars = setdiff( names(par_template), c("epsilon_stc","epsilon2_stc","delta_tc","delta2_tc") ) + pred = matrix( NA, nrow = nrow(newdata), ncol = nsim ) + for( i in seq_len(nsim) ){ + parvec = parmat[,i] + parlist = object$obj$env$parList( par = parvec ) - newobj = tinyVAST( - formula = object$formula, - data = object$data, - time_term = object$internal$time_term, - space_term = object$internal$space_term, - spacetime_term = object$internal$spacetime_term, - family = object$internal$family, - space_columns = object$internal$space_columns, - spatial_domain = object$spatial_domain, - time_column = object$internal$time_column, - times = all_times, - variable_column = object$internal$variable_column, - variables = object$internal$variables, - distribution_column = object$internal$distribution_column, - delta_options = list( formula = object$internal$delta_formula, - space_term = object$internal$delta_space_term, - time_term = object$internal$delta_time_term, - spacetime_term = object$internal$delta_spacetime_term, - spatial_varying = object$internal$delta_spatial_varying ), - spatial_varying = object$internal$spatial_varying, - weights = object$internal$weights, - control = new_control, - development = object$internal$development - ) + # Copy parameters with unchanged dimensions before building future precisions. + # Process arrays retain their padded dimensions until augmented below. + new_parlist = par_template + new_parlist[same_vars] = parlist[same_vars] + newrep = newobj$obj$report( unlist(new_parlist, use.names = FALSE) ) - # CHECK SIZES ... if throwing error - if( FALSE ){ - # Check length - range( - # NEW - sapply( newobj$tmb_inputs$tmb_par, length ) - - # OLD - sapply( new_parlist, length ) - ) # - isTRUE(all.equal(new_parlist, newobj$tmb_inputs$tmb_par, tolerance=Inf)) - } + # random effects are scaled by tau, so their precision is Q_ss * tau^2 + Q_ss = newrep$Q_ss * exp(2 * newrep$log_tau) + + # Replace epsilon + new_parlist$epsilon_stc = augment_epsilon( + #beta_z = parlist$beta_z, + eps_stc = parlist$epsilon_stc, + neweps_stc = new_parlist$epsilon_stc, + #model = object$internal$spacetime_term_ram$output$model, + linpred = 1 + ) + new_parlist$epsilon2_stc = augment_epsilon( + #beta_z = parlist$beta2_z, + eps_stc = parlist$epsilon2_stc, + neweps_stc = new_parlist$epsilon2_stc, + #model = object$internal$delta_spacetime_term_ram$output$model, + linpred = 2 + ) - ############## - # Step 6: simulate samples - ############## + # Replace delta + new_parlist$delta_tc = augment_delta( + #nu_z = parlist$nu_z, + delta_tc = parlist$delta_tc, + newdelta_tc = new_parlist$delta_tc, + #model = object$internal$time_term_ram$output$model, + linpred = 1 + ) + new_parlist$delta2_tc = augment_delta( + #nu_z = parlist$nu2_z, + delta_tc = parlist$delta2_tc, + newdelta_tc = new_parlist$delta2_tc, + #model = object$internal$delta_time_term_ram$output$model, + linpred = 2 + ) - pred = predict( - object = newobj, - newdata = newdata, - what = what - ) + # Include every augmented effect, including effects fitted as fixed parameters. + pred[,i] = predobj$report( unlist(new_parlist, use.names = FALSE) )[[what]] + } + if( nsim == 1 ) pred = pred[,1] return(pred) } diff --git a/R/utility.R b/R/utility.R index 8076299e..9eaabf1a 100644 --- a/R/utility.R +++ b/R/utility.R @@ -403,7 +403,7 @@ function( x, # control_initial = control - control_initial$nlminb_loops = 0 + control_initial$opt_loops = 0 control_initial$newton_loops = 0 control_initial$run_model = FALSE diff --git a/man/cAIC.Rd b/man/cAIC.Rd index 69271de3..1d0a9f1e 100644 --- a/man/cAIC.Rd +++ b/man/cAIC.Rd @@ -4,10 +4,18 @@ \alias{cAIC} \title{Calculate conditional AIC} \usage{ -cAIC(object) +cAIC(object, what = c("cAIC", "EDF"), method = c("fast", "stable")) } \arguments{ \item{object}{Output from \code{\link[=tinyVAST]{tinyVAST()}}.} + +\item{what}{whether to get cAIC or the EDF (the latter grouped by type of random effect).} + +\item{method}{whether to compute effective degrees of freedom using +the stable method \code{diag(solve(Hess, Hess_new))} +or a faster and experimental method +\code{-rowSums(sparseinv::Takahashi_Davis(-Hess) * t(Hess_new))}, +where the latter avoids constructing a dense matrix prior to extracting its diagonal} } \value{ cAIC value @@ -28,12 +36,13 @@ that have new random effects, e.g., extrapolation, or inference about generative parameters. Both cAIC and EDF are calculated using Eq. 6 of Zheng, Cadigan, and Thorson -(2024). +(2026). For models that include profiled fixed effects, these profiles are turned off. } \examples{ +\donttest{ data( red_snapper ) red_snapper = droplevels(subset(red_snapper, Data_type=="Biomass_KG")) @@ -59,11 +68,12 @@ fit = tinyVAST( data = red_snapper, cAIC(fit) # conditional AIC AIC(fit) # marginal AIC +} } \references{ -Zheng, N., Cadigan, N., & Thorson, J. T. (2024). -A note on numerical evaluation of conditional Akaike information for -nonlinear mixed-effects models (arXiv:2411.14185). arXiv. -\doi{10.48550/arXiv.2411.14185} +Zheng, N., Cadigan, N., & Thorson, J. T. (2026). +A unifying theory for the evaluation of conditional Akaike information for +mixed-effects models. Journal of Statistical Computation and Simulation. +\doi{10.1080/00949655.2026.2736799} } diff --git a/man/project.Rd b/man/project.Rd index f6f6ac22..7ec7f25e 100644 --- a/man/project.Rd +++ b/man/project.Rd @@ -11,7 +11,8 @@ project( what = "mu_g", future_var = TRUE, past_var = FALSE, - parm_var = FALSE + parm_var = FALSE, + nsim = 1 ) } \arguments{ @@ -46,9 +47,12 @@ condition of the forecast} \item{parm_var}{logical indicating whether to re-sample fixed effects from their predictive distribution, thus changing the GMRF for future process errors} + +\item{nsim}{number of samples} } \value{ -A vector of values corresponding to rows in \code{newdata} +A vector of values corresponding to rows in \code{newdata}, or a matrix +with a column for each sample when \code{nsim > 1} } \description{ Projects a fitted model forward in time. @@ -101,18 +105,15 @@ plot( x = seq_along(Y), extra_times = length(x) + 1:100 n_sims = 10 newdata = data.frame( "time" = c(seq_along(x),extra_times), "var" = "y" ) -Y = NULL -for(i in seq_len(n_sims) ){ - tmp = project( - mytiny, - newdata = newdata, - extra_times = extra_times, - future_var = TRUE, - past_var = TRUE, - parm_var = TRUE - ) - Y = cbind(Y, tmp) -} +Y = project( + mytiny, + newdata = newdata, + extra_times = extra_times, + future_var = TRUE, + past_var = TRUE, + parm_var = TRUE, + nsim = n_sims +) matplot( x = row(Y), y = Y, type = "l", lty = "solid", col = "black" ) diff --git a/man/tinyVASTcontrol.Rd b/man/tinyVASTcontrol.Rd index d45a31b5..2e284e91 100644 --- a/man/tinyVASTcontrol.Rd +++ b/man/tinyVASTcontrol.Rd @@ -27,7 +27,8 @@ tinyVASTcontrol( extra_reporting = FALSE, use_anisotropy = FALSE, sar_adjacency = "queen", - barrier_stiffness = 0.01 + barrier_stiffness = 0.01, + update_weights = FALSE ) } \arguments{ @@ -116,6 +117,10 @@ rate and resulting decorrelation distance) for barriers relative to normal areas in the SPDE method when using \code{add_mesh_covariates}. The default \code{barrier_stiffness = 0.01} is the value from Bakka et al. 2019.} + +\item{update_weights}{Whether to allow \code{weights_i} to be changed via +\code{obj$env$data} without re-taping (e.g., for stochastic gradient +descent). This slows down fitting for large datasets.} } \value{ An object (list) of class \code{tinyVASTcontrol}, containing either default or diff --git a/scratch/Upload_to_CRAN.R b/scratch/Upload_to_CRAN.R index 9f29676c..8af52b48 100644 --- a/scratch/Upload_to_CRAN.R +++ b/scratch/Upload_to_CRAN.R @@ -3,7 +3,6 @@ library(devtools) #pandoc::pandoc_install() -#setwd( R'(C:\Users\James.Thorson\Desktop\Git\tinyVAST)' ) setwd( R'(C:\Users\jtuth\Documents\GitHub\tinyVAST)') # Compile diff --git a/scratch/build_pkgdown_website.R b/scratch/build_pkgdown_website.R index d9b90cab..d4295e6a 100644 --- a/scratch/build_pkgdown_website.R +++ b/scratch/build_pkgdown_website.R @@ -34,7 +34,7 @@ if( FALSE ){ # output = "web_only/VAST.Rmd" ) } -setwd(R'(C:\Users\James.Thorson\Desktop\Git\tinyVAST)') +setwd(R'(C:\Users\james\OneDrive\Desktop\Git\tinyVAST)') # Only needed once if( FALSE ){ diff --git a/src/tinyVAST.cpp b/src/tinyVAST.cpp index 8b58db30..e8e987b9 100644 --- a/src/tinyVAST.cpp +++ b/src/tinyVAST.cpp @@ -40,7 +40,6 @@ Type objective_function::operator() (){ DATA_IVECTOR( c_i ); DATA_VECTOR( offset_i ); DATA_VECTOR( weights_i ); - DATA_UPDATE( weights_i ); // Warning: cannot use branching (weights_i > 0) when using DATA_UPDATE DATA_VECTOR( size_i ); // Ignored unless family = binomial DATA_SPARSE_MATRIX( S_kk ); // Sparse penalization matrix DATA_IVECTOR( Sdims ); // Dimensions of blockwise components of S_kk @@ -56,6 +55,11 @@ Type objective_function::operator() (){ // model_options(2)==0: no RSS; model_options(2)==1: yes RSR // model_options(3)==0: no extra reporting; model_options(3)==1: yes extra reporting // model_options(4)==0: no SE for _g; model_options(4)==1: SE for p_g; model_options(4)==2: SE for mu_g + // model_options(5)==0: fixed weights_i; model_options(5)==1: weights_i can be updated without re-taping + bool update_weights = (model_options.size() > 5) && (model_options(5) == 1); + if( update_weights ){ + DATA_UPDATE( weights_i ); // Warning: cannot use branching (weights_i > 0) when using DATA_UPDATE + } DATA_IMATRIX( Aepsilon_zz ); // NAs get converted to -2147483648 DATA_VECTOR( Aepsilon_z ); DATA_IMATRIX( Aomega_zz ); // NAs get converted to -2147483648 @@ -272,18 +276,18 @@ Type objective_function::operator() (){ // space_term omega_sc = omega_distribution( omega_sc, model_options, Rho_cc, - Gamma_cc, Gammainv_cc, Q_ss, + Gamma_cc, Gammainv_cc, Q_ss, exp(log_tau), exp(log_kappa), nngp_data, nll ); // nngp_data, omega2_sc = omega_distribution( omega2_sc, model_options, Rho2_cc, - Gamma2_cc, Gammainv2_cc, Q_ss, + Gamma2_cc, Gammainv2_cc, Q_ss, exp(log_tau), exp(log_kappa), nngp_data, nll ); // nngp_data, // spacetime_term epsilon_stc = epsilon_distribution( epsilon_stc, model_options, Rho_hh, - Gamma_hh, Gammainv_hh, Q_ss, + Gamma_hh, Gammainv_hh, Q_ss, exp(log_tau), exp(log_kappa), nngp_data, nll ); epsilon2_stc = epsilon_distribution( epsilon2_stc, model_options, Rho2_hh, - Gamma2_hh, Gammainv2_hh, Q_ss, + Gamma2_hh, Gammainv2_hh, Q_ss, exp(log_tau), exp(log_kappa), nngp_data, nll ); // time_term @@ -299,9 +303,9 @@ Type objective_function::operator() (){ // Distribution for SVC components nll += xi_distribution( model_options, xi_sl, log_sigmaxi_l, - Q_ss, exp(log_kappa), nngp_data ); + Q_ss, exp(log_tau), exp(log_kappa), nngp_data ); nll += xi_distribution( model_options, xi2_sl, log_sigmaxi2_l, - Q_ss, exp(log_kappa), nngp_data ); + Q_ss, exp(log_tau), exp(log_kappa), nngp_data ); // Linear predictor .. keep partial effects for RSR adjustment below vector p_i( n_i ); @@ -312,9 +316,9 @@ Type objective_function::operator() (){ p_i = offset_i; if(alpha_j.size() > 0){ palpha1_i = X_ij*alpha_j; } if(gamma_k.size() > 0){ pgamma1_i = Z_ik*gamma_k; } - vector pepsilon1_i = multiply_epsilon( Aepsilon_zz, Aepsilon_z, epsilon_stc, p_i.size() ) / exp(log_tau); - vector pomega1_i = multiply_omega( Aomega_zz, Aomega_z, omega_sc, p_i.size() ) / exp(log_tau); - vector pxi1_i = multiply_xi( A_is, xi_sl, W_il ) / exp(log_tau); + vector pepsilon1_i = multiply_epsilon( Aepsilon_zz, Aepsilon_z, epsilon_stc, p_i.size() ); + vector pomega1_i = multiply_omega( Aomega_zz, Aomega_z, omega_sc, p_i.size() ); + vector pxi1_i = multiply_xi( A_is, xi_sl, W_il ); vector pdelta1_i = multiply_delta( delta_tc, t_i, c_i, n_i ); p_i += palpha1_i; p_i += pgamma1_i; @@ -332,9 +336,9 @@ Type objective_function::operator() (){ p2_i.setZero(); if(alpha2_j.size() > 0){ palpha2_i = X2_ij*alpha2_j; } if(gamma2_k.size() > 0){ pgamma2_i = Z2_ik*gamma2_k; } - vector pepsilon2_i = multiply_epsilon( Aepsilon_zz, Aepsilon_z, epsilon2_stc, p_i.size() ) / exp(log_tau); - vector pomega2_i = multiply_omega( Aomega_zz, Aomega_z, omega2_sc, p_i.size() ) / exp(log_tau); - vector pxi2_i = multiply_xi( A_is, xi2_sl, W2_il ) / exp(log_tau); + vector pepsilon2_i = multiply_epsilon( Aepsilon_zz, Aepsilon_z, epsilon2_stc, p_i.size() ); + vector pomega2_i = multiply_omega( Aomega_zz, Aomega_z, omega2_sc, p_i.size() ); + vector pxi2_i = multiply_xi( A_is, xi2_sl, W2_il ); vector pdelta2_i = multiply_delta( delta2_tc, t_i, c_i, n_i ); p2_i += palpha2_i; p2_i += pgamma2_i; @@ -358,14 +362,18 @@ Type objective_function::operator() (){ if( components_e(e_i(i))==1 ){ mu_i(i) = one_predictor_likelihood( y_i(i), p_i(i), size_i(i), link_ez(e_i(i),0), family_ez(e_i(i),0), log_sigma_segment, nll_tmp, devresid, this ); negloglik_i(i) = nll_tmp; - nll += weights_i(i) * nll_tmp; + // Multiplying by weights_i=1 is not free, because it adds per-observation terms to the random-effect Hessian + if( update_weights || (weights_i(i) != Type(1.0)) ) nll_tmp *= weights_i(i); + nll += nll_tmp; deviance += pow( devresid, 2.0 ); devresid_i(i) = devresid; } if( components_e(e_i(i))==2 ){ mu_i(i) = two_predictor_likelihood( y_i(i), p_i(i), p2_i(i), size_i(i), link_ez.row(e_i(i)), family_ez.row(e_i(i)), log_sigma_segment, poislink_e(e_i(i)), nll_tmp, dev, this ); negloglik_i(i) = nll_tmp; - nll += weights_i(i) * nll_tmp; + // Multiplying by weights_i=1 is not free, because it adds per-observation terms to the random-effect Hessian + if( update_weights || (weights_i(i) != Type(1.0)) ) nll_tmp *= weights_i(i); + nll += nll_tmp; deviance += dev; devresid_i(i) = NAN; } @@ -392,17 +400,17 @@ Type objective_function::operator() (){ if( n_g > 0 ){ vector palpha1_g = X_gj*alpha_j; vector pgamma1_g = Z_gk*gamma_k; - vector pepsilon1_g = multiply_epsilon( AepsilonG_zz, AepsilonG_z, epsilon_stc, palpha1_g.size() ) / exp(log_tau); - vector pomega1_g = multiply_omega( AomegaG_zz, AomegaG_z, omega_sc, palpha1_g.size() ) / exp(log_tau); - vector pxi1_g = multiply_xi( A_gs, xi_sl, W_gl ) / exp(log_tau); + vector pepsilon1_g = multiply_epsilon( AepsilonG_zz, AepsilonG_z, epsilon_stc, palpha1_g.size() ); + vector pomega1_g = multiply_omega( AomegaG_zz, AomegaG_z, omega_sc, palpha1_g.size() ); + vector pxi1_g = multiply_xi( A_gs, xi_sl, W_gl ); vector pdelta1_g = multiply_delta( delta_tc, t_g, c_g, n_g ); vector p1_g = palpha1_g + pgamma1_g+ pepsilon1_g + pomega1_g + pdelta1_g + pxi1_g + offset_g ; // Second linear predictor vector palpha2_g = X2_gj*alpha2_j; vector pgamma2_g = Z2_gk*gamma2_k; - vector pepsilon2_g = multiply_epsilon( AepsilonG_zz, AepsilonG_z, epsilon2_stc, palpha2_g.size() ) / exp(log_tau); - vector pomega2_g = multiply_omega( AomegaG_zz, AomegaG_z, omega2_sc, palpha2_g.size() ) / exp(log_tau); - vector pxi2_g = multiply_xi( A_gs, xi2_sl, W2_gl ) / exp(log_tau); + vector pepsilon2_g = multiply_epsilon( AepsilonG_zz, AepsilonG_z, epsilon2_stc, palpha2_g.size() ); + vector pomega2_g = multiply_omega( AomegaG_zz, AomegaG_z, omega2_sc, palpha2_g.size() ); + vector pxi2_g = multiply_xi( A_gs, xi2_sl, W2_gl ); vector pdelta2_g = multiply_delta( delta2_tc, t_g, c_g, n_g ); vector p2_g = palpha2_g + pgamma2_g + pepsilon2_g + pomega2_g + pdelta2_g + pxi2_g; // Combined diff --git a/src/utils.h b/src/utils.h index 48fb16f0..dbc2638a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -446,6 +446,7 @@ tmbutils::array omega_distribution( const Eigen::SparseMatrix &Gamma_cc, const Eigen::SparseMatrix &Gammainv_cc, const Eigen::SparseMatrix &Q_ss, + Type tau, Type range, const nngp_data_t &nngp_data, Type &nll ){ @@ -465,12 +466,12 @@ tmbutils::array omega_distribution( // Separable precision ... Option-2 // Only compute Vinv_kk if Gamma_kk is full rank Eigen::SparseMatrix V_cc = Gamma_cc.transpose() * Gamma_cc; - matrix Vinv_cc = invertSparseMatrix( V_cc ); + matrix Vinv_cc = tmbutils::invertSparseMatrix( V_cc ); Eigen::SparseMatrix Vinv2_cc = asSparseMatrix( Vinv_cc ); Eigen::SparseMatrix Q_cc = IminusRho_cc.transpose() * Vinv2_cc * IminusRho_cc; // GMRF for SEM: separable variable-space - nll += SEPARABLE( GMRF(Q_cc), GMRF(Q_ss) )( omega_sc ); + nll += SCALE( SEPARABLE( GMRF(Q_cc), GMRF(Q_ss) ), Type(1.0)/tau )( omega_sc ); // Including this line with Makevars below seems to cause a crash: // PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) // PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) @@ -483,7 +484,7 @@ tmbutils::array omega_distribution( nll += NNGP( Type(1.0), range, omega_s, nngp_data ); } }else{ - nll += SEPARABLE( GMRF(I_cc), GMRF(Q_ss) )( omega_sc ); + nll += SCALE( SEPARABLE( GMRF(I_cc), GMRF(Q_ss) ), Type(1.0)/tau )( omega_sc ); } // Sparse inverse-product @@ -509,6 +510,7 @@ Type xi_distribution( tmbutils::array xi_sl, vector log_sigmaxi_l, const Eigen::SparseMatrix &Q_ss, + Type tau, Type range, const nngp_data_t &nngp_data ){ @@ -524,7 +526,7 @@ Type xi_distribution( } }else{ for( int l=0; l epsilon_distribution( const Eigen::SparseMatrix &Gamma_hh, const Eigen::SparseMatrix &Gammainv_hh, const Eigen::SparseMatrix &Q_ss, + Type tau, Type range, const nngp_data_t &nngp_data, Type &nll ){ @@ -575,12 +578,12 @@ tmbutils::array epsilon_distribution( // Separable precision ... Option-2 // Only compute Vinv_kk if Gamma_kk is full rank Eigen::SparseMatrix V_hh = Gamma_hh.transpose() * Gamma_hh; - matrix Vinv_hh = invertSparseMatrix( V_hh ); + matrix Vinv_hh = tmbutils::invertSparseMatrix( V_hh ); Eigen::SparseMatrix Vinv2_hh = asSparseMatrix( Vinv_hh ); Eigen::SparseMatrix Q_hh = IminusRho_hh.transpose() * Vinv2_hh * IminusRho_hh; // GMRF for DSEM: non-separable time-variable, with separable space - nll += SEPARABLE( GMRF(Q_ss), GMRF(Q_hh) )( epsilon_hs ); + nll += SCALE( SEPARABLE( GMRF(Q_ss), GMRF(Q_hh) ), Type(1.0)/tau )( epsilon_hs ); }else{ // Rank-deficient (projection) method if( model_options(0) == 7 ){ @@ -590,7 +593,7 @@ tmbutils::array epsilon_distribution( nll += NNGP( Type(1.0), range, epsilon_s, nngp_data ); } }else{ - nll += SEPARABLE( GMRF(Q_ss), GMRF(I_hh) )( epsilon_hs ); + nll += SCALE( SEPARABLE( GMRF(Q_ss), GMRF(I_hh) ), Type(1.0)/tau )( epsilon_hs ); } // Sparse inverse-product @@ -646,7 +649,7 @@ tmbutils::array delta_distribution( if( model_options(1) == 0 ){ Eigen::SparseMatrix V_hh = Gamma_hh.transpose() * Gamma_hh; - matrix Vinv_hh = invertSparseMatrix( V_hh ); + matrix Vinv_hh = tmbutils::invertSparseMatrix( V_hh ); Eigen::SparseMatrix Vinv2_hh = asSparseMatrix( Vinv_hh ); Eigen::SparseMatrix Q_hh = IminusRho_hh.transpose() * Vinv2_hh * IminusRho_hh; @@ -933,28 +936,28 @@ Type one_predictor_likelihood( // Distribution switch( family ){ case gaussian_family: - nll = -1 * dnorm( y, mu, exp(log_sigma_segment(0)), true ); + nll = -dnorm( y, mu, exp(log_sigma_segment(0)), true ); devresid = y - mu; if(isDouble::value && of->do_simulate){ y = rnorm( mu, exp(log_sigma_segment(0)) ); } break; case tweedie_family: - nll = -1 * dtweedie( y, mu, exp(log_sigma_segment(0)), 1.0 + invlogit(log_sigma_segment(1)), true ); + nll = -dtweedie( y, mu, exp(log_sigma_segment(0)), 1.0 + invlogit(log_sigma_segment(1)), true ); devresid = devresid_tweedie( y, mu, 1.0 + invlogit(log_sigma_segment(1)) ); if(isDouble::value && of->do_simulate){ y = rtweedie( mu, exp(log_sigma_segment(0)), 1.0 + invlogit(log_sigma_segment(1)) ); } break; case lognormal_family: - nll = -1 * dlnorm( y, logmu - 0.5*exp(2.0*log_sigma_segment(0)), exp(log_sigma_segment(0)), true ); + nll = -dlnorm( y, logmu - 0.5*exp(2.0*log_sigma_segment(0)), exp(log_sigma_segment(0)), true ); devresid = log(y) - ( logmu - 0.5*exp(2.0*log_sigma_segment(0)) ); if(isDouble::value && of->do_simulate){ y = exp(rnorm( logmu - 0.5*exp(2.0*log_sigma_segment(0)), exp(log_sigma_segment(0)) )); } break; case poisson_family: - nll = -1 * dpois( y, mu, true ); + nll = -dpois( y, mu, true ); devresid = sign(y - mu) * pow(2*(y*log((Type(1e-10) + y)/mu) - (y-mu)), 0.5); if(isDouble::value && of->do_simulate){ y = rpois( mu ); @@ -966,7 +969,7 @@ Type one_predictor_likelihood( //}else{ // nll = -1 * logmu; //} - nll = -1 * dbinom_custom( y * size, logmu, log_one_minus_mu, size, true ); + nll = -dbinom_custom( y * size, logmu, log_one_minus_mu, size, true ); if(isDouble::value && of->do_simulate){ y = rbinom( size, mu ); } @@ -975,7 +978,7 @@ Type one_predictor_likelihood( devresid = devresid_binom( y, size, mu ); break; case gamma_family: // shape = 1/CV^2; scale = mean*CV^2 - nll = -1 * dgamma( y, exp(-2.0*log_sigma_segment(0)), mu*exp(2.0*log_sigma_segment(0)), true ); + nll = -dgamma( y, exp(-2.0*log_sigma_segment(0)), mu*exp(2.0*log_sigma_segment(0)), true ); devresid = sign(y - mu) * pow(2 * ( (y-mu)/mu - log(y/mu) ), 0.5); if(isDouble::value && of->do_simulate){ y = rgamma( exp(-2.0*log_sigma_segment(0)), mu*exp(2.0*log_sigma_segment(0)) ); @@ -983,7 +986,7 @@ Type one_predictor_likelihood( break; case nbinom1_family: // dnbinom_robust( x, log(mu_i), log(var - mu) ) // var - mu = exp( log(mu) + log(theta) ) = theta * mu --> var = (theta+1) * mu - nll = -1 * dnbinom_robust( y, logmu, logmu + log_sigma_segment(0), true); + nll = -dnbinom_robust( y, logmu, logmu + log_sigma_segment(0), true); devresid = devresid_nbinom2( y, logmu, logmu - log_sigma_segment(0) ); // theta = mu / phi if(isDouble::value && of->do_simulate){ // rnbinom2( mu, var ) @@ -992,7 +995,7 @@ Type one_predictor_likelihood( break; case nbinom2_family: // dnbinom_robust( x, log(mu_i), log(var - mu) ) // var - mu = exp( 2 * log(mu) - log(theta) ) = mu^2 / theta --> var = mu + mu^2 / theta - nll = -1 * dnbinom_robust( y, logmu, Type(2.0) * logmu - log_sigma_segment(0), true); + nll = -dnbinom_robust( y, logmu, Type(2.0) * logmu - log_sigma_segment(0), true); devresid = devresid_nbinom2( y, logmu, log_sigma_segment(0) ); if(isDouble::value && of->do_simulate){ // rnbinom2( mu, var ) @@ -1001,7 +1004,7 @@ Type one_predictor_likelihood( break; case student_family: // dnbinom_robust( x, log(mu_i), log(var - mu) ) // var - mu = exp( 2 * log(mu) - log(theta) ) = mu^2 / theta --> var = mu + mu^2 / theta - nll = -1 * dstudent( y, mu, exp(log_sigma_segment(0)), 1.0 + exp(log_sigma_segment(1)), true); + nll = -dstudent( y, mu, exp(log_sigma_segment(0)), 1.0 + exp(log_sigma_segment(1)), true); devresid = devresid_student( y, mu, exp(log_sigma_segment(0)), 1.0 + exp(log_sigma_segment(1)) ); if(isDouble::value && of->do_simulate){ y = mu + exp(log_sigma_segment(0)) * rt(1.0 + exp(log_sigma_segment(1))); @@ -1063,11 +1066,11 @@ Type two_predictor_likelihood( y = rbinom( Type(1), mu1 ); } if( y == 0 ){ - nll = -1 * log_one_minus_mu1; + nll = -log_one_minus_mu1; dev = -2 * log_one_minus_mu1; } if( y>0 ){ // Not if-else so y>0 triggered when simulating y>0 - nll = -1 * logmu1; + nll = -logmu1; dev = -2 * logmu1; //deviance1_i(i) = -2 * log_mu1(i); switch( family(1) ){ diff --git a/tests/testthat/test-basic-fits.R b/tests/testthat/test-basic-fits.R index 7220c267..318c4a42 100644 --- a/tests/testthat/test-basic-fits.R +++ b/tests/testthat/test-basic-fits.R @@ -42,8 +42,18 @@ test_that("cv::cv works", { skip_on_covr() # Run cv::cv - CV = cv::cv(out, seed = 123) - expect_equal( as.numeric(CV[['CV crit']]), 1.607874, tolerance = 0.0001 ) + # cv::cv() re-evaluates the call from inside cv + # where the test-local `mesh` isn't visible + # bquote() solves this without assigning to global env. + out_cv <- eval(bquote(tinyVAST( + data = dat, + formula = n ~ s(w), + spatial_domain = .(mesh), # .() looks up mesh in the test environment + space_term = "", + control = tinyVASTcontrol(newton_loops = 1) + ))) + CV = cv::cv(out_cv, seed = 123) + expect_equal( as.numeric(CV[['CV crit']]), 1.60805, tolerance = 0.0001 ) }) # test_that("data_colnames are robust", { diff --git a/tests/testthat/test-index-standardization.R b/tests/testthat/test-index-standardization.R index 73a931f7..2b2c1fae 100644 --- a/tests/testthat/test-index-standardization.R +++ b/tests/testthat/test-index-standardization.R @@ -48,7 +48,7 @@ test_that("Basic index standardization works", { spatial_domain = mesh, family = delta_gamma(type="poisson-link"), control = tinyVASTcontrol(gmrf="sep") ) - expect_equal( my1$opt, my2$opt, tolerance=0.001 ) + expect_equal( my1$opt[c("par","objective")], my2$opt[c("par","objective")], tolerance=0.001 ) # Predicted sample-weighted total index = integrate_output( my1, @@ -70,7 +70,12 @@ test_that("Basic index standardization works", { spatial_domain = mesh, family = delta_gamma(type="poisson-link"), control = tinyVASTcontrol(gmrf="sep") ) - expect_equal( my3$opt, my4$opt, tolerance=0.001 ) + expect_equal( my3$opt[c("par","objective")], my4$opt[c("par","objective")], tolerance=0.001 ) + + # check cAIC + expect_equal( cAIC(my1, method = "stable"), cAIC(my1, method = "fast"), tolerance=0.001 ) + expect_equal( cAIC(my2, method = "stable"), cAIC(my2, method = "fast"), tolerance=0.001 ) + expect_equal( cAIC(my3, method = "stable", what = "EDF"), cAIC(my3, method = "fast", what = "EDF"), tolerance=0.001 ) }) test_that("Index standardization results are identical in VAST, tinyVAST, and sdmTMB", { diff --git a/tests/testthat/test-smooths.R b/tests/testthat/test-smooths.R index 6a57785f..29dcb947 100644 --- a/tests/testthat/test-smooths.R +++ b/tests/testthat/test-smooths.R @@ -114,6 +114,7 @@ test_that("t2 splines throw an error for now", { }) test_that("ti and te splines work", { + skip_on_cran() set.seed(1) dat <- mgcv::gamSim(1, n = 400, scale = 2) dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE)) @@ -262,18 +263,21 @@ test_that("A model with s(x, bs = 'cc') works", { }) test_that("A model with s() by variables works", { - set.seed(1) - # For some reason, it doesn't work on CI but does locally skip_on_ci() skip_on_cran() - dat <- mgcv::gamSim(4) - m_mgcv <- mgcv::gam(y ~ fac + s(x2, by = fac) + s(x0), data = dat) + set.seed(1) + n <- 400 + x2 <- runif(n) + fac <- factor(sample(1:3, n, replace = TRUE)) + mu <- ifelse(fac == 1, sin(2 * pi * x2), ifelse(fac == 2, cos(3 * pi * x2), 8 * (x2 - 0.5)^2)) + dat <- data.frame(y = mu + rnorm(n, sd = 0.5), x2 = x2, fac = fac) + m_mgcv <- mgcv::gam(y ~ fac + s(x2, by = fac), data = dat) p_mgcv <- predict(m_mgcv) - m_s <- sdmTMB::sdmTMB(formula = y ~ fac + s(x2, by = fac) + s(x0), data = dat, spatial = "off") + m_s <- sdmTMB::sdmTMB(formula = y ~ fac + s(x2, by = fac), data = dat, spatial = "off") - m_v <- tinyVAST(formula = y ~ fac + s(x2, by = fac) + s(x0), data = dat) + m_v <- tinyVAST(formula = y ~ fac + s(x2, by = fac), data = dat) expect_s3_class(m_v, "tinyVAST") p_m <- predict(m_mgcv)