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
|
#
# Copyright 2007-2018 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 = "MxBounds",
representation = representation(
min = "numeric",
max = "numeric",
parameters = "character"
))
setMethod("initialize", "MxBounds",
function(.Object, min, max, parameters) {
.Object@min <- min
.Object@max <- max
.Object@parameters <- parameters
return(.Object)
}
)
mxBounds <- function(parameters, min = NA, max = NA) {
if (length(min) > 1) {
stop("Only a single min value may be specified")
}
if (length(max) > 1) {
stop("Only a single max value may be specified")
}
if(is.na(min)) min <- NA_real_
if(is.na(max)) max <- NA_real_
if (missing(min) || !is.numeric(min)) {
stop(paste("Min argument is not a numeric",
"(the value of the lower bound)"))
}
if (missing(max) || !is.numeric(max)) {
stop(paste("Max argument is not a numeric",
"(the value of the upper bound)"))
}
if (missing(parameters) ||
typeof(parameters) != "character") {
stop(paste("Parameters argument is not a string",
"(the vector of free parameter names)"))
}
if (!is.na(min) && !is.na(max) && min > max) {
msg <- paste("min argument is greater than",
"max argument")
stop(msg)
}
return(new("MxBounds", min, max, parameters))
}
checkBounds <- function(model, bounds) {
parameters <- omxGetParameters(model)
parameters <- names(parameters)
for(i in 1:length(bounds)) {
bound <- bounds[[i]]
nomatch <- !(bound@parameters %in% parameters)
if (any(nomatch)) {
stop(paste("In model ", omxQuotes(model@name),
" the following parameter names are",
" used in mxBounds() but do",
" not exist in the model: ",
omxQuotes(bound@parameters[nomatch]), '.',
sep = ''), call. = FALSE)
}
}
}
modelAddBounds <- function(model, bounds) {
if (length(bounds) == 0) {
return(model)
}
checkBounds(model, bounds)
return(modelAddBoundsHelper(model, bounds))
}
modelAddBoundsHelper <- function(model, bounds) {
if (length(model@matrices) > 0) {
for(i in 1:length(bounds)) {
for(j in 1:length(model@matrices)) {
matrix <- model@matrices[[j]]
matches <- matrix$labels %in% bounds[[i]]@parameters
if (any(matches)) {
matrix$lbound[matches] <- bounds[[i]]@min
matrix$ubound[matches] <- bounds[[i]]@max
model@matrices[[j]] <- matrix
}
}
}
}
if (length(model@submodels) > 0) {
for(i in 1:length(model@submodels)) {
model@submodels[[i]] <- modelAddBoundsHelper(model@submodels[[i]], bounds)
}
}
return(model)
}
modelRemoveBounds <- function(model, bounds) {
if (length(bounds) == 0) {
return(model)
}
if (length(model@matrices) > 0) {
for(i in 1:length(bounds)) {
for(j in 1:length(model@matrices)) {
matrix <- model@matrices[[j]]
matches <- matrix@labels %in% bounds[[i]]@parameters
matrix@lbound[matches] <- as.numeric(NA)
matrix@ubound[matches] <- as.numeric(NA)
model@matrices[[j]] <- matrix
}
}
}
if (length(model@submodels) > 0) {
for(i in 1:length(model@submodels)) {
model@submodels[[i]] <- modelRemoveBounds(model@submodels[[i]], bounds)
}
}
return(model)
}
|