File: MxFlatModel.R

package info (click to toggle)
r-cran-openmx 2.21.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,412 kB
  • sloc: cpp: 36,577; ansic: 13,811; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 5
file content (289 lines) | stat: -rwxr-xr-x 9,364 bytes parent folder | download | duplicates (3)
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
#
#   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.


##' MxFlatModel
##'
##' This is an internal class and should not be used.
##'
##' @aliases
##' MxFlatModel-class
##' $,MxFlatModel-method
##' $<-,MxFlatModel-method
##' [[,MxFlatModel-method
##' [[<-,MxFlatModel-method
##' print,MxFlatModel-method
##' show,MxFlatModel-method
##' names,MxFlatModel-method
setClass(Class = "MxFlatModel",
	representation = representation(
		expectations = "list",
		fitfunctions = "list",
		datasets = "list",
		constMatrices = "list",
		freeMatrices = "list",
		parameters = "list",
		unsafe = "logical"
	),
	contains = "MxModel")
	
setMethod("initialize", "MxFlatModel",
	function(.Object, model) {
		.Object@name <- model@name
		return(.Object)
	}
)

setMethod("[[", "MxFlatModel",
	function(x, i, j, ..., drop = FALSE) {
		return(flatExtractMethod(x, i))
	}
)

setReplaceMethod("[[", "MxFlatModel",
	function(x, i, j, value) {
		return(flatReplaceMethod(x, i, value))
	}
)

setMethod("$", "MxFlatModel",
	function(x, name) {
		return(flatExtractMethod(x, name))
	}
)

setReplaceMethod("$", "MxFlatModel",
	function(x, name, value) {
		return(flatReplaceMethod(x, name, value))
	}
)

setMethod("names", "MxFlatModel", 
	function(x) {
			return(flatNamesMethod(x))
	}
)

flatNamesMethod <- function(model) {
	return(flatNamespaceList(model))
}

flatExtractMethod <- function(model, index) {
	return(flatNamespaceSearch(model, index))
}

flatReplaceMethod <- function(model, index, value) {
	return(flatNamespaceSearchReplace(model, index, value))
}

##' imxCheckVariables
##'
##' This is an internal function exported for those people who know
##' what they are doing.
##' 
##' @param flatModel flatModel
##' @param namespace namespace
imxCheckVariables <- function(flatModel, namespace) {
	datasets <- flatModel@datasets
	retval <- list()
	retval$parameters <- namespace$parameters
	retval$values <- namespace$values
	retval$startvals <- list()
	retval$bounds <- list()
	if(length(flatModel@matrices) > 0) {
		startVals <- list()
		freeVars <- list()
		fixedVars <- list()
		bounds <- list()
		varlocations <- list()
		for(i in 1:length(flatModel@matrices)) {
			result <- checkVariablesHelper(flatModel@matrices[[i]], startVals, 
				freeVars, fixedVars, bounds, varlocations, flatModel@name, datasets)
			startVals <- result[[1]]
			freeVars <- result[[2]]
			fixedVars <- result[[3]]
			bounds <- result[[4]]
			varlocations <- result[[5]]
		}
		retval$startvals <- startVals
		retval$bounds <- bounds
	}
	return(retval)
}

rowColToString <- function(row, col) {
	return(paste("(", row, ", ", col, ")", sep = ""))
}

