|
1 | 1 | 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) |
3 | 3 | { |
4 | 4 | ## 'sims.array' is supposed to be a 3-way array with |
5 | 5 | # n.sims*n.chains*n.parameters simulations, and |
@@ -59,10 +59,9 @@ as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags", |
59 | 59 | ## this check fails if you take out a part of the simulations |
60 | 60 | ## (for example, you don't want the array to have some of the |
61 | 61 | ## 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 | + } |
66 | 65 | indexes.short[[j]] <- as.list(numeric(length.short[j])) |
67 | 66 | for (k in 1:length.short[j]){ |
68 | 67 | 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", |
137 | 136 | ##fix this list |
138 | 137 | #sims.list[[j]] <- aperm(array(sims[, long.short[[j]]], c(n.sims, rev(n.indexes.short[[j]]))), c(1, (dimension.short[j] + 1):2)) |
139 | 138 | 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 | + } |
140 | 150 | #sims.list[[j]] <- sims[, long.short[[j]]] |
141 | 151 | summary.mean[[j]] <- array(summary[long.short[[j]],"mean"],n.indexes.short[[j]]) |
142 | 152 | summary.sd[[j]] <- array(summary[long.short[[j]],"sd"],n.indexes.short[[j]]) |
143 | 153 | summary.median[[j]] <- array(summary[long.short[[j]],"50%"],n.indexes.short[[j]]) |
| 154 | + |
144 | 155 | ##ell: added 025 and 975 |
145 | 156 | # summary.025[[j]] <- array(summary[long.short[[j]],"2.5%"],n.indexes.short[[j]]) |
146 | 157 | # summary.975[[j]] <- array(summary[long.short[[j]],"97.5%"],n.indexes.short[[j]]) |
| 158 | + |
147 | 159 | } |
148 | 160 | } |
149 | 161 |
|
@@ -188,3 +200,34 @@ as.bugs.array2 <- function(sims.array, model.file=NULL, program="jags", |
188 | 200 | class(all) <- "bugs" |
189 | 201 | all |
190 | 202 | } |
| 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 | + |
0 commit comments