Skip to content

Commit 3ba0e7e

Browse files
author
Nicholas Clark
committed
move some more dependencies
1 parent fe0e8d2 commit 3ba0e7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+466
-175
lines changed

.github/workflows/R-CMD-check-rstan.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ jobs:
5656
wrswoR
5757
tweedie
5858
splines2
59+
scoringRules
60+
matrixStats
5961
xts
6062
collapse
6163
rmarkdown

.github/workflows/R-CMD-check.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ jobs:
5454
wrswoR
5555
tweedie
5656
splines2
57+
scoringRules
58+
matrixStats
5759
xts
5860
collapse
5961
rmarkdown

.github/workflows/test-coverage.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jobs:
3838
wrswoR
3939
tweedie
4040
splines2
41+
scoringRules
42+
matrixStats
4143
xts
4244
collapse
4345
rmarkdown

DESCRIPTION

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Package: mvgam
22
Title: Multivariate (Dynamic) Generalized Additive Models
33
Version: 1.1.2
4-
Date: 2024-05-10
4+
Date: 2024-06-28
55
Authors@R: person("Nicholas J", "Clark", , "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7131-3301"))
66
Description: Fit Bayesian Dynamic Generalized Additive Models to sets of time series. Users can build dynamic nonlinear State-Space models that can incorporate semiparametric effects in observation and process components, using a wide range of observation families. Estimation is performed using Markov Chain Monte Carlo with Hamiltonian Monte Carlo in the software 'Stan'. References: Clark & Wells (2022) <doi:10.1111/2041-210X.13974>.
77
URL: https://github.com/nicholasjclark/mvgam, https://nicholasjclark.github.io/mvgam/
88
BugReports: https://github.com/nicholasjclark/mvgam/issues
99
License: MIT + file LICENSE
1010
Depends:
1111
R (>= 3.6.0),
12-
mgcv (>= 1.8-13),
1312
brms (>= 2.17)
1413
Imports:
1514
methods,
15+
mgcv (>= 1.8-13),
1616
insight (>= 0.19.1),
1717
marginaleffects (>= 0.16.0),
1818
Rcpp (>= 0.12.0),
@@ -22,13 +22,11 @@ Imports:
2222
rstantools (>= 2.1.1),
2323
bayesplot (>= 1.5.0),
2424
ggplot2 (>= 2.0.0),
25-
matrixStats,
2625
parallel,
2726
pbapply,
2827
mvnfast,
2928
purrr,
3029
zoo,
31-
scoringRules,
3230
smooth,
3331
dplyr,
3432
magrittr,
@@ -39,6 +37,8 @@ LazyData: true
3937
Roxygen: list(markdown = TRUE)
4038
RoxygenNote: 7.2.3
4139
Suggests:
40+
scoringRules,
41+
matrixStats,
4242
cmdstanr (>= 0.5.0),
4343
tweedie,
4444
splines2,

