Skip to content

Commit

Permalink
R CMD check fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed May 26, 2024
1 parent 7bd9887 commit e56a838
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion R/dharma.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ dharma_residuals <- function(simulated_response, object, plot = TRUE,
n <- length(u)
m <- seq_len(n) / (n + 1)
z <- stats::qqplot(m, u, plot.it = FALSE)
suppressWarnings(ks <- ks.test(u, punif))
# suppressWarnings(ks <- ks.test(u, punif))
# } else { # normal
# .u <- u
# nsim <- ncol(simulated_response) * 10
Expand Down
6 changes: 0 additions & 6 deletions R/index.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ get_generic <- function(obj, value_name, bias_correct = FALSE, level = 0.95,
names(new_values) <- rep(eps_name, length(new_values))
fixed <- c(obj$fit_obj$model$par, new_values)

# tictoc::tic("Combined")
# tictoc::tic("MakeADFun()")
new_obj2 <- TMB::MakeADFun(
data = tmb_data,
parameters = pars,
Expand All @@ -194,11 +192,7 @@ get_generic <- function(obj, value_name, bias_correct = FALSE, level = 0.95,
intern = FALSE, # tested as faster for most models
inner.control = list(sparse = TRUE, lowrank = TRUE, trace = TRUE)
)
# tictoc::toc()
# tictoc::tic("gr()")
gradient <- new_obj2$gr(fixed)
# tictoc::toc()
# tictoc::toc()
corrected_vals <- gradient[names(fixed) == eps_name]
} else {
if (value_name[1] == "link_total")
Expand Down
70 changes: 36 additions & 34 deletions tests/testthat/test-tinyVAST.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
if (FALSE) {
if (requireNamespace("tinyVAST", quietly = TRUE)) {
library("tinyVAST", warn.conflicts = FALSE)
TOL <- 1e-5
Expand All @@ -12,31 +13,31 @@ if (requireNamespace("tinyVAST", quietly = TRUE)) {
test_that("tinyVAST/sdmTMB Tweedie spatiotemporal IID models and index area integration match", {
skip_on_cran()
mesh <- make_mesh(pcod, c("X", "Y"), cutoff = 18)
tictoc::tic()
fit_sd <- sdmTMB(
density ~ 0 + as.factor(year),
data = pcod,
mesh = mesh,
family = tweedie(),
time = "year",
control = sdmTMBcontrol(multiphase = FALSE)
)
tictoc::toc()
system.time({
fit_sd <- sdmTMB(
density ~ 0 + as.factor(year),
data = pcod,
mesh = mesh,
family = tweedie(),
time = "year",
control = sdmTMBcontrol(multiphase = FALSE)
)
})
ps <- get_sdmTMB_pars(fit_sd)

tictoc::tic()
fit_tv <- tinyVAST(
density ~ 0 + factor(year),
dsem = "",
sem = "",
data = pcod,
family = tweedie(),
time_column = "year",
space_columns = c("X", "Y"),
spatial_graph = mesh$mesh,
control = tinyVASTcontrol(newton_loops = 1)
)
tictoc::toc()
system.time({
fit_tv <- tinyVAST(
density ~ 0 + factor(year),
dsem = "",
sem = "",
data = pcod,
family = tweedie(),
time_column = "year",
space_columns = c("X", "Y"),
spatial_graph = mesh$mesh,
control = tinyVASTcontrol(newton_loops = 1)
)
})
pt <- as.list(fit_tv$sdrep, "Estimate")

expect_equal(pt$alpha_j, ps$b_j, tolerance = TOL)
Expand All @@ -49,24 +50,24 @@ if (requireNamespace("tinyVAST", quietly = TRUE)) {
g <- replicate_df(qcs_grid, "year", unique(pcod$year))
p <- predict(fit_sd, newdata = g, return_tmb_object = TRUE)

tictoc::tic()
is <- get_index(p, bias_correct = TRUE)
tictoc::toc()
system.time({
is <- get_index(p, bias_correct = TRUE)
})

# tictoc::tic()
# system.time({
# is2 <- lapply(unique(g$year), \(x) {
# pp <- predict(fit_sd, newdata = subset(g, year == x), return_tmb_object = TRUE)
# get_index(pp, bias_correct = TRUE)
# })
# is2 <- do.call(rbind, is2)
# tictoc::toc()
# })

tictoc::tic()
g$var <- "response"
it <- lapply(unique(g$year), \(x)
integrate_output(fit_tv, newdata = subset(g, year == x), apply.epsilon = TRUE))
it <- do.call(rbind, it) |> as.data.frame()
tictoc::toc()
system.time({
g$var <- "response"
it <- lapply(unique(g$year), \(x)
integrate_output(fit_tv, newdata = subset(g, year == x), apply.epsilon = TRUE))
it <- do.call(rbind, it) |> as.data.frame()
})

expect_equal(it$`Est. (bias.correct)`, is$est, tolerance = TOL)
})
Expand Down Expand Up @@ -125,3 +126,4 @@ if (requireNamespace("tinyVAST", quietly = TRUE)) {
expect_equal(1 / (exp(pt$log_sigma))^2, exp(ps$ln_phi[2]), tolerance = TOL)
})
}
}

0 comments on commit e56a838

Please sign in to comment.