checkVariablesHelper <- function(matrix, startVals, freeVars,
		fixedVars, bounds, varlocations, modelname, datasets) {
	labels <- matrix@labels
	free <- matrix@free
	values <- matrix@values
	lbounds <- matrix@lbound
	ubounds <- matrix@ubound
	select <- !is.na(labels)
	free <- free[select]
	values <- values[select]
	lbounds <- lbounds[select]
	ubounds <- ubounds[select]
	rows <- row(labels)[select]
	cols <- col(labels)[select]
	labels <- labels[select]
	if(length(labels) > 0) {
		for(i in 1:length(labels)) {
			label <- labels[[i]]
			isFree <- free[[i]]
			value <- values[[i]]
			lbound <- lbounds[[i]]
			ubound <- ubounds[[i]]
			row <- rows[[i]]
			col <- cols[[i]]
			if (imxIsDefinitionVariable(label)) {
				if (isFree) {
					stop(paste("The definition variable", omxQuotes(label),
						"has been assigned to a free parameter",
						"in matrix", omxQuotes(simplifyName(matrix@name, modelname))), call. = FALSE)
				}
				result <- strsplit(label, '.', fixed=TRUE)[[1]]
				if(length(result) != 3) {
					stop("Internal error: definition variable does not have three pieces", call. = FALSE)
				}
				dataname <- paste(result[[1]], '.', result[[2]], sep = '')
				targetdata <- datasets[[dataname]]
				if (is.null(targetdata)) {
					stop(paste("The definition variable", omxQuotes(label),
						"in matrix", omxQuotes(simplifyName(matrix@name, modelname)),
						"refers to a data set that does not exist"), call. = FALSE)
				}
				targetNames <- dimnames(targetdata@observed)
				if (is.null(targetNames) || is.null(targetNames[[2]])) {
					stop(paste("The definition variable", omxQuotes(label),
						"in matrix", omxQuotes(simplifyName(matrix@name, modelname)),
						"refers to a data set that does not contain column names"), call. = FALSE)	
				}
				if (!(result[[3]] %in% targetNames[[2]])) {
					stop(paste("The definition variable", omxQuotes(label),
						"in matrix", omxQuotes(simplifyName(matrix@name, modelname)),
						"refers to a data set that does not contain",
						"a column with name", omxQuotes(result[[3]])), call. = FALSE)	
				}
			} else if (isFree) {
				if (hasSquareBrackets(label)) {
					stop(paste("The label with square brackets",
						"has been assigned to a free parameter",
						"in matrix", omxQuotes(simplifyName(matrix@name, modelname)),
						"at row", row, "and column", col), call. = FALSE)
				} else if (label %in% fixedVars) {
					stop(paste("The label", omxQuotes(label),
						"has been assigned to a free parameter",
						"and a fixed value!"), call. = FALSE)
				} else if (label %in% freeVars && !is.na(value) && length(startVals[[label]]) &&
					   startVals[[label]] != value) {
					loc <- varlocations[[label]]
					stop(paste("The free parameter", omxQuotes(label),
						"has been assigned multiple starting values!",
						"See matrix",
						omxQuotes(simplifyName(matrix@name, modelname)), 
						"at location", 
						rowColToString(row, col),
						"and matrix", 
						omxQuotes(simplifyName(loc[[1]], modelname)), 
						"at location", 
						rowColToString(loc[[2]], loc[[3]]), 
						"If you want to randomly select one of these values, call",
						"model <- omxAssignFirstParameters(model) before running again."), call. = FALSE)
				} else if (label %in% freeVars && 
								!identicalNA(lbound, bounds[[label]][[1]])) {
					loc <- varlocations[[label]]
					stop(paste("The free parameter", omxQuotes(label),
						"has been assigned multiple lower bounds!",
						"See matrix",
						omxQuotes(simplifyName(matrix@name, modelname)), 
						"at location", 
						rowColToString(row, col),
						"and matrix", 
						omxQuotes(simplifyName(loc[[1]], modelname)), 
						"at location", 
						rowColToString(loc[[2]], loc[[3]])), call. = FALSE)
				} else if (label %in% freeVars && 
								!identicalNA(ubound, bounds[[label]][[2]])) {
					loc <- varlocations[[label]]
					stop(paste("The free parameter", omxQuotes(label),
						"has been assigned multiple upper bounds!",
						"See matrix",
						omxQuotes(simplifyName(matrix@name, modelname)), 
						"at location", 
						rowColToString(row, col),
						"and matrix", 
						omxQuotes(simplifyName(loc[[1]], modelname)), 
						"at location", 
						rowColToString(loc[[2]], loc[[3]])), call. = FALSE)
				} else {
					if (!is.na(value)) startVals[[label]] <- value
					freeVars <- union(freeVars, label)
					bounds[[label]] <- c(lbound, ubound)
				}
			} else {
				if (hasSquareBrackets(label)) {
				} else if (label %in% freeVars) {
					stop(paste("The label", omxQuotes(label),
						"has been assigned to a fixed value",
						"and a free parameter!"), call. = FALSE)
				} else if (label %in% fixedVars && !is.na(value) && length(startVals[[label]]) &&
					   startVals[[label]] != value) {
					loc <- varlocations[[label]]
					stop(paste("The fixed variable", omxQuotes(label),
						"has been assigned multiple starting values!",
						"See matrix",
						omxQuotes(simplifyName(matrix@name, modelname)), 
						"at location", 
						rowColToString(row, col),
						"and matrix", 
						omxQuotes(simplifyName(loc[[1]], modelname)), 
						"at location", 
						rowColToString(loc[[2]], loc[[3]]),
						"Equate the values manually, give them distinct labels, or free the parameters and call",
						"model <- omxAssignFirstParameters(model)",
						"before running again."), call. = FALSE)
				} else {
					if (!is.na(value)) startVals[[label]] <- value
					fixedVars <- union(fixedVars, label)
				}
			}
			if(is.null(varlocations[[label]])) {
				varlocations[[label]] <- c(matrix@name, row, col)
			}
		}
	}
	return(list(startVals, freeVars, fixedVars, bounds, varlocations))
}

identicalNA <- function(x, y) {
	return((is.na(x) && is.na(y)) || (identical(x,y)))
}

displayFlatModel <- function(fm) {
	cat("expectations : ")
	print(fm@expectations)
	cat("fitfunctions : ")
	print(fm@fitfunctions)
	cat("compute : ")
	print(fm@compute)
	cat("datasets :", length(fm@datasets), '\n') 
}

setMethod("print", "MxFlatModel", function(x,...) { callNextMethod(); displayFlatModel(x) })
setMethod("show", "MxFlatModel", function(object) { callNextMethod(); displayFlatModel(object) })