NAMESPACE

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export(PW)
6868
export(RW)
6969
export(VAR)
7070
export(add_residuals)
71+
export(betar)
7172
export(code)
7273
export(compare_mvgams)
7374
export(dynamic)
@@ -78,6 +79,7 @@ export(hindcast)
7879
export(lfo_cv)
7980
export(lv_correlations)
8081
export(mvgam)
82+
export(nb)
8183
export(neff_ratio)
8284
export(nmix)
8385
export(nuts_params)
@@ -97,8 +99,11 @@ export(score)
9799
export(series_to_mvgam)
98100
export(sim_mvgam)
99101
export(student_t)
102+
export(te)
103+
export(ti)
100104
export(tweedie)
101105
export(variables)
106+
importFrom(Matrix,nearPD)
102107
importFrom(Rcpp,evalCpp)
103108
importFrom(bayesplot,color_scheme_get)
104109
importFrom(bayesplot,color_scheme_set)
@@ -189,7 +194,6 @@ importFrom(rlang,warn)
189194
importFrom(rstantools,posterior_epred)
190195
importFrom(rstantools,posterior_linpred)
191196
importFrom(rstantools,posterior_predict)
192-
importFrom(scoringRules,es_sample)
193197
importFrom(stats,.getXlevels)
194198
importFrom(stats,Gamma)
195199
importFrom(stats,acf)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# mvgam 1.1.2
22
* Added options for silencing some of the 'Stan' compiler and modeling messages using the `silent` argument in `mvgam()`
3+
* Moved a number of packages from 'Depends' to 'Imports' for simpler package loading and fewer potential masking conflicts
34
* Improved efficiency of the model initialisation by tweaking parameters of the underlying 'mgcv' `gam` object's convergence criteria, resulting in much faster model setups
45
* Added an option to use `trend_model = 'None'` in State-Space models, increasing flexibility by ensuring the process error evolves as white noise (#51)
56
* Added an option to use the non-centred parameterisation for some autoregressive trend models,

R/conditional_effects.R

-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ conditional_effects.mvgam = function(x,
200200

201201
class(out) <- 'mvgam_conditional_effects'
202202
return(out)
203-
204203
}
205204

206205
#' @rdname conditional_effects.mvgam

R/evaluate_mvgams.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,19 @@ sis_score <- function(truth, fc, interval_width = 0.9,
714714
}
715715

716716
#' Compute the multivariate energy score
717-
#' @importFrom scoringRules es_sample
718717
#' @noRd
719718
energy_score <- function(truth, fc, log = FALSE) {
719+
insight::check_if_installed("scoringRules",
720+
reason = 'to calculate energy scores')
721+
720722
# es_sample can't handle any NAs
721723
has_nas <- apply(fc, 2, function(x) any(is.na(x)))
722724
fc <- fc[,!has_nas]
723725
if(log){
724726
truth <- log(truth + 0.001)
725727
fc <- log(fc + 0.001)
726728
}
727-
es <- es_sample(y = truth, dat = fc)
729+
es <- scoringRules::es_sample(y = truth, dat = fc)
728730
return(es)
729731
}
730732

R/families.R

+14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#' @importFrom stats pnorm ppois plogis gaussian poisson Gamma dnbinom rnbinom dnorm dbeta
44
#' @importFrom stats binomial rbinom pbinom dbinom qbinom qlogis
55
#' @importFrom brms lognormal student bernoulli rstudent_t qstudent_t dstudent_t pstudent_t dbeta_binomial rbeta_binomial pbeta_binomial
6+
#' @importFrom mgcv betar nb
67
#' @param link a specification for the family link function. At present these cannot
78
#' be changed
9+
#' @param ... Arguments to be passed to the \pkg{mgcv} version of the associated functions
810
#' @details \code{mvgam} currently supports the following standard observation families:
911
#'\itemize{
1012
#' \item \code{\link[stats]{gaussian}} with identity link, for real-valued data
@@ -66,6 +68,18 @@ student_t = function(link = 'identity'){
6668
class = c("extended.family", "family"))
6769
}
6870

71+
#' @rdname mvgam_families
72+
#' @export
73+
betar = function(...){
74+
mgcv::betar(...)
75+
}
76+
77+
#' @rdname mvgam_families
78+
#' @export
79+
nb = function(...){
80+
mgcv::nb(...)
81+
}
82+
6983
#' @rdname mvgam_families
7084
#' @examples
7185
#' \dontrun{

