Skip to content

Commit 1cbe00c

Browse files
suyusungcran-robot
authored andcommitted
version 0.8-5
1 parent 8fe4f72 commit 1cbe00c

File tree

11 files changed

+112
-43
lines changed

11 files changed

+112
-43
lines changed

CHANGELOG

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2024-05-4 Yu-Sung Su <[email protected]>
2+
* DESCRIPTION (Version, Date): 0.8-5
3+
* incorporate patches suggested by CRAN
4+
* R/jags: add new parameter checkMissing = FALSE
5+
* R/mcmc2bugs: add new parameter checkMissing = FALSE
6+
* R/as.bugs.array2: add a patch to cases where levels of categorical variables have missing.
7+
1. add new parameter checkMissing = FALSE, When set to TRUE, this option will verify the
8+
presence of missing data within the parameters, typically focusing on categorical
9+
variables or levels within Multilevel Models (MLM). If missing data is detected,
10+
the function will attempt to return a sim.list object containing NA values to
11+
indicate the missingness. It is generally advised against enabling this option.
12+
Instead, it is recommended to ensure that all variables provided to the jags()
13+
function are free from missing data before proceeding with the analysis.
14+
2. it will now spell out warning message when categorical variables with missing data, users
15+
should be careful about the output results.
16+
* NAMESPACE: fix issues between Author and Authro@R
17+
118
2021-08-05 Yu-Sung Su <[email protected]>
219
* fix various issues suggested by Will Landau
320

