Skip to content

Commit 9b9f098

Browse files
committed
Make various minor roxygen/package structure/vignette suggestions.
1 parent 3f10111 commit 9b9f098

File tree

8 files changed

+117
-97
lines changed

8 files changed

+117
-97
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: nimbleMacros
22
Version: 0.1.0
33
Date: 2025-01-28
4-
Title: Macros Generating 'nimble' Code for Linear Models
4+
Title: Macros Generating 'nimble' Code for Linear Models.
55
Authors@R: c(person("Ken", "Kellner", email="contact@kenkellner.com",
66
role=c("cre","aut")),
77
person("Perry", "de Valpine", role = "aut"),
@@ -16,8 +16,8 @@ Suggests:
1616
knitr,
1717
markdown,
1818
testthat
19-
Description: Macros to generate 'nimble' code from a concise syntax. Included are macros for generating linear modeling code using a formula-based syntax, and for 'for' loops. (de Valpine et al. (2015) <doi:10.1080/10618600.2016.1172487>).
20-
License: GPL-3
19+
Description: Macros to generate 'nimble' code from a concise syntax. Included are macros for generating linear modeling code using a formula-based syntax and for 'for' loops.
20+
License: BSD_3_clause + file LICENSE | GPL (>= 2)
2121
NeedsCompilation: no
2222
Encoding: UTF-8
2323
VignetteBuilder: knitr

LICENSE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
YEAR: 2025
2+
COPYRIGHT HOLDER: Ken Kellner, Perry de Valpine, Christopher Paciorek, Daniel Turek
3+
ORGANIZATION: University of California

R/FORLOOP.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' mu[1:n] <- FORLOOP(beta[1] + beta[2]*x[1:n])
1818
#' })
1919
#'
20-
#' mod <- nimbleModel(code, constants=list(n=10))
20+
#' mod <- nimbleModel(code, constants = list(n=10))
2121
#' mod$getCode()
2222
NULL
2323

R/LINPRED.R

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
#' @param formula An R formula, possibly with the parameters followed by
1111
#' brackets containing indices. If there are no indices, the macro attempts
1212
#' to guess the correct indices from the context. The formula must be
13-
#' right-hand side only (e.g. ~x). This must always be the first argument supplied
14-
#' to LINPRED.
15-
#' @param link A link function which will be applied to the
13+
#' right-hand side only (e.g. \code{~x}). This must always be the first argument supplied
14+
#' to \code{LINPRED}.
15+
#' @param link A link function that will be applied to the
1616
#' left-hand-side (the response) in the final linear predictor. Default is none.
1717
#' @param coefPrefix All model coefficient names will begin with this prefix.
18-
#' default is beta_ (so x becomes beta_x, etc.)
18+
#' default is \code{"beta_"} (so 'x' becomes 'beta_x', etc.)
1919
#' @param sdPrefix All dispersion parameters will begin with this prefix.
20-
#' default is no prefix.
21-
#' @param priorSpecs Prior specifications, should be generated with setPrior()
22-
#' @param modMatNames Logical, should parameters be named so they match the
23-
#' names you would get from R's model.matrix function?
24-
#' @param noncenter Logical; use noncentered parameterization?
25-
#' @param centerVar Grouping covariate to 'center' on in parameterization. By
26-
#' default all random effects have mean 0 as with lme4.
20+
#' Default is no prefix.
21+
#' @param priorSpecs Prior specifications, generated with \code{setPrior()}
22+
#' @param modMatNames Logical indicating if parameters should be named so they match the
23+
#' names one would get from R's \code{model.matrix}. Default is \code{FALSE}.
24+
#' @param noncenter Logical indicating whether to use noncentered parameterization.
25+
#' Default is \code{FALSE}.
26+
#' @param centerVar Grouping variable (covariate) to 'center' the random effects on. By
27+
#' default all random effects have mean 0 as with \code{lme4}.
2728
#'
2829
#' @author Ken Kellner
2930
#'
@@ -33,7 +34,7 @@
3334
#' mu[1:3] <- LINPRED(~x + x2)
3435
#' })
3536
#'
36-
#' mod <- nimbleModel(code, constants=constants)
37+
#' mod <- nimbleModel(code, constants = constants)
3738
#' mod$getCode()
3839
NULL
3940

@@ -92,29 +93,31 @@ unpackArgs=TRUE
9293
)
9394

9495

