Skip to content

Commit cde9642

Browse files
authored
PG simple update (#1569)
* Minor updates to PG Sampler 1. Fixed when Design Matrix is initialized 2. Small bug of an empty inflationNodes 3. Added a warning for when infinite design matrix values are constructed. * Annoying I forgot to actually switch this one. * Should be good. * Inflation nodes fix -when inflation nodes are computed (outside of check) -error out if an infinite design matrix value is found on the first round.
1 parent 0afa439 commit cde9642

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

packages/nimble/R/MCMC_samplers.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,20 +2877,21 @@ sampler_polyagamma <- nimbleFunction(
28772877
sizeNodes <- setdiff(probAndSizeNodes, probNodes)
28782878

28792879
zeroInflated <- FALSE
2880-
2880+
## Zero-Inflation node detection:
2881+
inflationNodes <- model$getParents(probNodes, omit = c(target, nonTarget), stochOnly = TRUE)
2882+
if(length(inflationNodes)) inflationNodes <- setdiff(inflationNodes, nonTarget)
2883+
28812884
## Conjugacy checking, part 2.
28822885
## Make sure any stochastic dependencies between target and y are Bernoulli (i.e. only zero-inflation allowed)
28832886
## and that zero-inflation variable multiplies the baseline probability.
28842887

28852888
## First we need some processing to make sure that we can simply check inflation based only on `probNodes[1]`,
28862889
## to avoid costly checking.
28872890
if(check) {
2888-
inflationNodes <- model$getParents(probNodes, omit = c(target, nonTarget), stochOnly = TRUE)
28892891
if(length(inflationNodes)) {
28902892
## Check that inflation probabilities are directly specified as parents of `probNodes`
28912893
## to avoid having to check multiple declarations. Seemingly anything otherwise would be
28922894
## an unusual zero inflation construction.
2893-
inflationNodes <- setdiff(inflationNodes, nonTarget)
28942895
test <- model$getParents(probNodes, omit = c(target, nonTarget), stochOnly = TRUE, immediateOnly = TRUE)
28952896
test <- setdiff(test, nonTarget)
28962897
if(!identical(test, inflationNodes)) # So we need to only consider a single declaration.
@@ -3020,6 +3021,8 @@ sampler_polyagamma <- nimbleFunction(
30203021
}
30213022
if(all(fixedColumns))
30223023
fixed <- TRUE
3024+
3025+
initializeX <- TRUE ## Initialize Design Matrix on first run
30233026
} else {
30243027
X <- control$designMatrix
30253028
if(ncol(X) != nCoef)
@@ -3028,10 +3031,10 @@ sampler_polyagamma <- nimbleFunction(
30283031
stop("polyagamma sampler: number of rows of design matrix, ", nrow(X), ", doesn't match number of Bernoulli observations, ", N)
30293032
fixed <- TRUE
30303033
fixedColumns <- rep(TRUE, nCoef)
3034+
initializeX <- FALSE ## Don't Initialize Design Matrix on first run
30313035
}
30323036

30333037
initializeSize <- TRUE
3034-
initializeX <- TRUE
30353038
pgSampler <- samplePolyaGamma()
30363039

30373040
Q <- matrix(0, nrow = nCoef, ncol = nCoef)
@@ -3172,6 +3175,14 @@ sampler_polyagamma <- nimbleFunction(
31723175
values(model, inflationNodes) <<- inflationValuesSaved
31733176
values(model, inflationNodesDeps) <<- inflationDepsValuesSaved
31743177
}
3178+
## Check to see if covariates need to be scaled:
3179+
if(initializeX) {
3180+
for(j in 1:nCoef){
3181+
if( any(abs(X[, j]) == Inf) ){
3182+
stop("Infinite values constructed in the design matrix of the polyagamma sampler. Please consider scaling the covariate or providing the design matrix.\n")
3183+
}
3184+
}
3185+
}
31753186
nimCopy(from = mvSaved, to = model, row = 1, nodes = target, logProb = FALSE)
31763187
nimCopy(from = mvSaved, to = model, row = 1, nodes = copyNodesDeterm, logProb = FALSE)
31773188
initializeX <<- FALSE

0 commit comments

Comments
 (0)