R/mvgam_diagnostics.R

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ rhat.mvgam <- function(x, pars = NULL, ...) {
7979
#' @export neff_ratio
8080
#' @export
8181
neff_ratio.mvgam <- function(object, pars = NULL, ...) {
82+
insight::check_if_installed("matrixStats",
83+
reason = 'to calculate effective sample sizes')
8284
# bayesplot uses outdated ess code from rstan
8385
# bayesplot::neff_ratio(object$fit, pars = pars, ...)
8486
if(is.null(pars)){
@@ -105,6 +107,8 @@ neff_ratio.mvgam <- function(object, pars = NULL, ...) {
105107
#' @export neff_ratio
106108
#' @export
107109
neff_ratio.mvgam <- function(object, pars = NULL, ...) {
110+
insight::check_if_installed("matrixStats",
111+
reason = 'to calculate effective sample sizes')
108112
# bayesplot uses outdated ess code from rstan
109113
# bayesplot::neff_ratio(object$fit, pars = pars, ...)
110114
if(is.null(pars)){

R/sim_mvgam.R

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#'
77
#'@importFrom stats rnorm rbeta rpois rlnorm rgamma cor cov2cor cov stl ts
88
#'@importFrom brms lognormal
9+
#'@importFrom Matrix nearPD
910
#'@param T \code{integer}. Number of observations (timepoints)
1011
#'@param n_series \code{integer}. Number of discrete time series
1112
#'@param seasonality \code{character}. Either \code{shared}, meaning that all series share the exact same seasonal pattern,

R/ti.R

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This file contains functions dealing with the extended
2+
# formula syntax to specify smooth terms via mgcv
3+
4+
#' Defining smooths in \pkg{mvgam} formulae
5+
#'
6+
#' Functions used in definition of smooth terms within model formulae.
7+
#' The functions do not evaluate a (spline) smooth - they exist purely
8+
#' to help set up mvgam models using spline based smooths.
9+
#'
10+
#' @param ... Arguments passed to \code{\link[mgcv:ti]{mgcv::ti}} or
11+
#' \code{\link[mgcv:ti]{mgcv::te}}
12+
#'
13+
#' @details The functions defined here are just simple wrappers of the respective
14+
#' functions of the \pkg{mgcv} package. When using them, please cite the
15+
#' appropriate references obtained via \code{citation("mgcv")}.
16+
#'
17+
#' @seealso \code{\link[mgcv:ti]{mgcv::ti}}, \code{\link[mgcv:ti]{mgcv::te}}
18+
#'
19+
#'
20+
#' @examples
21+
#' \dontrun{
22+
#' # Simulate some data
23+
#' dat <- mgcv::gamSim(1, n = 200, scale = 2)
24+
#'
25+
#' # Fit univariate smooths for all predictors
26+
#' fit1 <- mvgam(y ~ s(x0) + s(x1) + s(x2) + s(x3),
27+
#' data = dat, chains = 2, family = gaussian())
28+
#' summary(fit1)
29+
#' conditional_effects(fit1)
30+
#'
31+
#' # Fit a more complicated smooth model
32+
#' fit2 <- mvgam(y ~ te(x0, x1) + s(x2, by = x3),
33+
#' data = dat, chains = 2, family = gaussian())
34+
#' summary(fit2)
35+
#' conditional_effects(fit2)
36+
#' }
37+
#'
38+
#' @rdname ti
39+
#' @export
40+
ti <- function(...) {
41+
mgcv::ti(...)
42+
}
43+
44+
#' @rdname ti
45+
#' @export
46+
te <- function(...) {
47+
mgcv::te(...)
48+
}

README.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ plot_mvgam_smooth(lynx_mvgam, series = 1,
182182

183183
As for many types of regression models, it is often more useful to plot model effects on the outcome scale. `mvgam` has support for the wonderful `marginaleffects` package, allowing a wide variety of posterior contrasts, averages, conditional and marginal predictions to be calculated and plotted. Below is the conditional effect of season plotted on the outcome scale, for example:
184184
```{r, fig.alt = "Using marginaleffects and mvgam to plot GAM smooth functions in R"}
185-
require(ggplot2)
185+
require(ggplot2); require(marginaleffects)
186186
plot_predictions(lynx_mvgam, condition = 'season', points = 0.5) +
187187
theme_classic()
188188
```

README.md

+39-38
Original file line numberDiff line numberDiff line change
@@ -235,30 +235,30 @@ summary(lynx_mvgam)
235235
#>
236236
#> GAM coefficient (beta) estimates:
237237
#> 2.5% 50% 97.5% Rhat n_eff
238-
#> (Intercept) 6.20 6.600 6.90 1 1177
239-
#> s(season).1 -0.55 -0.052 0.47 1 1078
240-
#> s(season).2 0.54 1.200 1.90 1 943
241-
#> s(season).3 1.10 1.900 2.60 1 920
242-
#> s(season).4 -0.11 0.540 1.20 1 1126
243-
#> s(season).5 -1.30 -0.580 0.14 1 757
244-
#> s(season).6 -1.10 -0.370 0.44 1 1156
245-
#> s(season).7 -0.24 0.670 1.50 1 1167
246-
#> s(season).8 0.12 1.100 1.90 1 630
247-
#> s(season).9 -0.54 0.050 0.66 1 977
248-
#> s(season).10 -1.40 -0.940 -0.53 1 1130
238+
#> (Intercept) 6.20 6.600 6.90 1.00 1100
239+
#> s(season).1 -0.56 -0.070 0.51 1.00 1202
240+
#> s(season).2 0.57 1.200 1.90 1.00 1102
241+
#> s(season).3 1.10 1.900 2.70 1.00 1073
242+
#> s(season).4 -0.14 0.520 1.20 1.00 1169
243+
#> s(season).5 -1.30 -0.590 0.18 1.00 961
244+
#> s(season).6 -1.10 -0.350 0.41 1.00 1257
245+
#> s(season).7 -0.19 0.670 1.40 1.00 1171
246+
#> s(season).8 0.10 1.100 1.90 1.01 627
247+
#> s(season).9 -0.54 0.051 0.63 1.00 1044
248+
#> s(season).10 -1.40 -0.950 -0.54 1.00 1069
249249
#>
250250
#> Approximate significance of GAM smooths:
251-
#> edf Ref.df Chi.sq p-value
252-
#> s(season) 9.98 10 38892 0.0012 **
251+
#> edf Ref.df Chi.sq p-value
252+
#> s(season) 9.93 10 46.1 1.5e-06 ***
253253
#> ---
254254
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
255255
#>
256256
#> Latent trend AR parameter estimates:
257257
#> 2.5% 50% 97.5% Rhat n_eff
258-
#> ar1[1] 0.610 0.86 0.99 1 1330
259-
#> ar2[1] 0.055 0.45 0.83 1 496
260-
#> ar3[1] 0.068 0.45 0.95 1 413
261-
#> sigma[1] 0.370 0.46 0.58 1 907
258+
#> ar1[1] 0.600 0.86 0.99 1.00 1331
259+
#> ar2[1] 0.038 0.45 0.86 1.00 421
260+
#> ar3[1] 0.079 0.45 0.93 1.01 379
261+
#> sigma[1] 0.360 0.46 0.59 1.00 663
262262
#>
263263
#> Stan MCMC diagnostics:
264264
#> n_eff / iter looks reasonable for all parameters
@@ -267,7 +267,7 @@ summary(lynx_mvgam)
267267
#> 0 of 2000 iterations saturated the maximum tree depth of 12 (0%)
268268
#> E-FMI indicated no pathological behavior
269269
#>
270-
#> Samples were drawn using NUTS(diag_e) at Wed Jun 12 9:37:37 AM 2024.
270+
#> Samples were drawn using NUTS(diag_e) at Fri Jun 28 3:39:52 PM 2024.
271271
#> For each parameter, n_eff is a crude measure of effective sample size,
272272
#> and Rhat is the potential scale reduction factor on split MCMC chains
273273
#> (at convergence, Rhat = 1)
@@ -374,7 +374,8 @@ be calculated and plotted. Below is the conditional effect of season
374374
plotted on the outcome scale, for example:
375375

376376
``` r
377-
require(ggplot2)
377+
require(ggplot2); require(marginaleffects)
378+
#> Loading required package: marginaleffects
378379
plot_predictions(lynx_mvgam, condition = 'season', points = 0.5) +
379380
theme_classic()
380381
```
@@ -386,8 +387,8 @@ series (testing and training)
386387

387388
``` r
388389
plot(lynx_mvgam, type = 'forecast', newdata = lynx_test)
389-
#> Out of sample DRPS:
390-
#> 1879.7308065
390+
#> Out of sample CRPS:
391+
#> 1846.84576525
391392
```
392393

393394
<img src="man/figures/README-unnamed-chunk-20-1.png" alt="Plotting forecast distributions using mvgam in R" width="60%" style="display: block; margin: auto;" />
@@ -517,41 +518,41 @@ summary(mod, include_betas = FALSE)
517518
#>
518519
#> Observation precision parameter estimates:
519520
#> 2.5% 50% 97.5% Rhat n_eff
520-
#> phi[1] 5.6 8.3 12 1 1528
521-
#> phi[2] 5.8 8.8 13 1 1347
522-
#> phi[3] 5.7 8.4 12 1 1430
521+
#> phi[1] 5.4 8.3 12 1 1248
522+
#> phi[2] 5.7 8.6 13 1 1312
523+
#> phi[3] 5.6 8.5 12 1 1724
523524
#>
524525
#> GAM coefficient (beta) estimates:
525-
#> 2.5% 50% 97.5% Rhat n_eff
526-
#> (Intercept) -0.15 0.19 0.45 1 1001
526+
#> 2.5% 50% 97.5% Rhat n_eff
527+
#> (Intercept) -0.2 0.19 0.46 1.01 566
527528
#>
528529
#> Approximate significance of GAM smooths:
529530
#> edf Ref.df Chi.sq p-value
530-
#> s(season) 4.297 5 39.37 <2e-16 ***
531-
#> s(season):seriesseries_1 0.936 4 0.50 0.98
532-
#> s(season):seriesseries_2 0.935 4 0.39 0.99
533-
#> s(season):seriesseries_3 0.645 4 2.81 0.86
531+
#> s(season) 3.872 5 29.63 1.6e-05 ***
532+
#> s(season):seriesseries_1 0.615 4 0.77 0.98
533+
#> s(season):seriesseries_2 1.012 4 0.30 0.99
534+
#> s(season):seriesseries_3 1.106 4 1.54 0.81
534535
#> ---
535536
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
536537
#>
537538
#> Latent trend marginal deviation (alpha) and length scale (rho) estimates:
538539
#> 2.5% 50% 97.5% Rhat n_eff
539-
#> alpha_gp[1] 0.078 0.42 0.90 1.00 877
540-
#> alpha_gp[2] 0.380 0.73 1.30 1.00 1130
541-
#> alpha_gp[3] 0.170 0.47 0.97 1.00 886
542-
#> rho_gp[1] 1.200 3.70 14.00 1.01 797
543-
#> rho_gp[2] 1.700 7.30 34.00 1.02 463
544-
#> rho_gp[3] 1.300 4.70 20.00 1.00 722
540+
#> alpha_gp[1] 0.051 0.41 0.92 1.01 525
541+
#> alpha_gp[2] 0.360 0.72 1.20 1.00 946
542+
#> alpha_gp[3] 0.150 0.46 1.00 1.00 659
543+
#> rho_gp[1] 1.100 3.80 15.00 1.01 370
544+
#> rho_gp[2] 1.900 7.80 37.00 1.01 365
545+
#> rho_gp[3] 1.400 5.10 21.00 1.00 645
545546
#>
546547
#> Stan MCMC diagnostics:
547548
#> n_eff / iter looks reasonable for all parameters
548549
#> Rhat looks reasonable for all parameters
549-
#> 4 of 2000 iterations ended with a divergence (0.2%)
550+
#> 12 of 2000 iterations ended with a divergence (0.6%)
550551
#> *Try running with larger adapt_delta to remove the divergences
551552
#> 0 of 2000 iterations saturated the maximum tree depth of 12 (0%)
552553
#> E-FMI indicated no pathological behavior
553554
#>
554-
#> Samples were drawn using NUTS(diag_e) at Wed Jun 12 9:38:57 AM 2024.
555+
#> Samples were drawn using NUTS(diag_e) at Fri Jun 28 3:40:36 PM 2024.
555556
#> For each parameter, n_eff is a crude measure of effective sample size,
556557
#> and Rhat is the potential scale reduction factor on split MCMC chains
557558
#> (at convergence, Rhat = 1)

0 commit comments

Comments
 (0)