95-
#' Macro to build code for priors on a linear predictor from R formula
96+
#' Macro to build code for priors on a linear predictor from an R formula
9697
#'
9798
#' Generates appropriate priors for a linear predictor derived from an
9899
#' R formula. As such it makes the most sense to use this macro together with
99-
#' the LINPRED macro which takes similar arguments.
100+
#' the LINPRED macro, which takes similar arguments.
100101
#'
101102
#' @name LINPRED_PRIORS
102103
#' @author Ken Kellner
103104
#'
104-
#' @param formula An R formula The formula must be right-hand side only (e.g. ~x).
105-
#' This must always be the first argument supplied to LINPRED_PRIORS
105+
#' @param formula An R formula The formula must be right-hand side only (e.g., \code{~x}).
106+
#' This must always be the first argument supplied to \code{LINPRED_PRIORS}.
106107
#' @param coefPrefix All model coefficient names will begin with this prefix.
107-
#' default is beta_ (so x becomes beta_x, etc.)
108+
#' default is \code{"beta_"} (so 'x' becomes 'beta_x', etc.)
108109
#' @param sdPrefix All dispersion parameters will begin with this prefix.
109-
#' default is no prefix.
110-
#' @param priorSpecs List of prior specifications, should be generated using
110+
#' Default is no prefix.
111+
#' @param priorSpecs List of prior specifications, generated using \code{setPriors}.
111112
#' setPriors()
112-
#' @param modMatNames Logical, should parameters be named so they match the
113-
#' names you would get from R's model.matrix function?
114-
#' @param noncenter Logical, use noncentered parameterization?
115-
#' @param centerVar Grouping covariate to 'center' on in parameterization. By
116-
#' default all random effects have mean 0 as with lme4.
113+
#' @param modMatNames Logical indicating if parameters should be named so they match the
114+
#' names one would get from R's \code{model.matrix}. Default is \code{FALSE}.
115+
#' @param noncenter Logical indicating whether to use noncentered parameterization.
116+
#' Default is \code{FALSE}.
117+
#' @param centerVar Grouping variable (covariate) to 'center' the random effects on. By
118+
#' default all random effects have mean 0 as with \code{lme4}.
117119
#'
120+
#'
118121
#' @author Ken Kellner
119122
#'
120123
#' @examples
@@ -1378,12 +1381,12 @@ correlatedRandomPrior <- function(x, priorSpecs, sdPrefix, sd_name, modelInfo, c
13781381
#' uppertri_mult_diag
13791382
#'
13801383
#' nimbleFunction needed when fitting correlated random effects.
1381-
#' Generates upper triangular Cholesky factor of covariance matrix (U in code)
1382-
#' from upper tri Cholesky factor of correlation matrix (Ustar in code)
1384+
#' Generates upper triangular Cholesky factor of covariance matrix ("U" in code)
1385+
#' from upper triangular Cholesky factor of correlation matrix ("Ustar" in code)
13831386
#' and vector of standard deviations. Taken from the NIMBLE manual,
1384-
#' section 5.2.4.1.2 LKJ distribution for correlation matrices.
1387+
#' section 5.2.4.1.2 (LKJ Distribution for Correlation Matrices).
13851388
#'
1386-
#' @param mat upper triangular Cholesky factor of correlation matrix (Ustar)
1389+
#' @param mat upper triangular Cholesky factor of correlation matrix ("Ustar")
13871390
#' @param vec vector of standard deviations for individual random effects
13881391
#'
13891392
#' @name uppertri_mult_diag

R/LM.R

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' Macro for fitting linear models, GLMs, and GLMMs
22
#'
33
#' This macro generates code for LMs, GLMs, and GLMMs using formula notation
4-
#' and arguments similar to R functions such as lm(), glm(), and lmer()/glmer().
4+
#' and arguments similar to R functions such as \code{lm()}, \code{glm()},
5+
#' and \code{lmer()}/\code{glmer()}.
56
#' Currently only normal, Poisson, and binomial models are supported.
67
#'
78
#' @name LM
@@ -10,19 +11,19 @@
1011
#' @param formula An R formula, possibly with the parameters followed by
1112
#' brackets containing indices. If there are no indices, the macro attempts
1213
#' to guess the correct indices from the context. Formulas can include
13-
#' random effects via lme4-style notation (e.g. ~ x + (1|group))
14+
#' random effects via lme4-style notation (e.g., \code{~ x + (1|group)})
1415
#' @param family A description of the error distribution and link function to
1516
#' be used in the model. This can be a character string naming a family function,
16-
#' a family function or the result of a call to a family function. See ?family.
17-
#' Supported families are gaussian (default), binomial, and poisson.
18-
#' @param coefPrefix All model coefficient names will begin with this prefix.
19-
#' default is beta_ (so x becomes beta_x, etc.)
20-
#' @param sdPrefix All dispersion parameters will begin with this prefix.
17+
#' a family function or the result of a call to a family function. See \code{?family}.
18+
#' Supported families are \code{gaussian} (default), \code{binomial}, and \code{poisson}.
19+
#' @param coefPrefix Character. All model coefficient names will begin with this prefix.
20+
#' default is \code{"beta_"} (so 'x' becomes 'beta_x', etc.)
21+
#' @param sdPrefix Character. All dispersion parameters will begin with this prefix.
2122
#' default is no prefix.
22-
#' @param priorSpecs List of prior specifications, should be generated using
23-
#' setPriors()
24-
#' @param modMatNames Logical, should parameters be named so they match the
25-
#' names you would get from R's model.matrix function?
23+
#' @param priorSpecs List of prior specifications, generated using
24+
#' \code{setPriors()}.
25+
#' @param modMatNames Logical indicating if parameters be named so they match the
26+
#' names one would get from R's \code{model.matrix}.
2627
#'
2728
#' @author Ken Kellner
2829
#'
@@ -35,7 +36,7 @@
3536
#' LM(y ~ x + x2)
3637
#' })
3738
#'
38-
#' mod <- nimbleModel(code, constants=constants)
39+
#' mod <- nimbleModel(code, constants = constants)
3940
#' mod$getCode()
4041
NULL
4142

@@ -81,7 +82,7 @@ LM <- list(process = function(code, modelInfo, .env){
8182
# Check that LHS is in the constants, otherwise this won't work
8283
LHS_name <- safeDeparse(LHS)
8384
if(! LHS_name %in% names(modelInfo$constants)){
84-
stop(paste0("If you don't provide dimensions for the data ", LHS_name,
85+
stop(paste0("LM: If you don't provide dimensions for the data ", LHS_name,
8586
", you must include ", LHS_name, " in the constants instead."), call.=FALSE)
8687
}
8788
idx <- substitute(1:LEN, list(LEN=as.numeric(length(modelInfo$constants[[LHS_name]]))))
@@ -145,10 +146,10 @@ processFamily <- function(fam){
145146
}
146147

147148
if(is.null(fam$family)){
148-
stop("'family' not recognized")
149+
stop("processFamily: 'family' not recognized: ", fam, call. = FALSE)
149150
}
150151
if(!fam$link %in% c("log", "identity","logit","probit","cloglog")){
151-
stop("Unsupported link function", call.=FALSE)
152+
stop("processFamily: Unsupported link function: ", fam$link, call.=FALSE)
152153
}
153154
fam
154155
}
@@ -167,7 +168,7 @@ getDataDistCode <- function(family, response, idx, dispParName){
167168
out <- substitute(DAT ~ FORLOOP(DIST(mu_[IDX], size=N)),
168169
list(DAT=response, DIST=quote(dbinom), N=dispParName, IDX=idx))
169170
} else {
170-
stop("Family not supported", call.=FALSE)
171+
stop("getDataDistCode: Family not supported: ", family, call.=FALSE)
171172
}
172173
out
173174
}

R/setPriors.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
#' Set up prior values for different categories of nodes
22
#'
33
#' Generates a list of custom specifications of priors for parameters in the model.
4-
#' It is possible to set priors for a category of parameters (e.g. intercept,
4+
#' It is possible to set priors for a category of parameters (e.g., intercept,
55
#' coefficient, sd, factor, continuous) or to set a prior for a specific
66
#' parameter name (optionally including brackets with indices).
7-
#' Exact name matches including brackets/indices are used first, followed by
8-
#' name matches without indices, followed by data type (factor/continuous)
9-
#' followed by parameter type (intercept/coefficient/sd).
10-
#' Arguments can be supplied as quoted code, a character string, or
11-
#' as a list of prior components. For example, to specify the prior
12-
#' \code{dnorm(0, sd = 10)} you could specify \code{quote(dnorm(0, sd = 10))},
13-
#' or \code{"dnorm(0, sd = 10)"}, or \code{list("dnorm", 0, sd = 10)}.
147
#'
158
#' @name setPriors
169
#' @author Ken Kellner
@@ -28,6 +21,16 @@
2821
#'
2922
#' @seealso [nimble::dlkj_corr_cholesky] for more on the LKJ distribution
3023
#'
24+
#' @details
25+
#'
26+
#' Exact name matches including brackets/indices are used first, followed by
27+
#' name matches without indices, followed by data type (factor/continuous)
28+
#' followed by parameter type (intercept/coefficient/sd).
29+
#' Arguments can be supplied as quoted code, a character string, or
30+
#' as a list of prior components. For example, to specify the prior
31+
#' \code{dnorm(0, sd = 10)} you could specify \code{quote(dnorm(0, sd = 10))},
32+
#' or \code{"dnorm(0, sd = 10)"}, or \code{list("dnorm", 0, sd = 10)}.
33+
#'
3134
#' @author Ken Kellner
3235
#'
3336
#' @examples
@@ -46,7 +49,7 @@ setPriors <- function(intercept = quote(dnorm(0, sd = 1000)),
4649
sd = quote(dunif(0, 100)),
4750
factor = NULL,
4851
continuous = NULL,
49-
eta = 1.3,
52+
eta = 1,
5053
...){
5154
# Get specific prior names
5255
extra <- list(...)
@@ -57,7 +60,7 @@ setPriors <- function(intercept = quote(dnorm(0, sd = 1000)),
5760

5861
# Check if any outputs are numeric (indicating they were passed without quote())
5962
lapply(1:length(out), function(i) if(is.numeric(out[[i]])){
60-
stop("Prior code for ", names(out)[i], " should be wrapped in quote()", call.=FALSE)
63+
stop("setPriors: Prior code for ", names(out)[i], " should be wrapped in quote()", call.=FALSE)
6164
})
6265

6366
# Remove any null values
@@ -82,7 +85,7 @@ setPriors <- function(intercept = quote(dnorm(0, sd = 1000)),
8285
convertListToPrior <- function(input){
8386
if(!is.list(input)) return(input)
8487
if(is.function(input[[1]])){
85-
stop("Function name must be a character string or wrapped in quote()", call.=FALSE)
88+
stop("convertListToPrior: Function name must be a character string or wrapped in quote()", call.=FALSE)
8689
}
8790

8891
out <- input
@@ -101,7 +104,7 @@ removeBracket <- function(node){
101104
#' Match a prior from a list of prior settings
102105
#'
103106
#' Attempts to determine which prior to put on a parameter based on a list of settings,
104-
#' such as the output from setPriors(). The function follows the following
107+
#' such as the output from \code{setPriors()}. The function follows the following
105108
#' search pattern: (1) looks for an exact match to the parameter name including brackets;
106109
#' (2) a match to the parameter name without brackets; (3) goes through each value
107110
#' supplied to \code{...} in order and looks for a match in the names of the
@@ -116,7 +119,7 @@ removeBracket <- function(node){
116119
#' @param ... Character strings that categorize the parameter and match
117120
#' the names of elements in \code{priorSpecs}. The order is important:
118121
#' the first match found is used.
119-
#' @param priorSpecs A named list of prior settings, e.g. as generated by
122+
#' @param priorSpecs A named list of prior settings, e.g., as generated by
120123
#' \code{setPriors}
121124
#'
122125
#' @author Ken Kellner
@@ -161,10 +164,10 @@ matchPrior <- function(parName, ..., priorSpecs){
161164
return(priorSpecs[[i]])
162165
}
163166
}
164-
stop("Unable to set prior for parameter ", parName, call.=FALSE)
167+
stop("matchPrior: Unable to set prior for parameter: ", parName, call.=FALSE)
165168
}
166169

167170
checkValidPrior <- function(prior){
168-
if(!(is.call(prior) | is.name(prior))) stop("Invalid prior ", prior, call.=FALSE)
171+
if(!(is.call(prior) | is.name(prior))) stop("Invalid prior: ", prior, call.=FALSE)
169172
invisible()
170173
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
status](https://github.com/nimble-dev/nimbleMacros/workflows/R-CMD-check/badge.svg)](https://github.com/nimble-dev/nimbleMacros/actions)
55

66
`nimbleMacros` is an R package that extends [NIMBLE](https://r-nimble.org/), an R package for hierarchical statistical modeling.
7-
The package provides a set of model macros, which are special code chunks which NIMBLE automatically expands into larger blocks of valid NIMBLE model code.
7+
The package provides a set of model macros, which are special code chunks that NIMBLE automatically expands into larger blocks of valid NIMBLE model code.
88

99
For example, the following NIMBLE model code contains a macro called `LINPRED`, and the objective is to create a linear predictor containing an intercept and a slope for one covariate, `x`.
1010

0 commit comments

Comments
 (0)