File: RObjectiveLISRELFactorRegression.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 (382 lines) | stat: -rw-r--r-- 12,398 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
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
#
#   Copyright 2007-2019 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.


#------------------------------------------------------------------------------
# Author: Michael D Hunter
# Filename: RObjectiveLISRELFactorRegression.R
# Date: 2011.06.30

# Purpose: To demonstrate an example of LISREL model
#  specification in OpenMx as an mxFitFunctionR.
#  Later there will be a model of type LISREL.

# Revision History:
#   Michael Hunter -- 2011.06.30 Created File
#   Michael Hunter -- 2011.07.21 Added extensive comments, contributed to svn
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# The source of the model and the data presented here is
# Joreskog, K. G., & Sorbom, D.  (1982).  Recent developments in
#  structural equation modeling.  Journal of Marketing Research,
#  19(4), p. 404-416.
# The BibTeX is
#$article{ ,
#    Author = {Karl G {J\"{o}reskog} and Dag S\"{o}rbom},
#    Journal = {Journal of Marketing Research},
#    Month = {November},
#    Number = {4},
#    Pages = {404-416},
#    Title = {Recent Developments in Structural Equation Modeling},
#    Volume = {19},
#    Year = {1982}
#    }

# The data occurs on page 409 in Table 2
# The model is example 1, on pages 409-411.


#------------------------------------------------------------------------------
# Load the OpenMx package
library(OpenMx)
#options(error = utils::recover)

#------------------------------------------------------------------------------
# On LISREL
# 
# LISREL requires specifying 9 matrices.  These are
# LambdaX, LambdaY, ThetaDelta, ThetaEpsilon, ThetaDeltaEpsilon,
#  Beta, Gamma, Psi, and Phi
# 
# The extended LISREL model (a model including means) requires 
#  specifying 4 additional matrices, for a total of 13.  These are
#  Alpha, Kappa, TauX, and TauY.
# 
# Here is a brief description of the matrices
# LambdaX (abbreviated LX): matrix of exogenous factor loadings
# LambdaY (abbreviated LY): matrix of endogenous factor loadings
# ThetaDelta (abbreviated TD): residual covariance matrix of exogenous manifest variables (often diagonal)
# ThetaEpsilon (abbreviated TE): residual covariance matrix of endogenous manifest variables (often diagonal)
# ThetaDeltaEpsilon (abbreviated TH): residual covariance matrix of exogenous manifest variables with endogenous manifest variables (often zero)
# Beta (abbreviated BE): matrix of regression weights of the endogenous latent variables predicting themselves
# Gamma (abbreviated GA): matrix of regression weights of the exogenous latent variables predicting the endogenous latent variables
# Psi (abbreviated PS): residual covariance matrix of the endogenous latent variables
# Phi (abbreviated PH): covariance matrix of the exogenous latent variables (factor covariance matrix)
# Alpha (abbreviated AL): relates to means of endogenous latent variables
# Kappa (abbreviated KA): means of the exogenous latent variables
# TauX (abbreviated TX): residual means of the exogenous manifest variables
# TauY (abbreviated TY): residual means of the endogenous manifest variables


#------------------------------------------------------------------------------
# Define the function to perform the LISREL optimization


#--------------------------------------
# Author: Michael D Hunter
# Filename: funcLISREL.R
# Date: 2011.06.30
# Purpose: To define a number of helper functions to allow
#  OpenMx to more easily estimate models under the LISREL
#  specification.  This implementation uses an mxFitFunctionR
#  function.  Later these functions will be translated and
#  moved to the C backend for an mxModel of type LISREL
#  as opposed to RAM.
#--------------------------------------


#--------------------------------------
# Take in the Lambda matrices of factor loadings
#  and put them together in blocks to form one
#  larger Lambda matrix

lisrelBlockLambda <- function(LambdaY, LambdaX){
    
    upper <- matrix(0, nrow=nrow(LambdaY), ncol=ncol(LambdaX))
    lower <- matrix(0, nrow=nrow(LambdaX), ncol=ncol(LambdaY))
    
    ret <- rbind(cbind(LambdaY, upper),
                 cbind(lower, LambdaX)
                )
    return(ret)
}


#--------------------------------------
# Take in the Theta matrices of measurement error covariances
#  and put them together in blocks to form one larger
#  Theta matrix