DESCRIPTION

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
Package: R2jags
2-
Version: 0.7-1.1
3-
Date: 2021-08-05
2+
Version: 0.8-5
3+
Date: 2024-05-01
44
Title: Using R to Run 'JAGS'
5-
Author: Yu-Sung Su <[email protected]>,
6-
Masanao Yajima <[email protected]>,
5+
Authors@R: c(person("Yu-Sung", "Su", role = c("aut", "cre"),
6+
email = "[email protected]",
7+
comment = c(ORCID = "0000-0001-5021-8209")),
8+
person("Masanao", "Yajima", email = "[email protected]", role = "aut")
9+
)
10+
Author: Yu-Sung Su [aut, cre] (<https://orcid.org/0000-0001-5021-8209>),
11+
Masanao Yajima [aut]
712
Maintainer: Yu-Sung Su <[email protected]>
813
BugReports: https://github.com/suyusung/R2jags/issues/
914
Depends: R (>= 2.14.0), rjags (>= 3-3)
15+
Suggests: testthat (>= 3.0.0)
1016
Imports: abind, coda (>= 0.13), graphics, grDevices, methods,
11-
R2WinBUGS, parallel, stats, utils
17+
R2WinBUGS, parallel, stats, stringr, utils
1218
SystemRequirements: JAGS (http://mcmc-jags.sourceforge.net)
1319
Description: Providing wrapper functions to implement Bayesian analysis in JAGS. Some major features include monitoring convergence of a MCMC model using Rubin and Gelman Rhat statistics, automatically running a MCMC model till it converges, and implementing parallel processing of a MCMC model for multiple chains.
1420
License: GPL (> 2)
15-
RoxygenNote: 7.1.1
16-
Suggests: testthat (>= 3.0.0)
17-
Config/testthat/edition: 3
1821
NeedsCompilation: no
19-
Packaged: 2024-02-04 20:09:16 UTC; ripley
22+
Packaged: 2024-05-04 11:43:21 UTC; yusung
2023
Repository: CRAN
21-
Date/Publication: 2024-02-05 08:15:27 UTC
24+
Date/Publication: 2024-05-04 12:50:02 UTC

MD5

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
225db27ff5043d97292791af501fc127 *CHANGELOG
2-
703d08b1d4460a53e3463b70c14f6bfe *DESCRIPTION
3-
7756418df5cb9f5eddb9cedde0f2fb11 *NAMESPACE
1+
d1e90403a82fc2133b09badb579175ad *CHANGELOG
2+
7712c12873b0c06d264b4cf8c7613d05 *DESCRIPTION
3+
3a5facc9680703fabf76cc9317eb3367 *NAMESPACE
44
0993ec3c619611d40927f6a4b24f9f91 *R/AllClass.R
5-
65e8671c8fbf9de2f49416201f041bfa *R/as.bugs.array2.R
6-
3d051481132ba73a38f117e0a544842c *R/as.mcmc.R
5+
de6ba4efcaa658ade6f69483f63fdc50 *R/as.bugs.array2.R
6+
277a4853017c68d07ba6817bd20381cc *R/as.mcmc.R
77
9e6b25427b6c52ebdc74e9c431abc55c *R/attach.jags.R
88
46e343f5bcfbc633064ada1a8933f78a *R/autojags.R
9-
67ee6f9913a0372002b9db3d9d3b35ee *R/jags.R
10-
dd0789cc2d85e9fe05c75190888f6cac *R/jags.sims.R
9+
ff6d52ce77e326d305a5d291a73f277a *R/jags.R
10+
7cb48206684061510a25d7976411ed22 *R/jags.sims.R
1111
a11f0bc0296d1e57fe6b827e97f24a86 *R/jags2.R
1212
4b1abee38647e8af6636c71f13cd5510 *R/jags2bugs.R
1313
290c44ab85aca43cfc2d56504af84f86 *R/jagsParallel.R
14-
f84f5317262e71e94270bf600d217b63 *R/mcmc2bugs.R
14+
60bfe7aa49a5fb0689c84c8f00e2640a *R/mcmc2bugs.R
1515
5f45c187679ee5855226de41e1618b12 *R/plot.R
1616
c1ff7891fcb484cf531c2dc205a67fb2 *R/print.R
1717
256ff74ae7f2d740d75ed706a615ba30 *R/recompile.R
1818
3843b38a6c53f3c4eec045cbf087dd20 *R/traceplot.R
1919
d8a7cc60b636be2563414d771da07dfd *R/update.R
2020
c228c7d0dc1a92b3a0f3a16e16669e52 *R/util.R
21+
a3c507e13de0fc31d125d233b347ce2d *README.md
2122
f0f5ffb1058850994fc0628460844805 *inst/model/schools.txt
2223
cb5c8a434518b884d450c2867320976c *man/attach.jags.Rd
2324
f7d2806381459966d900d31a46df8e42 *man/autojags.Rd
24-
b950cd92225f415bdeea57e003ec9d10 *man/jags.Rd
25+
68fe2149fef3b97fc34fd8ca04b7b190 *man/jags.Rd
2526
aa6c6fcdab566cc3a189ba07ff5a8423 *man/jags2bugs.Rd
2627
dd7972715f34b0925c6895109da53a2d *man/recompile.Rd
2728
3182b61e64111486844ec8c8f12bdf1e *man/traceplot.Rd

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ importFrom(abind, "abind")
1616

1717
importFrom(coda, "as.mcmc", "mcmc", "nvar", "varnames", "nchain", "as.mcmc.list", "traceplot")
1818

19+
importFrom(stringr, "str_match")
20+
1921
import(parallel)
2022

2123
importFrom(R2WinBUGS, "attach.bugs",

R/as.bugs.array2.R

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags",
2-
DIC=FALSE, DICOutput=NULL, n.iter=NULL, n.burnin=0, n.thin=1)
2+
DIC=FALSE, DICOutput=NULL, n.iter=NULL, n.burnin=0, n.thin=1, checkMissing=FALSE)
33
{
44
## 'sims.array' is supposed to be a 3-way array with
55
# n.sims*n.chains*n.parameters simulations, and
@@ -59,10 +59,9 @@ as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags",
5959
## this check fails if you take out a part of the simulations
6060
## (for example, you don't want the array to have some of the
6161
## parameters) so I took them out.
62-
63-
## if (length(long.short[[j]]) != length.short[j])
64-
## stop(paste("error in parameter", root.short[[j]],
65-
## "in parameters.to.save"))
62+
if (length(long.short[[j]]) != length.short[j]){
63+
warning(paste("error/missing in parameter", root.short[[j]],"in parameters.to.save, \n Be aware of the output results.\n"), noBreaks.=TRUE)
64+
}
6665
indexes.short[[j]] <- as.list(numeric(length.short[j]))
6766
for (k in 1:length.short[j]){
6867
indexes.short[[j]][[k]] <- indexes.long[[long.short[[j]][k]]]
@@ -137,13 +136,26 @@ as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags",
137136
##fix this list
138137
#sims.list[[j]] <- aperm(array(sims[, long.short[[j]]], c(n.sims, rev(n.indexes.short[[j]]))), c(1, (dimension.short[j] + 1):2))
139138
sims.list[[j]] <- array(sims[, long.short[[j]]], c(n.sims, n.indexes.short[[j]]))
139+
140+
# this is a quick fix to the case where elements in levels are missing, ie empty cells in a parameter matrix 2024.3.31
141+
# the code is ugly. need to fix in the future. Add two hiddent functions below in the end of the page
142+
if(checkMissing){
143+
missingCell <- .checkEmptyCell(n.indexes.short[[j]], long.short[[j]], parameter.names)
144+
if(length(missingCell)>1){
145+
for(s in 1:n.sims){
146+
sims.list[[j]][s,,][missingCell] <- NA
147+
}
148+
}
149+
}
140150
#sims.list[[j]] <- sims[, long.short[[j]]]
141151
summary.mean[[j]] <- array(summary[long.short[[j]],"mean"],n.indexes.short[[j]])
142152
summary.sd[[j]] <- array(summary[long.short[[j]],"sd"],n.indexes.short[[j]])
143153
summary.median[[j]] <- array(summary[long.short[[j]],"50%"],n.indexes.short[[j]])
154+
144155
##ell: added 025 and 975
145156
# summary.025[[j]] <- array(summary[long.short[[j]],"2.5%"],n.indexes.short[[j]])
146157
# summary.975[[j]] <- array(summary[long.short[[j]],"97.5%"],n.indexes.short[[j]])
158+
147159
}
148160
}
149161

@@ -188,3 +200,34 @@ as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags",
188200
class(all) <- "bugs"
189201
all
190202
}
203+
204+
205+
.checkEmptyCell <- function(n.indexes.short, long.short, parameter.names){
206+
size1 <- prod(n.indexes.short)
207+
size2 <- length(long.short)
208+
if(size1 > size2){
209+
paraNames <- parameter.names[long.short]
210+
indicesLst <- lapply(paraNames, .extract_indices)
211+
indices <- do.call(rbind, indicesLst)
212+
paramMatrix <- matrix(FALSE, nrow = n.indexes.short[1], ncol = n.indexes.short[2])
213+
paramMatrix[indices] <- TRUE
214+
missingCell <- which(!paramMatrix, arr.ind = TRUE)
215+
}else{
216+
missingCell <- NA
217+
}
218+
return(missingCell)
219+
}
220+
221+
.extract_indices <- function(parameter.names) {
222+
#indices <- gregexpr("\\d+", parameter.names)[[1]]
223+
#indices <- as.numeric(indices)
224+
#indices <- gsub("\\d+", "", parameter.names) # Extract only digits
225+
#indices <- as.numeric(indices)) # Split into individual digits and convert to numeric
226+
indices <- str_match(parameter.names, "\\[(\\d+),(\\d+)\\]")[,-1]
227+
indices <- matrix(as.numeric(indices), ncol = 2, byrow = TRUE)
228+
#indices <- as.numeric(indices)
229+
return(indices)
230+
}
231+
232+
233+

