Skip to content

Commit

Permalink
few cmdcheck updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Clark committed Mar 19, 2024
1 parent d4d1c51 commit 1ebdff1
Show file tree
Hide file tree
Showing 33 changed files with 437 additions and 40 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ importFrom(scoringRules,es_sample)
importFrom(stats,Gamma)
importFrom(stats,acf)
importFrom(stats,as.formula)
importFrom(stats,binomial)
importFrom(stats,coef)
importFrom(stats,complete.cases)
importFrom(stats,cor)
Expand Down
2 changes: 1 addition & 1 deletion R/families.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Supported mvgam families
#' @importFrom stats make.link dgamma pgamma rgamma qnorm plnorm runif pbeta dlnorm dpois
#' @importFrom stats pnorm ppois plogis gaussian poisson Gamma dnbinom rnbinom dnorm dbeta
#' @importFrom stats rbinom pbinom dbinom qbinom qlogis
#' @importFrom stats binomial rbinom pbinom dbinom qbinom qlogis
#' @importFrom brms lognormal student bernoulli rstudent_t qstudent_t dstudent_t pstudent_t
#' @param link a specification for the family link function. At present these cannot
#' be changed
Expand Down
8 changes: 4 additions & 4 deletions R/get_mvgam_priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ get_mvgam_priors = function(formula,

# Check for gp terms in the validated formula
list2env(check_gp_terms(formula, data_train, family = family),
env = environment())
envir = environment())

# Check for missing rhs in formula
list2env(check_obs_intercept(formula, orig_formula),
env = environment())
envir = environment())

# Validate observation formula
formula <- interpret_mvgam(formula, N = max(data_train$time))
Expand All @@ -202,7 +202,7 @@ get_mvgam_priors = function(formula,
list2env(check_nmix(family, family_char,
trend_formula, trend_model,
trend_map, data_train,
priors = TRUE), env = environment())
priors = TRUE), envir = environment())

# Validate remaining trend arguments
trend_val <- validate_trend_restrictions(trend_model = trend_model,
Expand All @@ -216,7 +216,7 @@ get_mvgam_priors = function(formula,
data_train = data_train,
use_stan = use_stan,
priors = TRUE)
list2env(trend_val, env = environment())
list2env(trend_val, envir = environment())
if(is.null(trend_map)) trend_map <- rlang::missing_arg()
if(is.null(n_lv)) n_lv <- rlang::missing_arg()

Expand Down
7 changes: 6 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ utils::globalVariables(c("y", "year", "smooth_vals", "smooth_num",
"term", "data_test", "object", "row_num", "trends_test",
"trend", "trend_series", "trend_y", ".", "gam",
"group", "mod", "row_id", "byvar", "direction",
"index..time..index", "trend_test", "Var2"))
"index..time..index", "trend_test", "Var2",
"add_cor", "add_ma", "add_nmix", "binomial",
"current", "drop_obs_intercept",
"gp_details", "gp_terms", "k", "mus",
"name", "needed", "nmix_trendmap", "orig_formula",
"trial", "use_var1", "use_var1cor", "xcols_drop"))
4 changes: 0 additions & 4 deletions R/hindcast.mvgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#'@name hindcast.mvgam
#'@importFrom stats predict
#'@inheritParams predict.mvgam
#'@param series Either a \code{integer} specifying which series in the set is to be forecast,
#'or the character string \code{'all'}, specifying that all series should be forecast. This is preferable
#'if the fitted model contained multivariate trends (either as a dynamic factor or \code{VAR} process),
#'as it saves recomputing the full set of trends for each series individually
#'@param ... Ignored
#'@details Posterior retrodictions are drawn from the fitted \code{mvgam} and
#'organized into a convenient format
Expand Down
41 changes: 35 additions & 6 deletions R/mvgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
#' \item`bernoulli()` for binary data
#' \item`nb()` for count data
#' \item`poisson()` for count data
#' \item`binomial()` for count data with imperfect detection when the number of trials is known
#' \item`binomial()` for count data with imperfect detection when the number of trials is known;
#' note that the `cbind()` function must be used to bind the discrete observations and the number
#' of trials
#' \item`nmix()` for count data with imperfect detection when the number of trials
#' is unknown and should be modeled via a State-Space N-Mixture model.
#' The latent states are Poisson, capturing the 'true' latent
Expand Down Expand Up @@ -571,6 +573,33 @@
#' scale_y_discrete(labels = mod$trend_model$changepoints) +
#' labs(y = 'Potential changepoint',
#' x = 'Rate change')
#'
#' # Example showcasing how cbind() is needed for Binomial observations
#' # Simulate two time series of Binomial trials
#' trials <- sample(c(20:25), 50, replace = TRUE)
#' x <- rnorm(50)
#' detprob1 <- plogis(-0.5 + 0.9*x)
#' detprob2 <- plogis(-0.1 -0.7*x)
#' dat <- rbind(data.frame(y = rbinom(n = 50, size = trials, prob = detprob1),
#' time = 1:50,
#' series = 'series1',
#' x = x,
#' ntrials = trials),
#' data.frame(y = rbinom(n = 50, size = trials, prob = detprob2),
#' time = 1:50,
#' series = 'series2',
#' x = x,
#' ntrials = trials)) %>%
#' dplyr::mutate(series = as.factor(series)) %>%
#' dplyr::arrange(time, series)
#'
#' # Fit a model using the binomial() family; must specify observations
#' # and number of trials in the cbind() wrapper
#' mod <- mvgam(cbind(y, ntrials) ~ series + s(x, by = series)
#' family = binomial(),
#' data = dat)
#' summary(mod)
#'
#' }
#'@export