lisrelBlockTheta <- function(ThetaEpsilon, ThetaDelta, ThetaDeltaEpsilon){
    
    ret <- rbind(cbind(ThetaEpsilon, t(ThetaDeltaEpsilon)),
                 cbind(ThetaDeltaEpsilon, ThetaDelta)
            )
    return(ret)
}


#--------------------------------------
# Take in the regression matrices (Beta and Gamma),
#  the covariance matrix of the exogenous variables (Phi),
#  and the latent structural error covariance matrix (Psi)
#  then block them together to form a matrix I'm calling W

lisrelBlockW <- function(Beta, Gamma, Phi, Psi){
    
    I <- diag(1, dim(Beta)[1])
    A <- solve(I - Beta)
    
    AG <- A %*% Gamma
    
    block1 <- AG %*% Phi %*% t(AG) + A %*% Psi %*% t(A)
    block2 <- AG %*% Phi
    
    ret <- rbind(cbind(block1, block2),
                 cbind(t(block2), Phi)
                )
    return(ret)
}


#--------------------------------------
# Take all of the LISREL matrices and block them and
#  produce the model implied (aka expected) covariance
#  matrix (here called miCov)

lisrelCov <- function(LambdaX, LambdaY, ThetaEpsilon, ThetaDelta, ThetaDeltaEpsilon, Beta, Gamma, Phi, Psi){
    
    Lambda <- lisrelBlockLambda(LambdaY, LambdaX)
    Theta <- lisrelBlockTheta(ThetaEpsilon, ThetaDelta, ThetaDeltaEpsilon)
    W <- lisrelBlockW(Beta, Gamma, Phi, Psi)
    
    miCov <- Lambda %*% W %*% t(Lambda) + Theta
    
    return(miCov)
}


#--------------------------------------
# Take the model implied covariance matrix and the observed covariance
#  matrix, and produce a value that can be minimized to produce the
#  maximum likelihood estimates of the parameters
# Objective Function

lisrelML <- function(miCov, obsCov){
    ret <- suppressWarnings(log(det(miCov))) + tr(obsCov %*% solve(miCov))
    return(ret)
}


#--------------------------------------
# Rescale the output of lisrelML to official log likelihood units
# x is the output of lisrelML

lisrelMLRescale <- function(x, obsCov){
    ret <- x - suppressWarnings(log(det(obsCov))) - ncol(obsCov)
    return(ret)
}


#--------------------------------------
# Produce the Unweighted Least Squares (ULS) value that can be minimized
#  to make ULS estimates of the parameters
# Objective Function

lisrelULS <- function(miCov, obsCov){
    ret <- tr((obsCov-miCov)^2)/2
    return(ret)
}


#--------------------------------------
# Take in an mxModel object and produce a function value to be minimized
#  to produce maximum likelihood estimates of the free parameters.
# mxFitFunctionR function

lisrelCovMx <- function(model, state){
    expectedCov <- lisrelCov(
        model$LambdaX$values,
        model$LambdaY$values,
        model$ThetaEpsilon$values,
        model$ThetaDelta$values,
        model$ThetaDeltaEpsilon$values,
        model$Beta$values,
        model$Gamma$values,
        model$Phi$values,
        model$Psi$values)
    ret <- lisrelML(expectedCov, model$data$observed)
    return(ret)
}


#------------------------------------------------------------------------------
# Define the data to be fit
obsCor <- matrix(c(
    1.00, 0.43, 0.52, 0.54, 0.83,
    0.43, 1.00, 0.67, 0.45, 0.45,
    0.52, 0.67, 1.00, 0.63, 0.56,
    0.54, 0.45, 0.63, 1.00, 0.52,
    0.83, 0.45, 0.56, 0.52, 1.00),
    nrow=5, ncol=5, byrow=TRUE
)

# Define the exogenous and endogenous variables
endInd <- c(1, 5) #Indexes of the endogenous/dependent variables
exoInd <- c(2:4) #Indexes of the exogenous/independent variables

# Correlation matrix rearranged into separate blocks of
# endogenous and exogenous variables
gObsCor <- cbind(rbind(obsCor[endInd, endInd], obsCor[exoInd, endInd]), rbind(obsCor[endInd, exoInd], obsCor[exoInd, exoInd]) )


#------------------------------------------------------------------------------
# Specify the model

numManExo <- length(exoInd)
numManEnd <- length(endInd)
numLatExo <- 1
numLatEnd <- 2

