1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
#
# Copyright 2007-2021 by the individuals mentioned in the source code history
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
setClass(Class = "MxThreshold",
representation = representation(
variable = "character",
nThresh = "numeric",
free = "logical",
values = "numeric",
labels = "character",
lbound = "numeric",
ubound = "numeric"
))
setMethod("initialize", "MxThreshold",
function(.Object, variable, nThresh, free, values, labels, lbound, ubound) {
.Object@variable <- variable
.Object@nThresh <- nThresh
.Object@free <- free
.Object@values <- values
.Object@labels <- labels
.Object@lbound <- lbound
.Object@ubound <- ubound
return(.Object)
}
)
setMethod("$", "MxThreshold", imxExtractSlot)
setReplaceMethod("$", "MxThreshold",
function(x, name, value) {
stop("Changing threshold values directly is not recommended. Please use the mxThreshold() function instead.")
}
)
setMethod("names", "MxThreshold", slotNames)
##' mxNormalQuantiles
##'
##' Get quantiles from a normal distribution
##'
##' @param nBreaks the number of thresholds, or a vector of the number of thresholds
##' @param mean the mean of the underlying normal distribution
##' @param sd the standard deviation of the underlying normal distribution
##' @aliases omxNormalQuantiles
##' @return
##' a vector of quantiles
##' @examples
##' mxNormalQuantiles(3)
##' mxNormalQuantiles(3, mean=7)
##' mxNormalQuantiles(2, mean=1, sd=3)
mxNormalQuantiles <- function(nBreaks, mean=0, sd=1) {
if(length(nBreaks) > 1) {
return(unlist(mapply(mxNormalQuantiles, nBreaks=nBreaks, mean=mean, sd=sd)))
}
if(is.na(nBreaks)) {
return(as.numeric(NA))
}
if(nBreaks < 0) {
stop("Error in mxNormalQuantiles: You must request at least one quantile.")
}
return(qnorm(seq(0,1,length.out=nBreaks+2)[1:nBreaks+1], mean=mean, sd=sd))
}
omxNormalQuantiles <-mxNormalQuantiles
thresholdCheckLengths <- function(nThresh, starts, free, values, labels, lbounds, ubounds, varName) {
if(single.na(starts)) {
starts = rep(0, 5)
}
ends <- matrix(NA, 5, 2)
ends[1,] <- thresholdCheckSingleLength(nThresh, starts[1], length(free), "free/fixed designations", varName)
ends[2,] <- thresholdCheckSingleLength(nThresh, starts[2], length(values), "values", varName)
ends[3,] <- thresholdCheckSingleLength(nThresh, starts[3], length(labels), "labels", varName)
ends[4,] <- thresholdCheckSingleLength(nThresh, starts[4], length(lbounds), "lower bounds", varName)
ends[5,] <- thresholdCheckSingleLength(nThresh, starts[5], length(ubounds), "upper bounds", varName)
ends
}
thresholdCheckSingleLength <- function(nElements, startingPoint, len, lenName, varName) {
if(nElements > len - startingPoint) {
if(startingPoint != 0 && startingPoint %% len != 0) {
stop(paste("This is tricky. You've specified a set of",
lenName, "for the mxThreshold function that does not",
"quite line up. mxThreshold has consumed", startingPoint,
"values from the list of", len, "but needs", nElements, "to complete",
"the list of", lenName, "for", omxQuotes(varName)), call.=FALSE)
}
if(nElements %% len != 0) {
stop(paste("mxThreshold() call will generate",
nElements, "thresholds but you have specified",
len, lenName, "for", omxQuotes(varName)), call. = FALSE)
} else {
startingPoint = 0
}
}
return(c(startingPoint+1, startingPoint + nElements))
}
as.MxMatrix.MxThreshold <- function(threshold) {
mxMatrix("Full", nrow=threshold@nThresh, ncol=1, free=threshold@free, values=threshold@values,
labels=threshold@labels, lbound=threshold@lbound, ubound=threshold@ubound,
dimnames=list(NULL, threshold@variable),condenseSlots=FALSE)
}
thresholdSubset <- function(var, a) {
return(rep(var, length.out=a[2])[a[1]:a[2]])
}
splitThresholds <- function(variables, nThresh, free, values, labels, lbound, ubound) {
nVars <- length(variables)
nThresh <- rep(nThresh, length.out=nVars)
nElements <- sum(nThresh)
thresholds <- as.list(rep(NA, nVars))
starts <- NA
# This block is primarily to catch cases like nThresh=c(2, 4, 2), free=c(TRUE, TRUE, TRUE, FALSE)
# That is, where the wrapping of an input vector doesn't mesh with the threshold count
for(i in 1:nVars) {
idx <- thresholdCheckLengths(nThresh[i], starts, free, values, labels, lbound, ubound, variables[i])
thresholds[[i]] <- new("MxThreshold", variables[i], nThresh[i], thresholdSubset(free, idx[1,]),
as.numeric(thresholdSubset(values, idx[2,])),
as.character(thresholdSubset(labels, idx[3,])),
as.numeric(thresholdSubset(lbound, idx[4,])),
as.numeric(thresholdSubset(ubound, idx[5,])))
starts <- idx[,2]
}
if(length(thresholds) == 1) {
thresholds <- thresholds[[1]]
}
return(thresholds)
}
mxThreshold <- function(vars, nThresh=NA, free=FALSE, values=mxNormalQuantiles(nThresh),
labels=NA, lbound=NA, ubound=NA) {
if(all.na(vars)) {
stop("You must specify a variable name for which these thresholds should be applied.")
}
nVars = length(vars)
if(single.na(nThresh)) {
if(nVars > 0) {
stop(paste("mxThreshold error: You must specify a list of numbers",
" of levels if you specify more than one variable name."))
} else if( all(is.na(values))) {
stop(paste("mxThreshold error: You must specify either values or a",
"number of levels for each variable in order to generate thresholds."))
}
nThresh = length(values)
}
repeats <- setdiff(vars, unique(vars))
if(length(repeats) != 0) {
stop(paste("mxThreshold error: You listed", omxQuotes(repeats),
"more than once in your list of thresholded variables."))
}
nLevelsListed = length(nThresh)
if(nVars < nLevelsListed) {
stop(paste("mxThreshold error: You specified a number of levels for",
nLevelsListed, "variables, but only listed", nVars, "variables."))
}
if(!( nVars == nLevelsListed || nVars %% nLevelsListed==0)) {
stop(paste("mxThreshold error: number of variables listed (",
nVars, ") is not a multiple of the number of level counts (",
nLevelsListed, ")."))
}
splitThresholds(vars, nThresh, free, values, labels, lbound, ubound)
}
mxMarginalProbit <- mxThreshold
generateDataThresholdColumns <- function(covarianceColumnNames, thresholdsMatrix) {
covarianceLength <- length(covarianceColumnNames)
thresholdColumns <- replicate(covarianceLength, as.integer(NA))
thresholdLevels <- replicate(covarianceLength, as.integer(NA))
thresholdColumnNames <- dimnames(thresholdsMatrix)[[2]]
for(i in 1:length(thresholdColumnNames)) {
oneThresholdName <- thresholdColumnNames[[i]]
numThresholds <- sum(!is.na(thresholdsMatrix[,i]))
columnIndex <- match(oneThresholdName, covarianceColumnNames)
thresholdColumns[[columnIndex]] <- as.integer(i - 1)
thresholdLevels[[columnIndex]] <- as.integer(numThresholds)
}
return(list(thresholdColumns, thresholdLevels))
}
verifyThresholds <- function(flatModel, model, labelsData, dataName, covNames, threshName) {
if (single.na(threshName) || flatModel@unsafe) {
return()
}
modelName <- flatModel@name
object <- flatModel[[threshName]]
if (is.null(object)) {
stop(paste("The thresholds matrix/algebra", omxQuotes(threshName),
"for model", omxQuotes(modelName),
"does not exist."), call. = FALSE)
} else if (is(object, "MxAlgebra") || is(object, "MxMatrix")) {
} else {
stop(paste("The thresholds entity", omxQuotes(threshName),
"for model", omxQuotes(modelName),
"is not a MxMatrix or MxAlgebra."), call. = FALSE)
}
tuple <- evaluateMxObject(threshName, flatModel, labelsData, new.env(parent = emptyenv()))
thresholds <- tuple[[1]]
mxd <- flatModel@datasets[[dataName]]
observed <- mxd@observed
dataType <- mxd@type
threshNames <- verifyThresholdNames(thresholds, mxd, modelName, observedThresholds=FALSE)
dataThresh <- NA
if(dataType == "acov") {
dataThresh <- flatModel@datasets[[dataName]]@observedStats[['thresholds']]
}
for(i in 1:length(threshNames)) {
tName <- threshNames[[i]]
tColumn <- thresholds[,i]
values = NA
if(dataType=="raw") {
observedColumn <- observed[,tName]
if(!all(is.na(observedColumn))) {
if (!is.ordered(observedColumn)) {
stop(paste("In model",
omxQuotes(modelName),
"column",
omxQuotes(tName),
"is not an ordered factor.",
"Use mxFactor() on this column."), call. = FALSE)
}
expectedThreshCount <- length(levels(observedColumn)) - 1
if (nrow(thresholds) < expectedThreshCount) {
stop(paste("In model",
omxQuotes(modelName),
"the number of thresholds in column",
omxQuotes(tName),
"is less than the (l - 1), where l is equal",
"to the number of levels in the ordinal",
"data. Use mxFactor() on this column."),
call. = FALSE)
}
values <- tColumn[1:expectedThreshCount]
if (any(is.na(values))) {
stop(paste("In model",
omxQuotes(modelName),
"I was expecting", expectedThreshCount,
"thresholds in column", omxQuotes(tName),
"of matrix/algebra",
omxQuotes(simplifyName(threshName, modelName)),
"but I hit NA values after only",
length(values[!is.na(values)]), "thresholds.",
"You need to increase the number of",
"thresholds for", omxQuotes(tName),
"and give them values other than NA"), call. = FALSE)
}
}
} else if(dataType == "acov") {
observedThresh <- dataThresh[,tName]
if( !single.na(observedThresh)) {
expectedThreshCount <- sum(!is.na(observedThresh))
if (nrow(thresholds) < expectedThreshCount) {
stop(paste("In model",
omxQuotes(modelName),
"the number of expected thresholds in column",
omxQuotes(tName),
"(", nrow(thresholds), "), is less than the number of thresholds",
"observed in the data (", expectedThreshCount , "). If you use",
"a prep function to set up your mxData object,",
"be sure you ran mxFactor() on your data first.",
"Otherwise, be sure all unused thresholds in your",
"data thresholds argument are NA."),
call. = FALSE)
}
values <- tColumn[1:expectedThreshCount]
if (any(is.na(values))) {
stop(paste("In model",
omxQuotes(modelName),
"I was expecting", expectedThreshCount,
"thresholds in column", omxQuotes(tName),
"of matrix/algebra",
omxQuotes(simplifyName(threshName, modelName)),
"but I hit NA values after only",
length(values[!is.na(values)]), "thresholds.",
"You need to increase the number of",
"thresholds for", omxQuotes(tName),
"and give them values other than NA"), call. = FALSE)
}
}
}
if(!single.na(values)) {
sortValues <- sort(values, na.last = NA)
if (!identical(sortValues, values)) {
stop(paste("In model",
omxQuotes(modelName),
"the thresholds in column",
omxQuotes(tName), "of matrix/algebra",
omxQuotes(simplifyName(threshName, modelName)),
"are not in ascending order.",
"The current order is: ",
omxQuotes(values), "and",
"ascending order is: ",
omxQuotes(sortValues),".",
"Only the first", expectedThreshCount,
"element(s) of this column are inspected."),
call. = FALSE)
}
}
}
}
verifyThresholdNames <- function(thresholds, obsNames, modelName=NA, observedThresholds=TRUE) {
if (is(obsNames, "MxData")) obsNames <- observedDataNames(obsNames)
if(is.na(modelName)) {
modelName = "[Model Name Unknown]"
}
if (is.null(dimnames(thresholds)) || is.null(dimnames(thresholds)[[2]])) {
if(!observedThresholds) {
stop(paste("The thresholds matrix/algebra",
"for model", omxQuotes(modelName), "does not contain column names"),
call. = FALSE)
} else {
stop(paste(
"The observed thresholds matrix does not contain column names"),
call. = FALSE)
}
}
if (is.null(obsNames)) {
if(!observedThresholds) {
stop(paste("The observed data frame for model",
omxQuotes(modelName), "does not contain column names"),
call. = FALSE)
} else {
stop(paste("The observed data does not contain column names"),
call. = FALSE)
}
}
threshNames <- dimnames(thresholds)[[2]]
missingNames <- setdiff(threshNames, obsNames)
if (length(missingNames) > 0) {
if(!observedThresholds) {
stop(paste("The column name(s)", omxQuotes(missingNames),
"appear in the thresholds matrix but not",
"in the observed data.frame or matrix",
"in model", omxQuotes(modelName)), call. = FALSE)
} else {
stop(paste("The column name(s)", omxQuotes(missingNames),
"appear in the thresholds but not",
"in the observed data."), call. = FALSE)
}
}
return(threshNames)
}
displayThreshold <- function(object) {
cat("mxThreshold", '\n')
cat("$variable: ", omxQuotes(object@variable), '\n')
cat("$nThresh", object@nThresh, '\n')
cat("$values: ", object@values, '\n')
cat("$free: ", object@free, '\n')
cat("$labels: ", object@labels, '\n')
cat("$lbound: ", object@lbound, '\n')
cat("$ubound: ", object@ubound, '\n')
}
setMethod("print", "MxThreshold", function(x,...) { displayThreshold(x) })
setMethod("show", "MxThreshold", function(object) { displayThreshold(object) })
setAs("MxThreshold", "MxMatrix", function(from) { as.MxMatrix.MxThreshold(from)})
# --------------
#' An S4 base class for discrete marginal distributions
#'
#' @aliases DiscreteBase-class $,DiscreteBase-method $<-,DiscreteBase-method
#' @seealso \link{mxMarginalPoisson}, \link{mxMarginalNegativeBinomial}
DiscreteBase <- setClass(Class = "DiscreteBase",
representation = representation(
variable = "character",
maxCount = "integer",
free = "logical",
labels = "character",
lbound = "numeric",
ubound = "numeric"))
setMethod("names", "DiscreteBase", slotNames)
setMethod("$", "DiscreteBase", imxExtractSlot)
setReplaceMethod("$", "DiscreteBase",
function(x, name, value) {
stop(paste("Changing",class(x), "values directly is not recommended."))
}
)
setGeneric("getSpec", function(mxm) return(standardGeneric("getSpec")))
setClass(Class = "MxMarginalPoisson",
contains = "DiscreteBase",
representation = representation(
zeroInf = "numeric",
lambda = "numeric"))
setMethod("initialize", "MxMarginalPoisson",
function(.Object, variable, maxCount, lambda, zeroInf, free,
labels, lbound, ubound) {
.Object@variable <- variable
.Object@maxCount <- as.integer(maxCount)
.Object@lambda <- lambda
.Object@zeroInf <- zeroInf
.Object@free <- as.logical(free)
.Object@labels <- as.character(labels)
.Object@lbound <- as.numeric(lbound)
.Object@ubound <- as.numeric(ubound)
return(.Object)
})
setMethod("getSpec", "MxMarginalPoisson", function(mxm) c(mxm@maxCount, 1))
setAs("MxMarginalPoisson", "MxMatrix", function(from) {
mxMatrix("Full", nrow=2, ncol=1,
free=from@free,
values=c(from@zeroInf, from@lambda),
labels=from@labels,
lbound=from@lbound,
ubound=from@ubound,
dimnames=list(NULL, from@variable),
condenseSlots=FALSE)
})
displayMarginalPoisson <- function(Ob) {
cat("mxMarginalPoisson", '\n')
for (sl in c("variable", "maxCount", "lambda", "zeroInf",
"free", "labels", "lbound", "ubound")) {
slname <- paste0("$", sl)
cat(slname, ":", slot(Ob, sl), '\n')
}
invisible(Ob)
}
setMethod("print", "MxMarginalPoisson", function(x,...) { displayMarginalPoisson(x) })
setMethod("show", "MxMarginalPoisson", function(object) { displayMarginalPoisson(object) })
#' Indicator with marginal Poisson distribution
#'
#' @param vars character vector of manifest indicators
#' @param maxCount maximum observed count
#' @param lambda non-negative means
#' @param zeroInf zero inflation parameter in probability units
#' @param free logical vector indicating whether paremeters are free
#' @param labels character vector of parameter labels
#' @param lbound numeric vector of lower bounds
#' @param ubound numeric vector of upper bounds
#' @return a list of MxMarginPoisson obects
#' @aliases MxMarginalPoisson-class print,MxMarginalPoisson-method show,MxMarginalPoisson-method $,MxMarginalPoisson-method $<-,MxMarginalPoisson-method
mxMarginalPoisson <- function(vars, maxCount=NA, lambda, zeroInf=.01,
free=TRUE, labels=NA, lbound=0, ubound=c(1,NA))
{
for (par in c('maxCount','lambda','zeroInf')) {
if (length(get(par)) != length(vars) &&
length(vars) %% length(get(par)) != 0) {
stop(paste("Parameter",omxQuotes(par),"has wrong length"))
}
}
for (par in c('free','labels','lbound','ubound')) {
if (length(get(par)) > 1 && length(get(par))/2 != length(vars) &&
length(vars) %% (length(get(par))/2) != 0) {
stop(paste("Parameter",omxQuotes(par),"has wrong length"))
}
}
poi <- vector('list', length(vars))
vx <- 1
for (xx in 1:length(vars)) {
poi[[xx]] <-
new("MxMarginalPoisson", vars[xx],
maxCount[1 + (xx-1) %% length(maxCount)],
lambda[1 + (xx-1) %% length(lambda)],
zeroInf[1 + (xx-1) %% length(zeroInf)],
sapply(0:1, function(x) free[1 + (vx+x-1) %% length(free)]),
sapply(0:1, function(x) labels[1 + (vx+x-1) %% length(labels)]),
sapply(0:1, function(x) lbound[1 + (vx+x-1) %% length(lbound)]),
sapply(0:1, function(x) ubound[1 + (vx+x-1) %% length(ubound)]))
vx <- vx + 2
}
if (length(poi) == 1) poi <- poi[[1]]
poi
}
setClass(Class = "MxMarginalNegativeBinomial",
contains = "DiscreteBase",
representation = representation(
zeroInf = "numeric",
size = "numeric",
prob = "MxOptionalNumeric",
mu = "MxOptionalNumeric"))
setMethod("initialize", "MxMarginalNegativeBinomial",
function(.Object, variable, maxCount, size, prob, mu, zeroInf, free,
labels, lbound, ubound) {
.Object@variable <- variable
.Object@maxCount <- as.integer(maxCount)
.Object@size <- size
.Object@prob <- prob
.Object@mu <- mu
.Object@zeroInf <- zeroInf
.Object@free <- as.logical(free)
.Object@labels <- as.character(labels)
.Object@lbound <- as.numeric(lbound)
.Object@ubound <- as.numeric(ubound)
return(.Object)
})
setMethod("getSpec", "MxMarginalNegativeBinomial",
function(mxm) c(mxm@maxCount, ifelse(length(mxm@prob), 2, 3)))
setAs("MxMarginalNegativeBinomial", "MxMatrix", function(from) {
if (length(from@mu) == 0) {
v2 <- from@prob
} else {
v2 <- from@mu
}
mxMatrix("Full", nrow=3, ncol=1,
free=from@free,
values=c(from@zeroInf, from@size, v2),
labels=from@labels,
lbound=from@lbound,
ubound=from@ubound,
dimnames=list(NULL, from@variable),
condenseSlots=FALSE)
})
displayMarginalNegativeBinomial <- function(Ob) {
cat("mxMarginalNegativeBinomial", '\n')
for (sl in c("variable", "maxCount", "size", "prob", "mu", "zeroInf",
"free", "labels", "lbound", "ubound")) {
slname <- paste0("$", sl)
cat(slname, ":", slot(Ob, sl), '\n')
}
invisible(Ob)
}
setMethod("print", "MxMarginalNegativeBinomial", function(x,...) { displayMarginalNegativeBinomial(x) })
setMethod("show", "MxMarginalNegativeBinomial", function(object) { displayMarginalNegativeBinomial(object) })
#' Indicator with marginal Negative Binomial distribution
#'
#' @param vars character vector of manifest indicators
#' @param maxCount maximum observed count
#' @param size positive target number of successful trials
#' @param prob probability of success in each trial
#' @param mu alternative parametrization via mean
#' @param zeroInf zero inflation parameter in probability units
#' @param free logical vector indicating whether paremeters are free
#' @param labels character vector of parameter labels
#' @param lbound numeric vector of lower bounds
#' @param ubound numeric vector of upper bounds
#' @return a list of MxMarginPoisson obects
#' @aliases MxMarginalNegativeBinomial-class print,MxMarginalNegativeBinomial-method show,MxMarginalNegativeBinomial-method $,MxMarginalNegativeBinomial-method $<-,MxMarginalNegativeBinomial-method
mxMarginalNegativeBinomial <- function(vars, maxCount=NA, size, prob=c(), mu=c(), zeroInf=.01,
free=TRUE, labels=NA, lbound=NA, ubound=NA)
{
if (!missing(prob) && !missing(mu)) stop("'prob' and 'mu' both specified")
isMu <- !missing(mu)
if (!isMu) {
if (missing(lbound)) lbound <- c(0,0,0)
if (missing(ubound)) ubound <- c(1,NA,1)
} else {
if (missing(lbound)) lbound <- c(0,0,NA)
if (missing(ubound)) ubound <- c(1,NA,NA)
}
parList <- c('maxCount','size','zeroInf', ifelse(isMu,'mu','prob'))
for (par in parList) {
if (length(get(par)) != length(vars) &&
length(vars) %% length(get(par)) != 0) {
stop(paste("Parameter",omxQuotes(par),"has wrong length"))
}
}
for (par in c('free','labels','lbound','ubound')) {
if (length(get(par)) > 1 && length(get(par))/3 != length(vars) &&
length(vars) %% (length(get(par))/3) != 0) {
stop(paste("Parameter",omxQuotes(par),"has wrong length"))
}
}
poi <- vector('list', length(vars))
vx <- 1
for (xx in 1:length(vars)) {
poi[[xx]] <-
new("MxMarginalNegativeBinomial", vars[xx],
maxCount[1 + (xx-1) %% length(maxCount)],
size[1 + (xx-1) %% length(size)],
prob[1 + (xx-1) %% length(prob)],
mu[1 + (xx-1) %% length(mu)],
zeroInf[1 + (xx-1) %% length(zeroInf)],
sapply(0:2, function(x) free[1 + (vx+x-1) %% length(free)]),
sapply(0:2, function(x) labels[1 + (vx+x-1) %% length(labels)]),
sapply(0:2, function(x) lbound[1 + (vx+x-1) %% length(lbound)]),
sapply(0:2, function(x) ubound[1 + (vx+x-1) %% length(ubound)]))
vx <- vx + 3
}
if (length(poi) == 1) poi <- poi[[1]]
poi
}
|