Expand Down Expand Up @@ -639,11 +668,11 @@ mvgam = function(formula,

# Check for gp terms in the validated formula
list2env(check_gp_terms(formula, data_train, family = family),
env = environment())
envir = environment())

# Check for missing rhs in formula
list2env(check_obs_intercept(formula, orig_formula),
env = environment())
envir = environment())

# Check for brmspriors
if(!missing(priors)){
Expand Down Expand Up @@ -704,7 +733,7 @@ mvgam = function(formula,
# Nmixture additions?
list2env(check_nmix(family, family_char,
trend_formula, trend_model,
trend_map, data_train), env = environment())
trend_map, data_train), envir = environment())

# Validate remaining trend arguments
trend_val <- validate_trend_restrictions(trend_model = trend_model,
Expand All @@ -717,7 +746,7 @@ mvgam = function(formula,
n_lv = n_lv,
data_train = data_train,
use_stan = use_stan)
list2env(trend_val, env = environment())
list2env(trend_val, envir = environment())
if(is.null(trend_map)) trend_map <- rlang::missing_arg()
if(is.null(n_lv)) n_lv <- rlang::missing_arg()

Expand Down Expand Up @@ -786,7 +815,7 @@ mvgam = function(formula,
data_train = data_train,
family = family,
family_char = family_char,
knots = knots), env = environment())
knots = knots), envir = environment())