Jor82Ex1 <- mxModel(
    name='LISREL 5 Model Example 1 from 1982 Paper',
    mxData(observed=gObsCor, type='cor', numObs=1000), # ADJUST HERE
    mxMatrix(
        name='LambdaY',
        type='Iden',
        free = FALSE,
        nrow=numManEnd,
        ncol=numLatEnd
        ),
    mxMatrix(
        name='LambdaX',
        nrow=numManExo,
        ncol=numLatExo,
        free = TRUE,
        values=c(.8, .8, .8),
        labels=c('lam1', 'lam2', 'lam3')
        ),
    mxMatrix(
        name='Beta',
        nrow=numLatEnd,
        ncol=numLatEnd,
        free=c(F, T, F, F),
        values=c(0, .8, 0, 0),
        labels=c(NA, 'bet', NA, NA)
        ),
    mxMatrix(
        name='Gamma',
        nrow=numLatEnd,
        ncol=numLatExo,
        free = TRUE,
        values=c(.8, 1.8),
        labels=c('gam1', 'gam2')
        ),
    mxMatrix(
        name='Psi',
        nrow=numLatEnd,
        ncol=numLatEnd,
        free=c(T, F, F, T),
        values=c(.8, 0, 0, 1.8),
        labels=c('zet1sq', NA, NA, 'zet2sq'),
	lbound=1e-6
        ),
    mxMatrix(
        name='Phi',
        nrow=numLatExo,
        ncol=numLatExo,
        free = FALSE,
        values=1
        ),
    mxMatrix(
        name='ThetaEpsilon',
        nrow=numManEnd,
        ncol=numManEnd,
        free = FALSE,
        values=0
        ),
    mxMatrix(
        name='ThetaDelta',
        type='Symm',
        nrow=numManExo,
        ncol=numManExo,
        free=c(T, F, T, F, T, F, T, F, T),
        values=c(.8, 0, .1, 0, .8, 0, .1, 0, .8),
        labels=c('del1', NA, 'del13', NA, 'del2', NA, 'del13', NA, 'del3')
        ),
    mxMatrix(
        name='ThetaDeltaEpsilon',
        nrow=numManExo,
        ncol=numManEnd,
        free = FALSE,
        values=0
        ),
    mxFitFunctionR(lisrelCovMx)
)

Jor82Ex1$ThetaDelta$lbound[diag(numManExo)==1] <- 1e-6

#------------------------------------------------------------------------------
# Fit the model
ex1Run <- mxRun(Jor82Ex1)

summary(ex1Run)



#------------------------------------------------------------------------------
# Fitted Results

# The paper (Joreskog & Sorbom, 1982) reports correlation sacross
#  variables and standard deviations within variables
#  for the residual covariance matrices.
# This takes a covariance matrix (the matrix estimated,
#  turns it into a correlation matrix with the square root of
#  the variances (i.e. the standard deviations) down the diagonal.
TDf <- cov2cor(ex1Run$ThetaDelta$values)
diag(TDf) <- sqrt(diag(ex1Run$ThetaDelta$values))

PSf <- cov2cor(ex1Run$Psi$values)
diag(PSf) <- sqrt(diag(ex1Run$Psi$values))

# The other matrices are reported raw.
LXf <- ex1Run$LambdaX$values
GAf <- ex1Run$Gamma$values
BEf <- ex1Run$Beta$values


#------------------------------------------------------------------------------
# Published Results

TDp <- matrix(c(0.64, 0, -0.31, 0, 0.52, 0, -0.31, 0, 0.65), nrow=3, ncol=3)
LXp <- matrix(c(0.76, 0.85, 0.76), nrow=3, ncol=1)
GAp <- matrix(c(0.63, 0.21), nrow=2, ncol=1)
BEp <- matrix(c(0, 0.70, 0, 0), nrow=2, ncol=2)
PSp <- diag(c(0.78, 0.53))

#------------------------------------------------------------------------------
# Compare Fitted and Published Results

# Note: Because the published paper only reports
#  two decimals of precision, the comparison can only
#  be made down to two decimal places (i.e. epsilon=0.01)

omxCheckCloseEnough(TDf, TDp, epsilon=0.01)
omxCheckCloseEnough(LXf, LXp, epsilon=0.01)
omxCheckCloseEnough(GAf, GAp, epsilon=0.01)
omxCheckCloseEnough(BEf, BEp, epsilon=0.01)
omxCheckCloseEnough(PSf, PSp, epsilon=0.01)