Skip to content

Commit af18244

Browse files
parameterTransform handles multivariate user dists, with a warning (#1532)
* parameterTransform handles multivariate user dists, with a warning * Fix boolean operator. * Rework new messages with verbosity system. * Fix missing commas in warnings. --------- Co-authored-by: Christopher Paciorek <paciorek@stat.berkeley.edu>
1 parent 8072fe3 commit af18244

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

packages/nimble/R/options.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ nimOptimMethod("bobyqa",
215215
MCMCwarnUnsampledStochasticNodes = TRUE,
216216
MCMCRJcheckHyperparam = TRUE,
217217
MCMCenableWAIC = FALSE,
218-
MCMCuseBarkerAsDefaultMV = FALSE,
218+
MCMCuseBarkerAsDefaultMV = FALSE,
219+
parameterTransformWarnUserDists = TRUE,
219220
useClearCompiledInADTesting = TRUE,
220221
unsupportedDerivativeHandling = 'error', # default is error, other options are 'warn' and 'ignore'. Handled in updateADproxyModelMethods in cppDefs_nimbleFunction.R
221222
errorIfMissingNFVariable = TRUE,

packages/nimble/R/parameterTransform.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ parameterTransform <- nimbleFunction(
9797
## 3: scalar interval-constrained (0, 1)
9898
## 4: scalar semi-interval (-Inf, b) or (a, Inf)
9999
## 5: scalar interval-constrained (a, b)
100-
## 6: multivariate {normal, t, CAR}
100+
## 6: multivariate {normal, t, CAR} and multivariate user-defined distributions
101101
## 7: multivariate {wishart, inverse-wishart}
102102
## 8: multivariate dirichlet
103103
## 9: LKJ
@@ -171,15 +171,27 @@ parameterTransform <- nimbleFunction(
171171
if(length(all.vars(lowerBdExpr)) > 0) stop('Node ', node, ' appears to have a non-constant lower bound, which cannot be used in parameterTransform.')
172172
if(length(all.vars(upperBdExpr)) > 0) stop('Node ', node, ' appears to have a non-constant upper bound, which cannot be used in parameterTransform.')
173173
} else { ## some other distribution with finite support
174-
message(' [Warning] `parameterTransform` system cannot process the ', dist, ' distribution of node ', node, '.\n The upper and lower bounds of the ', dist, ' distribution must be constant.\n If you\'re uncertain about this, please get in touch with the NIMBLE development team.')
174+
messageIfVerbose(' [Warning] `parameterTransform` system cannot process the `', dist, '`\n',
175+
' distribution for node `', node, '. The upper and lower bounds\n',
176+
' of `', dist, '` must be constant.\n')
175177
}
176178
transformType[i] <- 5L
177179
transformData[i,DATA1] <- bounds[1] ## formerly lowerBound
178180
transformData[i,DATA2] <- bounds[2] - bounds[1] ## formerly range
179181
next }
180-
stop(paste0('`parameterTransform` system doesn\'t have a transformation for the bounds of node: ', node, ', which are (', bounds[1], ', ', bounds[2], ')'))
182+
stop('`parameterTransform` system doesn\'t have a transformation for the bounds of node: ', node, ', which are (', bounds[1], ', ', bounds[2], ')')
181183
} else { ## multivariate
182-
if(dist %in% c('dmnorm', 'dmvt', 'dcar_normal', 'dcar_proper')) { ## 6: multivariate {normal, t, CAR}; also set for non-scalar determ nodes when allowDeterm is TRUE
184+
if(dist %in% c('dmnorm', 'dmvt', 'dcar_normal', 'dcar_proper') || ## 6: multivariate {normal, t, CAR},
185+
isUserDefined(dist)) ## all multivariate user-defined distributions,
186+
{ ## and non-scalar determ nodes when allowDeterm is TRUE
187+
if(isUserDefined(dist) && getNimbleOption('parameterTransformWarnUserDists'))
188+
messageIfVerbose(
189+
' [Warning] `parameterTransform` system detected multivariate user-defined\n',
190+
' distribution `', dist, '`. No transformation will be applied\n',
191+
' to any dimension of the `x` values of `', dist, '`.\n',
192+
' If some values of `x` are not valid, you may encounter errors.\n',
193+
' This warning can be disabled using\n',
194+
' `nimbleOptions(parameterTransformWarnUserDists = FALSE)`.')
183195
transformType[i] <- 6L
184196
d <- length(model$expandNodeNames(node, returnScalarComponents = TRUE))
185197
transformData[i,NIND2] <- transformData[i,NIND1] + d - 1
@@ -211,7 +223,7 @@ parameterTransform <- nimbleFunction(
211223
transformData[i,DATA1] <- d
212224
transformData[i,DATA2] <- p
213225
next }
214-
stop(paste0('parameterTransform doesn\'t handle \'', dist, '\' distributions.'), call. = FALSE)
226+
stop('parameterTransform doesn\'t handle \'', dist, '\' distributions.', call. = FALSE)
215227
}
216228
}
217229
if(nNodes == 0) {

0 commit comments

Comments
 (0)