# Update initial values of lambdas using the full estimates from the
# fitted gam model to speed convergence; remove initial betas so that the
Expand Down
4 changes: 3 additions & 1 deletion man/get_mvgam_priors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions man/hindcast.mvgam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion man/mvgam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/update.mvgam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions misc/BeamOptions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
\put(\LenToUnit{0.88\paperwidth},\LenToUnit{0.88\paperheight}){#1}}}
\AddToShipoutPictureFG{
\AtPagemyUpperLeft{{
\href{http://mc-stan.org/}{\includegraphics[scale=0.25,keepaspectratio]{../logos/Stan.png}}
\href{https://nicholasjclark.github.io/mvgam/}{\includegraphics[scale=0.5,keepaspectratio]{mvgam_logo.png}}
\hspace{0.025in}
\href{http://ggplot2.tidyverse.org}{\includegraphics[scale=0.5,keepaspectratio]{../logos/ggplot2.png}}
\href{https://mc-stan.org/}{\includegraphics[scale=0.75,keepaspectratio]{stan_logo.png}}
}}
}%
Binary file added misc/Rplots.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions misc/cache/__packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
knitr
ggplot2
nlme
mgcv
Rcpp
brms
marginaleffects
insight
mvgam
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added misc/figure/unnamed-chunk-3-1.pdf
Binary file not shown.
Binary file added misc/figure/unnamed-chunk-4-1.pdf
Binary file not shown.
Binary file added misc/figure/unnamed-chunk-5-1.pdf
Binary file not shown.
Binary file added misc/figure/unnamed-chunk-6-1.pdf
Binary file not shown.
Binary file added misc/figure/unnamed-chunk-7-1.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions misc/mvgam_cheatsheet-concordance.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\Sconcordance{concordance:mvgam_cheatsheet.tex:mvgam_cheatsheet.Rnw:%
1 7 1 50 0 1 6 3 1 1 7 1 1 1 8 95 1 4 0 34 1 %
6 0 14 1 6 0 10 1 1 3 34 1 1 6 8 1 4 0 5 1 9 %
0 22 1}
34 changes: 21 additions & 13 deletions misc/mvgam_cheatsheet.Rnw
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
\documentclass[final,9pt,fleqn]{beamer}
\input{BeamOptions.tex}

\setbeamertemplate{footline}{\hfill {\footnotesize \href{https://creativecommons.org/licenses/by-sa/4.0/}{CC BY-SA 4.0} $\circ$ Nicholas J. Clark $\circ$ Learn more at \href{https://nicholasjclark.github.io/mvgam/index.html}{https://nicholasjclark.github.io/mvgam/index.html} $\circ$ package version $1.0.8$ $\circ$ updated: \today} \hspace {0.1in} \vspace{0.1in}}
\setbeamertemplate{footline}{\hfill {\footnotesize \href{https://creativecommons.org/licenses/by-sa/4.0/}{CC BY-SA 4.0} $\circ$ Nicholas J. Clark $\circ$ Learn more at \href{https://nicholasjclark.github.io/mvgam/index.html}{https://nicholasjclark.github.io/mvgam/index.html} $\circ$ package version $1.0.9$ $\circ$ updated: \today} \hspace {0.1in} \vspace{0.1in}}

\begin{document}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@



<<setup, include=FALSE>>=
library(knitr)
Expand Down Expand Up @@ -52,7 +60,7 @@ Usage: \texttt{\color{Orchid} mvgam(formula, trend\_formula, data, trend\_model,
\texttt{\color{Orchid} data}: a \texttt{data.frame} or \texttt{list} containing the response variable(s) and optional predictor variables. See \href{https://nicholasjclark.github.io/mvgam/articles/data_in_mvgam.html}{the data formatting vignette} for guidance on data preparation

\medskip
\texttt{\color{Orchid} trend\_model}: optional latent dynamic process. Options include:
\texttt{\color{Orchid} trend\_model}: optional latent dynamic process. Options include:
\begin{itemize}

\item\texttt{\color{Orchid} None}: default, no dynamic trend
Expand All @@ -72,7 +80,7 @@ Usage: \texttt{\color{Orchid} mvgam(formula, trend\_formula, data, trend\_model,
For autoregressive processes (\texttt{\color{Orchid} RW(), AR() or VAR()}), moving average and correlated process errors can also be specified by changing the \texttt{\color{Orchid} ma} and \texttt{\color{Orchid} cor} arguments

\medskip
\texttt{\color{Orchid} family}: observation distribution. Options include:
\texttt{\color{Orchid} family}: observation distribution. Options include (among others):
\begin{itemize}

\item\texttt{\color{Orchid} gaussian()}: Gaussian with identity link
Expand All @@ -94,15 +102,15 @@ For autoregressive processes (\texttt{\color{Orchid} RW(), AR() or VAR()}), movi
See \href{https://nicholasjclark.github.io/mvgam/articles/mvgam_overview.html}{the introductory vignette} for more guidance on supported families and dynamic processes

\medskip
\texttt{\color{Orchid} ...}: other arguments such as user-specified \texttt{\color{Orchid} priors}, \texttt{\color{Orchid} newdata} for generating probabilistic forecasts and options to control \texttt{\color{Orchid} Stan} MCMC parameters
\texttt{\color{Orchid} ...}: other arguments such as user-specified \texttt{\color{Orchid} priors}, \texttt{\color{Orchid} newdata} for generating probabilistic forecasts and options to control \texttt{\color{Orchid} Stan} MCMC parameters

\medskip
\textbf{\color{BrickRed} Prior to modelling}, it is useful to:
\textbf{\color{BrickRed} Prior to modelling}, it is useful to:
\begin{itemize}

\item Inspect features of the data with \texttt{\color{Orchid} plot\_mvgam\_series()}

\item Ensure there are no \texttt{\color{Orchid} NA}'s in predictors (though \texttt{\color{Orchid} NA}'s are allowed in response variables). See \href{https://nicholasjclark.github.io/mvgam/articles/data_in_mvgam.html}{the data formatting vignette} for guidance on data preparation
\item Ensure there are no \texttt{\color{Orchid} NA}'s in predictors (though \texttt{\color{Orchid} NA}'s are allowed in response variables). See \href{https://nicholasjclark.github.io/mvgam/articles/data_in_mvgam.html}{the data formatting vignette} for guidance on data preparation

\item Inspect default priors with \texttt{\color{Orchid} get\_mvgam\_priors()}

Expand Down Expand Up @@ -168,9 +176,9 @@ conditional_effects(model)[[1]] + xlim(c(1, 12.1))

For most \texttt{mvgam} models, functions from the \texttt{marginaleffects} package can be used for more targeted prediction-based inference. See \href{https://marginaleffects.com/}{The Marginal Effects Zoo} for guidance on computing and plotting predictions, slopes and comparisons
<<fig.width=4, fig.height=1.75, fig.align='center', warning=FALSE, echo=FALSE>>=
post_contrasts <- avg_comparisons(model,
post_contrasts <- avg_comparisons(model,
variables = list(season = c(5, 11))) %>%
posteriordraws()
posteriordraws()
post_contrasts %>%
ggplot(aes(x = draw)) +
Expand Down Expand Up @@ -205,13 +213,13 @@ Use \texttt{\color{Orchid} ppc(model)} to plot various kinds of posterior predic
Extract in-sample posterior predictions with \texttt{\color{Orchid} hindcast(model)}. If validation data exist, generate forecast predictions with \texttt{\color{Orchid} forecast(model, newdata = newdata)}. As above, change the \texttt{\color{Orchid} type} argument for predictions on different scales. Both functions generate an object of class \texttt{mvgam\_forecast}, that can be plotted with an \texttt{S3} \texttt{\color{Orchid} plot()} function. See \href{https://nicholasjclark.github.io/mvgam/articles/forecast_evaluation.html}{the forecasting vignette} for more details about how to produce forecasts.

<<fig.width=5.4, fig.height=2.5, fig.align='center', warning=FALSE, results='hide',echo=FALSE>>=
par(family = "serif",
las = 1,
par(family = "serif",
las = 1,
mar=c(3,3,2,2),
mgp = c(2,0.5,0),
bty = "l",
cex.axis = 0.9,
cex.lab = 0.9,
bty = "l",
cex.axis = 0.9,
cex.lab = 0.9,
cex.main = 0.9,
xaxs = 'r',
yaxs = 'r',
Expand Down
Binary file modified misc/mvgam_cheatsheet.pdf
Binary file not shown.
Binary file modified misc/mvgam_cheatsheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1ebdff1

Please sign in to comment.