R/as.mcmc.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ as.mcmc.rjags <- function (x, ...) {
44
x <- x$BUGSoutput
55
mclist <- vector("list", x$n.chains)
66
mclis <- vector("list", x$n.chains)
7-
strt <- x$n.burnin + 1
7+
strt <- x$n.burnin + x$n.thin
88
end <- x$n.iter
99
ord <- order(dimnames(x$sims.array)[[3]])
1010
for (i in 1:x$n.chains) {

R/jags.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ jags <- function( data, inits,
1313
digits = 5,
1414
RNGname = c("Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister"),
1515
jags.module = c("glm","dic"),
16-
quiet = FALSE
16+
quiet = FALSE,
17+
checkMissing = FALSE
1718
)
1819
{
1920
#require( rjags )
@@ -166,15 +167,15 @@ jags <- function( data, inits,
166167
thin = n.thin,
167168
by = refresh,
168169
progress.bar = progress.bar )
169-
170170
fit <- mcmc2bugs( samples,
171171
model.file = model.file,
172172
program = "jags",
173173
DIC = DIC,
174174
DICOutput = NULL,
175175
n.iter = n.iter,
176176
n.burnin = n.burnin,
177-
n.thin = n.thin )
177+
n.thin = n.thin,
178+
checkMissing = checkMissing )
178179

179180
out <- list( model = m,
180181
BUGSoutput = fit,

R/jags.sims.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323

2424

25-
jags.sims <- function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin,
26-
DIC = TRUE)
25+
jags.sims <- function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC = TRUE)
2726
{
2827

2928
#require(R2WinBUGS)
@@ -46,8 +45,7 @@ jags.sims <- function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin,
4645
left.bracket.short <- as.vector(regexpr("[[]", parameters.to.save))
4746
right.bracket.short <- as.vector(regexpr("[]]", parameters.to.save))
4847
root.short <- ifelse(left.bracket.short == -1, parameters.to.save,
49-
substring(parameters.to.save, 1, left.bracket.short -
50-
1))
48+
substring(parameters.to.save, 1, left.bracket.short - 1))
5149
dimension.short <- rep(0, n.roots)
5250
indexes.short <- vector(n.roots, mode = "list")
5351
n.indexes.short <- vector(n.roots, mode = "list")
@@ -107,8 +105,7 @@ jags.sims <- function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin,
107105
}
108106
}
109107
sims <- sims[sample(n.sims), , drop = FALSE]
110-
sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots,
111-
mode = "list")
108+
sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots,mode = "list")
112109
names(sims.list) <- names(summary.mean) <- names(summary.sd) <- names(summary.median) <- root.short
113110
for (j in 1:n.roots) {
114111
if (length.short[j] == 1) {

R/mcmc2bugs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
mcmc2bugs <- function(x, model.file = NULL, program = "", DIC = FALSE,
3-
DICOutput = NULL, n.iter = NULL, n.burnin = 0, n.thin = 1){
3+
DICOutput = NULL, n.iter = NULL, n.burnin = 0, n.thin = 1, checkMissing=FALSE){
44
parameter.names <- dimnames(x[[1]])[[2]]
55
n.keeps <- dim(x[[1]])[1]
66
n.chains <- summary(x)[["nchain"]]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# R2jags
2+
R2jags: Using R to Run 'JAGS'

0 commit comments

Comments
 (0)