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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
|
#
# 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.
##' MxRAMModel
##'
##' This is an internal class and should not be used directly.
##'
##' @aliases
##' $<-,MxRAMModel-method
##' [[<-,MxRAMModel-method
setClass(Class = "MxRAMModel",
contains = "MxModel")
imxModelTypes[['RAM']] <- "MxRAMModel"
##' imxVariableTypes
##'
##' This is an internal function exported for those people who know
##' what they are doing.
##'
##' @details The acceptable variable types
imxVariableTypes <- c("exogenous", "endogenous")
# Define generic functions
setMethod("imxTypeName", "MxRAMModel",
function(model) { "RAM" }
)
setMethod("imxInitModel", "MxRAMModel",
function(model) {
if (any(!is.na(match(model@name, c('A','S','F','M'))))) {
stop(paste("The name", omxQuotes(model@name), "is not valid",
"for a type='RAM' model:\n Pick something other than ",
"reserved names ", omxQuotes(c('A','S','F','M'))))
}
if (is.null(model[['A']])) {
model[['A']] <- createMatrixA(model)
}
if (is.null(model[['S']])) {
model[['S']] <- createMatrixS(model)
}
if (is.null(model[['expectation']])) {
model[['expectation']] <- mxExpectationRAM('A', 'S', 'F')
}
if (is.null(model[['fitfunction']])) {
model[['fitfunction']] <- mxFitFunctionML()
}
model[['F']] <- createMatrixF(model)
return(model)
}
)
setMethod("imxModelBuilder", "MxRAMModel",
function(model, lst, name,
manifestVars, latentVars, productVars, submodels, remove, independent) {
model <- nameArgument(model, name)
model <- variablesArgumentRAM(model, manifestVars, latentVars, productVars, submodels, remove)
model <- listArgumentRAM(model, lst, remove)
notPathOrData <- getNotPathsOrData(lst)
callNextMethod(model, notPathOrData, NA, character(),
character(), character(), list(), remove, independent)
}
)
setMethod("imxVerifyModel", "MxRAMModel",
function(model) {
if ((length(model$A) == 0) ||
(length(model$S) == 0)) {
msg <- paste("The RAM model", omxQuotes(model@name),
"does not contain any paths.",
" You can add paths to your model like this:",
" mxPath(from = 'x1', to = 'y1')")
stop(msg, call. = FALSE)
}
if (length(model@submodels) > 0) {
return(all(sapply(model@submodels, imxVerifyModel)))
}
return(TRUE)
})
# Helper functions used by the generic functions
variablesArgumentRAM <- function(model, manifestVars, latentVars, productVars, submodels, remove) {
manifestVars <- unlist(manifestVars)
latentVars <- unlist(latentVars)
if (single.na(manifestVars)) {
manifestVars <- character()
}
if (single.na(latentVars)) {
latentVars <- character()
}
if (single.na(productVars)) {
productVars <- character()
}
latentVars <- c(latentVars, productVars)
if (remove == TRUE) {
if (length(latentVars) + length(manifestVars) > 0) {
model <- removeVariablesRAM(model, latentVars, manifestVars)
}
if (length(submodels)) for(i in 1:length(submodels)) {
model <- removeSingleNamedEntity(model, submodels[[i]])
}
} else {
if (length(manifestVars) + length(latentVars) > 0) {
latentVars <- varsToCharacter(latentVars, "latent")
manifestVars <- varsToCharacter(manifestVars, "manifest")
checkVariables(model, latentVars, manifestVars)
model <- addVariablesRAM(model, latentVars, manifestVars)
model[['expectation']]$isProductNode <- colnames(model$A) %in% productVars
if (length(productVars) && !is.null(model[['M']])) {
model[['M']]$values[1,productVars] <- 1
}
}
if (length(submodels)) for(i in 1:length(submodels)) {
model <- addSingleNamedEntity(model, submodels[[i]])
}
}
return(model)
}
removeVariablesRAM <- function(model, latent, manifest) {
missingLatent <- setdiff(latent, model@latentVars)
missingManifest <- setdiff(manifest, model@manifestVars)
if (length(missingLatent) > 0) {
stop(paste("The latent variable(s)", omxQuotes(missingLatent),
"are not present in the model.",
"They cannot be deleted"), call. = FALSE)
} else if (length(missingManifest) > 0) {
stop(paste("The manifest variable(s)", omxQuotes(missingManifest),
"are not present in the model.",
"They cannot be deleted"), call. = FALSE)
} else if (length(unique(latent)) != length(latent)) {
stop("The latent variables list contains duplicate elements",
call. = FALSE)
} else if (length(unique(manifest)) != length(manifest)) {
stop("The manifest variables list contains duplicate elements",
call. = FALSE)
}
model@latentVars <- setdiff(model@latentVars, latent)
model@manifestVars <- setdiff(model@manifestVars, manifest)
A <- model[['A']]
S <- model[['S']]
A <- removeVariablesAS(A, latent)
A <- removeVariablesAS(A, manifest)
S <- removeVariablesAS(S, latent)
S <- removeVariablesAS(S, manifest)
model[['F']] <- createMatrixF(model)
model[['A']] <- A
model[['S']] <- S
Thresh <- model[['Thresholds']]
if(!all.na(Thresh)) {
newCols <- setdiff(colnames(Thresh), manifest)
newRows <- nrow(Thresh) - min(colSums(is.na(Thresh@values[,newCols])))
model[['Thresholds']] <- Thresh[1:newRows,newCols]
}
return(model)
}
addVariablesRAM <- function(model, latent, manifest) {
modelLatent <- unlist(model@latentVars, use.names = FALSE)
modelManifest <- unlist(model@manifestVars, use.names = FALSE)
model <- addVariablesHelper(model, "latentVars", latent)
model <- addVariablesHelper(model, "manifestVars", manifest)
latent <- unlist(latent, use.names = FALSE)
manifest <- unlist(manifest, use.names = FALSE)
newLatent <- setdiff(latent, modelLatent)
newManifest <- setdiff(manifest, modelManifest)
A <- model[['A']]
S <- model[['S']]
M <- model[['M']]
if (is.null(A)) {
A <- createMatrixA(model)
} else {
A <- addVariablesAS(A, model, newLatent, newManifest)
}
if (is.null(S)) {
S <- createMatrixS(model)
} else {
S <- addVariablesAS(S, model, newLatent, newManifest)
}
if (!is.null(M)) {
M <- addVariablesM(M, model, newLatent, newManifest)
}
model[['A']] <- A
model[['S']] <- S
model[['M']] <- M
model[['F']] <- createMatrixF(model)
return(model)
}
listArgumentRAM <- function(model, lst, remove) {
if(remove == TRUE) {
model <- removeEntriesRAM(model, lst)
} else {
model <- addEntriesRAM(model, lst)
}
return(model)
}
addEntriesRAM <- function(model, entries) {
if (length(entries) == 0) {
return(model)
}
filter <- sapply(entries, is, "MxPath")
paths <- entries[filter]
if (length(paths) > 0) {
model <- insertAllPathsRAM(model, paths)
}
filter <- sapply(entries, is, "MxThreshold")
thresholds <- entries[filter]
if(length(thresholds) > 0) {
model <- insertAllThresholdsRAM(model, thresholds)
}
filter <- sapply(entries, is, "DiscreteBase")
discrete <- entries[filter]
if(length(discrete)) {
model <- insertDiscreteRAM(model, discrete)
}
filter <- sapply(entries, is, "MxData")
data <- entries[filter]
if (length(data) > 0) {
if (length(data) > 1) {
warning("Multiple data sources specified. Only one will be chosen.")
}
data <- data[[1]]
model@data <- data
# If the data are WLS, then change the fit function to WLS away from the default ML.
if(is(model@data,"MxDataLegacyWLS") && class(model@fitfunction) %in% "MxFitFunctionML"){
model[['fitfunction']] <- mxFitFunctionWLS()
}
model[['F']] <- createMatrixF(model)
}
return(model)
}
requireThresholds <- function(data) {
return(!is.null(data) && ((data@type == 'raw') ||
((data@type == 'cov' || data@type == 'cor'))))
}
requireMeansVector <- function(data) {
return(!is.null(data) && ((data@type == 'raw') ||
((data@type == 'cov' || data@type == 'cor') &&
!(length(data@means) == 1 && is.na(data@means)))))
}
removeEntriesRAM <- function(model, entries) {
if (length(entries) == 0) {
return(model)
}
filter <- sapply(entries, is, "MxPath")
paths <- entries[filter]
if (length(paths) > 0) {
model <- removeAllPathsRAM(model, paths)
}
return(model)
}
getNotPathsOrData <- function(lst) {
if (length(lst) == 0) {
return(lst)
}
pathfilter <- sapply(lst, is, "MxPath")
datafilter <- sapply(lst, is, "MxData")
thresholdfilter <- sapply(lst, is, "MxThreshold")
discretefilter <- sapply(lst, is, "DiscreteBase")
retval <- lst[!(pathfilter | datafilter | thresholdfilter | discretefilter)]
return(retval)
}
expectationIsMissing <- function(model, what) {
expectation <- model@expectation
return(!is.null(expectation) &&
is(expectation, "BaseExpectationNormal") &&
is.na(slot(expectation, what)))
}
expectationIsMissingMeans <- function(model) {
expectation <- model@expectation
return(!is.null(expectation) &&
is(expectation, "MxExpectationRAM") &&
is.na(expectation@M))
}
insertDiscreteRAM <- function(model, discrete) {
DiscSpec <- model@expectation@discreteSpec
Disc <- model[[model@expectation@discrete]]
if (is.null(Disc)) {
Disc <- mxMatrix("Full", 0, 0, name="Discrete", condenseSlots=FALSE)
if(expectationIsMissing(model, 'discrete')) {
model@expectation@discrete <- "Discrete"
} else {
Disc <- model[[model@expectation@discrete]]
}
}
legalVars <- model@manifestVars
if (is.list(legalVars)) {
# for LISREL
legalVars <- legalVars$endogenous
}
allVars <- unique(sapply(discrete, slot, "variable"))
varExist <- allVars %in% legalVars
if(!all(varExist)) {
missingVars <- allVars[!varExist]
stop(paste("You need to add", omxQuotes(missingVars),
"to the manifestVars before you",
"use them as discrete indicators"), call. = FALSE)
}
if(!is.list(discrete)) discrete <- list(discrete)
todoSpec <- lapply(discrete, function(x) getSpec(x))
todo <- lapply(discrete, function(x) as(x, 'MxMatrix'))
newRows <- max(sapply(todo, nrow))
newVars <- union(colnames(Disc), allVars)
if(length(newVars) > ncol(Disc)) {
newDisc <- mxMatrix("Full", newRows, length(newVars),
dimnames=list(NULL, newVars), name=Disc@name, condenseSlots=FALSE)
newDisc$values <- NA
if (ncol(Disc)) newDisc[1:nrow(Disc), 1:ncol(Disc)] <- Disc
Disc <- newDisc
newSpec <- matrix(NA, 2, length(newVars))
colnames(newSpec) <- newVars
newSpec[,colnames(DiscSpec)] <- DiscSpec
DiscSpec <- newSpec
}
for (c1 in 1:length(allVars)) {
n1 <- allVars[c1]
DiscSpec[,n1] <- todoSpec[[c1]]
mat <- todo[[c1]]
Disc[1:nrow(mat),n1] <- mat
}
model@expectation@discreteSpec <- DiscSpec
model[[Disc@name]] <- Disc
model
}
insertAllThresholdsRAM <- function(model, thresholds) {
Thresh <- model[[model@expectation@thresholds]]
if (is.null(Thresh)) {
Thresh <- mxMatrix("Full", 0, 0, name="Thresholds", condenseSlots=FALSE)
if(expectationIsMissing(model, 'thresholds')) {
model@expectation@thresholds <- "Thresholds"
} else {
Thresh <- model[[model@expectation@thresholds]]
}
}
legalVars <- model@manifestVars
if (is.list(legalVars)) {
# for LISREL
legalVars <- legalVars$endogenous
}
maxNThresh <- nrow(Thresh)
allVars <- unique(as.character(lapply(thresholds, getElement, "variable")))
varExist <- allVars %in% legalVars
if(!all(varExist)) {
missingVars <- allVars[!varExist]
stop(paste("You need to add",
omxQuotes(missingVars),
"to the manifestVars before you",
"can assign them thresholds."), call. = FALSE)
}
maxNThresh <- max(sapply(thresholds, getElement, "nThresh"))
newVars <- union(colnames(Thresh), allVars)
if(length(newVars) > ncol(Thresh)) { # Rebuild Threshold matrix if needed
oldCols <- ncol(Thresh)
oldRows <- nrow(Thresh)
newCols <- length(newVars)
newRows <- max(nrow(Thresh), maxNThresh)
newThresh <- mxMatrix("Full", newRows, newCols, dimnames=list(NULL, newVars),
name=Thresh@name, condenseSlots=FALSE) # Maintains the old ordering
newThresh@values[,] <- NA
if(oldRows > 0 && oldCols > 0) {
newThresh[1:oldRows, 1:oldCols] <- Thresh
}
Thresh <- newThresh
}
if(!is.list(thresholds)) { thresholds <- list(thresholds)}
for(i in 1:length(thresholds)) {
thisThresh <- thresholds[[i]]
values <- thisThresh@values
msg = NULL
if(all.na(values)) {
msg = paste("The thresholds you are attempting to specify",
"does not have any starting values, but type='RAM' models require them.")
}
if(!identical(values, sort(values, na.last=NA))) {
msg = paste("The thresholds you are attempting to specify",
"has starting values that are not strictly increasing,",
"but type='RAM' models require them to be.")
}
if(length(msg)>0) {
msg <- paste(msg, paste("An easy way to specify threshold starting values",
"that are evenly spaced across a normal distribution is using:"),
"mxThreshold(vars='x1', nThresh=3, values=mxNormalQuantiles(3))",
"See '?mxNormalQuantiles' and '?mxThreshold' for more details.", sep='\n')
stop(msg, call.=FALSE)
}
thisVar <- thisThresh@variable
theseRows <- 1:thisThresh@nThresh
threshMat <- as(thisThresh, "MxMatrix")
Thresh[theseRows, thisVar] <- threshMat
}
model[['Thresholds']] <- Thresh
return(model)
}
insertAllPathsRAM <- function(model, paths) {
if (is.null(model[['A']])) { model[['A']] <- createMatrixA(model) }
if (is.null(model[['S']])) { model[['S']] <- createMatrixS(model) }
for(i in 1:length(paths)) {
path <- paths[[i]]
if ("one" %in% path@from && is.null(model[['M']])) {
model[['M']] <- createMatrixM(model)
if(expectationIsMissingMeans(model)) {
model@expectation@M <- "M"
}
}
model <- insertPathRAM(prepPath(path), model)
}
return(model)
}
removeAllPathsRAM <- function(model, paths) {
A <- model[['A']]
S <- model[['S']]
M <- model[['M']]
if (is.null(A)) { A <- createMatrixA(model) }
if (is.null(S)) { S <- createMatrixS(model) }
for(i in 1:length(paths)) {
path <- paths[[i]]
if (single.na(path@to)) {
path@to <- path@from
paths[[i]] <- path
}
if (length(path@from) == 1 && (path@from == "one")) {
M <- removeMeansPathRAM(path, M)
} else {
expanded <- expandPathConnect(path@from, path@to, path@connect)
path@from <- expanded$from
path@to <- expanded$to
retval <- removePathRAM(path, A, S)
A <- retval[[1]]
S <- retval[[2]]
}
}
model[['A']] <- A
model[['S']] <- S
if (!is.null(M)) {
model[['M']] <- M
}
return(model)
}
insertPathRAM <- function(path, model) {
allfrom <- path@from
allto <- path@to
allarrows <- path@arrows
allfree <- path@free
allvalues <- path@values
alllabels <- path@labels
alllbound <- path@lbound
allubound <- path@ubound
alljoinKey <- path@joinKey
maxlength <- max(length(allfrom), length(allto))
A <- model[['A']]
S <- model[['S']]
M <- model[['M']]
A_free <- A@free
A_values <- A@values
A_labels <- A@labels
A_lbound <- A@lbound
A_ubound <- A@ubound
S_free <- S@free
S_values <- S@values
S_labels <- S@labels
S_lbound <- S@lbound
S_ubound <- S@ubound
selVec <- model[['selectionVector']]
selPlan <- model$expectation$selectionPlan
legalVars <- c(colnames(A), "one")
isProductNode <- model$expectation$isProductNode
names(isProductNode) <- colnames(A)
for(i in 0:(maxlength - 1)) {
from <- allfrom[[i %% length(allfrom) + 1]]
to <- allto[[i %% length(allto) + 1]]
arrows <- allarrows[[i %% length(allarrows) + 1]]
nextvalue <- allvalues[[i %% length(allvalues) + 1]]
nextfree <- allfree[[i %% length(allfree) + 1]]
nextlabel <- alllabels[[i %% length(alllabels) + 1]]
nextubound <- allubound[[i %% length(allubound) + 1]]
nextlbound <- alllbound[[i %% length(alllbound) + 1]]
nextjoinKey <- alljoinKey[[i %% length(alljoinKey) + 1]]
nextstep <- path@step[[i %% length(path@step) + 1]]
if (!is.na(nextjoinKey)) {
upperFrom <- strsplit(from, imxSeparatorChar, fixed = TRUE)
upperFromBadLen <- sapply(upperFrom, length) != 2
if (any(upperFromBadLen)) {
msg <- paste("Between level paths must be from",
"modelName.variableName, not",
omxQuotes(from[upperFromBadLen]))
stop(msg, call.=FALSE)
}
fromUpperModel <- upperFrom[[1]][1]
fromUpperVar <- upperFrom[[1]][2]
upperModelName <- fromUpperModel[1]
upperModel <- model[[upperModelName]]
if (is.null(upperModel)) {
msg <- paste("You need to create an upper level RAM",
"model called", omxQuotes(upperModelName),
"and add it as a submodel of", omxQuotes(model@name),
"before you can create paths between these models.")
stop(msg, call.=FALSE)
}
upperVars <- c(upperModel@manifestVars, upperModel@latentVars)
upperVarExist <- fromUpperVar %in% upperVars
if (!all(upperVarExist)) {
stop(paste("You need to add",
omxQuotes(fromUpperVar[!upperVarExist]),
"to either manifestVars or latentVars in model",
omxQuotes(upperModelName),
"before you can use them in a path."), call. = FALSE)
}
lowerVarExist <- to %in% rownames(A)
if (!all(lowerVarExist)) {
stop(paste("You need to add",
omxQuotes(to[!lowerVarExist]),
"to either manifestVars or latentVars before you",
"can use them in a path."), call. = FALSE)
}
bMatName <- NULL
priorBetween <- model$expectation$between
sameJoinMask <- sapply(priorBetween, function(x) {
bmat <- model[[ x ]]
if (is.null(bmat)) return(FALSE)
bmat$joinKey == nextjoinKey && bmat$joinModel == upperModelName
})
if (length(sameJoinMask) && sum(sameJoinMask) > 1) {
stop(paste("Confusingly there is more than 1 join to", omxQuotes(upperModelName),
"using foreign key", omxQuotes(path@joinKey)), call.=FALSE)
}
if (all(!sameJoinMask)) {
bMatNameBase <- paste0("from_", upperModelName)
bMatName <- bMatNameBase
found <- FALSE
for (try in 1:9) {
if (is.null( model[[ bMatName ]] )) {
found = TRUE
break
}
bMatName <- paste0(bMatNameBase, try)
}
if (!found) {
stop(paste("Failed to invent an unused name for the",
"between level mapping matrix. Tried variations on",
omxQuotes(bMatNameBase)), call.=FALSE)
}
model <- mxModel(model, mxMatrix(name=bMatName, nrow=nrow(A), ncol=length(upperVars),
dimnames=list(rownames(A), upperVars),
joinKey = nextjoinKey, joinModel = upperModelName))
} else {
bMatName <- priorBetween[ which(sameJoinMask) ]
}
bMat <- model[[ bMatName ]]
bMat <- imxGentleResize(bMat, list(rownames(A), upperVars))
bMat$values[to, fromUpperVar] <- nextvalue
bMat$free [to, fromUpperVar] <- nextfree
bMat$labels[to, fromUpperVar] <- nextlabel
bMat$ubound[to, fromUpperVar] <- nextubound
bMat$lbound[to, fromUpperVar] <- nextlbound
model[[ bMatName ]] <- bMat
model$expectation$between <- unique(c(priorBetween, bMatName))
next
}
allFromTo <- unique(c(from, to))
varExist <- allFromTo %in% legalVars
if(!all(varExist)) {
fromComponents <- length(strsplit(from, imxSeparatorChar, fixed = TRUE)[[1]])
if (fromComponents == 2) {
stop(paste0("In model ", omxQuotes(model@name),
", you tried to add a path from ", omxQuotes(from),
". Did you forget joinKey?"))
}
missingVars <- allFromTo[!varExist]
stop(paste("You need to add",
omxQuotes(missingVars),
"to either manifestVars or latentVars before you",
"can use them in a path."), call. = FALSE)
}
if (from == 'one') {
if (arrows != 1) {
stop(paste('The means path must be a single-headed arrow\n',
'path from "one" to', omxQuotes(to)), call. = FALSE)
}
if (isProductNode[to]) {
stop(paste('Cannot change mean of product node', omxQuotes(to),
'from the identity value of 1'), call.=FALSE)
}
M@free[1, to] <- nextfree
M@values[1, to] <- nextvalue
M@labels[1, to] <- nextlabel
M@ubound[1, to] <- nextubound
M@lbound[1, to] <- nextlbound
next
}
if (arrows == 0) {
# directly modify the unfiltered covariance matrix
# expectation: data.frame w/ step, rowname, colname ; name of mxMatrix
# mxMatrix: vector of parameters to plop in
pair <- c(from,to)[order(match(c(from,to), legalVars))] # canonical order
r1 <- data.frame(step=nextstep, from=pair[1], to=pair[2])
selPlan <- rbind(selPlan, r1)
oldSelVec <- selVec
selVec <- mxMatrix('Full', nrow(selPlan), 1)
if (!is.null(oldSelVec)) selVec[1:nrow(oldSelVec),1] <- oldSelVec
selVec[nrow(selVec),1]$free <- nextfree
selVec[nrow(selVec),1]$values <- nextvalue
selVec[nrow(selVec),1]$labels <- nextlabel
selVec[nrow(selVec),1]$ubound <- nextubound
selVec[nrow(selVec),1]$lbound <- nextlbound
p1 <- order(selPlan$step, selPlan$from, selPlan$to)
selPlan <- selPlan[p1,]
selVec <- selVec[p1,]
} else if (arrows == 1) {
A_free[to, from] <- nextfree
A_values[to, from] <- nextvalue
A_labels[to, from] <- nextlabel
A_ubound[to, from] <- nextubound
A_lbound[to, from] <- nextlbound
S_values[to, from] <- 0
S_labels[to, from] <- as.character(NA)
S_free[to, from] <- FALSE
S_values[from, to] <- 0
S_labels[from, to] <- as.character(NA)
S_free[from, to] <- FALSE
} else if (arrows == 2) {
S_free[to, from] <- nextfree
S_values[to, from] <- nextvalue
S_labels[to, from] <- nextlabel
S_ubound[to, from] <- nextubound
S_lbound[to, from] <- nextlbound
S_free[from, to] <- nextfree
S_values[from, to] <- nextvalue
S_labels[from, to] <- nextlabel
S_ubound[from, to] <- nextubound
S_lbound[from, to] <- nextlbound
A_values[to, from] <- 0
A_labels[to, from] <- as.character(NA)
A_free[to, from] <- FALSE
A_values[to, from] <- 0
A_labels[to, from] <- as.character(NA)
A_free[to, from] <- FALSE
} else {
stop(paste("Unknown arrow type", arrows,
"with source", omxQuotes(from),
"and sink", omxQuotes(to)),
call. = FALSE)
}
}
A@free <- A_free
A@values <- A_values
A@labels <- A_labels
A@lbound <- A_lbound
A@ubound <- A_ubound
S@free <- S_free
S@values <- S_values
S@labels <- S_labels
S@lbound <- S_lbound
S@ubound <- S_ubound
model[['A']] <- A
model[['S']] <- S
if (!is.null(M)) model[['M']] <- M
if (!is.null(selVec)) {
model$expectation$selectionVector <- 'selectionVector'
model[['selectionVector']] <- selVec
}
if (!is.null(selPlan)) model$expectation$selectionPlan <- selPlan
model
}
removeMeansPathRAM <- function(path, M) {
if(is.null(M)) {
return(NULL)
}
allto <- path@to
for(i in 0:(length(allto) - 1)) {
to <- allto[[i %% length(allto) + 1]]
M@free[1, to] <- FALSE
M@values[1, to] <- 0
M@labels[1, to] <- as.character(NA)
}
return(M)
}
removePathRAM <- function(path, A, S) {
allfrom <- path@from
allto <- path@to
allarrows <- path@arrows
maxlength <- max(length(allfrom), length(allto))
A_free <- A@free
A_values <- A@values
A_labels <- A@labels
S_free <- S@free
S_values <- S@values
S_labels <- S@labels
for(i in 0:(maxlength - 1)) {
from <- allfrom[i %% length(allfrom) + 1]
to <- allto[i %% length(allto) + 1]
arrows <- allarrows[i %% length(allarrows) + 1]
if (arrows == 1) {
A_values[to, from] <- 0
A_labels[to, from] <- as.character(NA)
A_free[to, from] <- FALSE
} else if (arrows == 2) {
S_values[to, from] <- 0
S_labels[to, from] <- as.character(NA)
S_free[to, from] <- FALSE
S_values[from, to] <- 0
S_labels[from, to] <- as.character(NA)
S_free[from, to] <- FALSE
} else {
stop(paste("Unknown arrow type", arrows,
"with source", omxQuotes(from),
"and sink", omxQuotes(to)),
call. = FALSE)
}
}
A@free <- A_free
A@values <- A_values
A@labels <- A_labels
S@free <- S_free
S@values <- S_values
S@labels <- S_labels
return(list(A, S))
}
createMatrixM <- function(model) {
variables <- c(model@manifestVars, model@latentVars)
len <- length(variables)
names <- list(NULL, variables)
values <- matrix(as.numeric(model$expectation$isProductNode), 1, len)
labels <- matrix(as.character(NA), 1, len)
free <- matrix(c(rep.int(FALSE, length(model@manifestVars)),
rep.int(FALSE, length(model@latentVars))), 1, len)
retval <- mxMatrix("Full", values = values, free = free, labels = labels, name = "M", condenseSlots=FALSE)
dimnames(retval) <- names
return(retval)
}
createMatrixA <- function(model) {
variables <- c(model@manifestVars, model@latentVars)
len <- length(variables)
names <- list(variables, variables)
values <- matrix(0, len, len)
free <- matrix(FALSE, len, len)
labels <- matrix(as.character(NA), len, len)
retval <- mxMatrix("Full", values = values, free = free, labels = labels, name = "A", condenseSlots=FALSE)
dimnames(retval) <- names
return(retval)
}
createMatrixS <- function(model) {
variables <- c(model@manifestVars, model@latentVars)
len <- length(variables)
names <- list(variables, variables)
values <- matrix(0, len, len)
free <- matrix(FALSE, len, len)
labels <- matrix(as.character(NA), len, len)
retval <- mxMatrix("Symm", values = values, free = free, labels = labels, name = "S", condenseSlots=FALSE)
dimnames(retval) <- names
return(retval)
}
createMatrixF <- function(model) {
variables <- c(model@manifestVars, model@latentVars)
len <- length(variables)
values <- diag(nrow = length(model@manifestVars), ncol = len)
names <- list(model@manifestVars, variables)
if (!is.null(model@data)) {
manifestNames <- observedDataNames(model@data)
extraVars <- setdiff(model@manifestVars, manifestNames)
# would need to exclude freq, weight, keys, etc TODO
## extraData <- setdiff(manifestNames, model@manifestVars)
## if (length(extraData) > 0) {
## msg <- paste("The observed data contains the variables:",
## omxQuotes(extraData), "that have not been declared in the",
## "manifest variables.")
## stop(msg, call. = FALSE)
## }
if (length(extraVars) > 0) {
msg <- paste("The manifest variables include",
omxQuotes(extraVars), "that have not been found in the",
"observed data.")
stop(msg, call. = FALSE)
}
}
free <- matrix(FALSE, length(model@manifestVars), len)
labels <- matrix(as.character(NA), length(model@manifestVars), len)
retval <- mxMatrix("Full", values = values, free = free, labels = labels, name = "F", condenseSlots=TRUE)
dimnames(retval) <- names
return(retval)
}
addVariablesAS <- function(oldmatrix, model, newLatent, newManifest) {
newLatent <- length(newLatent)
newManifest <- length(newManifest)
oldmatrix@values <- addVariablesMatrix(oldmatrix@values, 0,
model, newLatent, newManifest)
oldmatrix@free <- addVariablesMatrix(oldmatrix@free, FALSE,
model, newLatent, newManifest)
oldmatrix@labels <- addVariablesMatrix(oldmatrix@labels, as.character(NA),
model, newLatent, newManifest)
oldmatrix@lbound <- addVariablesMatrix(oldmatrix@lbound, as.numeric(NA),
model, newLatent, newManifest)
oldmatrix@ubound <- addVariablesMatrix(oldmatrix@ubound, as.numeric(NA),
model, newLatent, newManifest)
variables <- c(model@manifestVars, model@latentVars)
dimnames(oldmatrix) <- list(variables, variables)
return(oldmatrix)
}
addVariablesM <- function(oldmatrix, model, newLatent, newManifest) {
oldmatrix@values <- addVariablesMatrixM(oldmatrix@values, 0, 0, model, newLatent, newManifest)
oldmatrix@free <- addVariablesMatrixM(oldmatrix@free, FALSE, TRUE, model, newLatent, newManifest)
oldmatrix@labels <- addVariablesMatrixM(oldmatrix@labels, as.character(NA), as.character(NA),
model, newLatent, newManifest)
oldmatrix@lbound <- addVariablesMatrixM(oldmatrix@lbound, as.numeric(NA), as.numeric(NA),
model, newLatent, newManifest)
oldmatrix@ubound <- addVariablesMatrixM(oldmatrix@ubound, as.numeric(NA), as.numeric(NA),
model, newLatent, newManifest)
dimnames(oldmatrix) <- list(NULL, c(model@manifestVars, model@latentVars))
return(oldmatrix)
}
removeVariablesAS <- function(oldmatrix, variables) {
if (length(variables) > 0) {
for (i in 1:length(variables)) {
index <- match(variables[[i]], dimnames(oldmatrix)[[1]])
oldmatrix@values <- oldmatrix@values[-index, -index]
oldmatrix@free <- oldmatrix@free[-index, -index]
oldmatrix@labels <- oldmatrix@labels[-index, -index]
oldmatrix@lbound <- oldmatrix@lbound[-index, -index]
oldmatrix@ubound <- oldmatrix@ubound[-index, -index]
}
}
return(oldmatrix)
}
addVariablesMatrix <- function(oldmatrix, value, model, newLatent, newManifest) {
currentManifest <- length(model@manifestVars) - newManifest
currentLatent <- length(model@latentVars) - newLatent
newSize <- length(model@manifestVars) + length(model@latentVars)
if (currentManifest > 0) {
manifestXmanifest <- oldmatrix[1 : currentManifest, 1 : currentManifest]
} else {
manifestXmanifest <- matrix(value, 0, 0)
}
if (currentLatent > 0) {
latentStart <- currentManifest + 1
latentEnd <- currentManifest + currentLatent
manifestXlatent <- oldmatrix[1 : currentManifest, latentStart : latentEnd]
latentXmanifest <- oldmatrix[latentStart : latentEnd, 1 : currentManifest]
latentXlatent <- oldmatrix[latentStart : latentEnd, latentStart : latentEnd]
} else {
manifestXlatent <- matrix(value, 0, 0)
latentXmanifest <- matrix(value, 0, 0)
latentXlatent <- matrix(value, 0, 0)
}
newtop <- cbind(manifestXmanifest, matrix(value, currentManifest, newManifest),
manifestXlatent, matrix(value, currentManifest, newLatent))
newtop <- rbind(newtop, matrix(value, newManifest, newSize))
newbottom <- cbind(latentXmanifest, matrix(value, currentLatent, newManifest),
latentXlatent, matrix(value, currentLatent, newLatent))
newbottom <- rbind(newbottom, matrix(value, newLatent, newSize))
newmatrix <- rbind(newtop, newbottom)
return(newmatrix)
}
addVariablesMatrixM <- function(oldmatrix, newLatentValue, newManifestValue, model, newLatent, newManifest) {
newManifest <- length(newManifest)
newLatent <- length(newLatent)
currentManifest <- length(model@manifestVars) - newManifest
currentLatent <- length(model@latentVars) - newLatent
values <- c(oldmatrix[1, 1:currentManifest],
rep.int(newManifestValue, newManifest),
oldmatrix[1, (currentManifest + 1) : (currentLatent + currentManifest)],
rep.int(newLatentValue, newLatent))
newmatrix <- matrix(values, 1, length(model@manifestVars) + length(model@latentVars))
return(newmatrix)
}
|