Learn R Programming

sem (version 3.1-16)

sem: General Structural Equation Models

Description

sem fits general structural equation models (with both observed and unobserved variables). Observed variables are also called indicators or manifest variables; unobserved variables are also called factors or latent variables. Normally, the generic function (sem) is called directly with a semmod first argument produced by specifyModel, specifyEquations, or cfa, invoking the sem.semmod method, which in turn sets up a call to the sem.default method; thus, the user may wish to specify arguments accepted by the semmod and default methods. Similarly, for a multigroup model, sem would normally be called with a semmodList object produced by multigroupModel as its first argument, and would then generate a call to the code msemmod method.

Usage

# S3 method for semmod
sem(model, S, N, data, 
    raw=identical(na.action, na.pass), obs.variables=rownames(S), 
  	fixed.x=NULL, formula= ~ ., na.action=na.omit, 
    robust=!missing(data), debug=FALSE, 
    optimizer=optimizerSem, objective=objectiveML, ...)
    
# S3 method for default
sem(model, S, N, raw=FALSE, data=NULL, start.fn=startvalues,
    pattern.number=NULL, valid.data.patterns=NULL,
    use.means=TRUE, param.names, 
  	var.names, fixed.x=NULL, robust=!is.null(data), semmod=NULL, debug=FALSE,
		analytic.gradient=!identical(objective, objectiveFIML), 
        warn=FALSE, maxiter=1000, par.size=c("ones", "startvalues"), 
		start.tol=1E-6, optimizer=optimizerSem, objective=objectiveML, cls, ...)
		
# S3 method for semmodList
sem(model, S, N, data, raw=FALSE, fixed.x=NULL, 
		robust=!missing(data), formula, group="Group", debug=FALSE, ...)
		
# S3 method for msemmod
sem(model, S, N, start.fn=startvalues,
        group="Group", groups=names(model), raw=FALSE, fixed.x, 
		param.names, var.names, debug=FALSE, analytic.gradient=TRUE, warn=FALSE,
		maxiter=5000, par.size = c("ones", "startvalues"), start.tol = 1e-06, 
		start=c("initial.fit", "startvalues"), initial.maxiter=1000,
		optimizer = optimizerMsem, objective = msemObjectiveML, ...)
    
startvalues(S, ram, debug=FALSE, tol=1E-6)
startvalues2(S, ram, debug=FALSE, tol=1E-6)

# S3 method for sem coef(object, standardized=FALSE, ...) # S3 method for msem coef(object, ...) # S3 method for sem vcov(object, robust=FALSE, analytic=inherits(object, "objectiveML") && object$t

Value

sem returns an object of class c(objective, "sem"), where objective

is the name of the objective function that was optimized (e.g., "objectiveML"), with the following elements:

var.names

vector of variable names.

ram

RAM matrix, including any rows generated for covariances among fixed exogenous variables; column 5 includes computed start values.

S

observed covariance matrix.

J

RAM selection matrix, \(J\), which picks out observed variables.

n.fix

number of fixed exogenous variables.

n

number of observed variables.

N

number of observations.

m

number of variables (observed plus unobserved).

t

number of free parameters.

raw

TRUE if the model is fit to a raw moment matrix, FALSE otherwise.

data

the observed-variable data matrix, or NULL if data are not supplied.

semmod

the semmod specification object for the model, if one was supplied; otherwise NULL.

optimizer

the optimizer function.

objective

the objective function.

coeff

estimates of free parameters.

vcov

estimated asymptotic covariance matrix of parameter estimates, based on a numeric Hessian, if supplied by the optimizer; otherwise NULL.

par.posn

indices of free parameters.

convergence

TRUE or FALSE, depending upon whether the optimization apparently converged.

iterations

number of iterations performed.

criterion

value of the objective function at the minimum.

C

model-reproduced covariance matrix.

A

RAM \(A\) matrix.

P

RAM \(P\) matrix.

adj.obj

robust adjusted value of the objective function; NULL if robust is FALSE.

robust.vcov

robust estimated coefficient covariance matrix; NULL if robust is FALSE.

For multigroup models, sem returns an object of class c("msemObjectiveML", "msem").

Arguments

model

RAM specification, which is a simple encoding of the path diagram for the model. The model may be given either in symbolic form (as a semmod object, as returned by the specifyModel, specifyEquations, or cfa function, or as a character matrix), invoking sem.semmod, which calls sem.default after setting up the model, or (less conveniently) in numeric form, invoking sem.default directly, which is not recommended (see Details below). The model argument may also be a multigroup-model specification, as produced by multigroupModel.

S

covariance matrix among observed variables; may be input as a symmetric matrix, or as a lower- or upper-triangular matrix. S may also be a raw (i.e., “uncorrected”) moment matrix --- that is, a sum-of-squares-and-products matrix divided by N. This form of input is useful for fitting models with intercepts, in which case the moment matrix should include the mean square and cross-products for a unit variable all of whose entries are 1; of course, the raw mean square for the unit variable is 1. Raw-moment matrices may be computed by rawMoments. If the ram argument is given in symbolic form, then the observed-variable covariance or raw-moment matrix may contain variables that do not appear in the model, in which case a warning is printed. S may also be a list of covariance or moment matrices for each group in a multigroup model. As an alternative to specifying S the user may supply a data frame containing the data for the model (see the argument data).

N

number of observations on which the covariance matrix is based; for a multigroup model, a vector of group \(N\)s.

data

As a generally preferable alternative to specifying S and N, the user may supply a data frame containing the data to which the model is to be fit. In a multigroup model, the data argument may be a list of data frames or a single data frame; in the later event, the factor given as the group argument is used to split the data into groups.

start.fn

a function to compute startvalues for the free parameters of the model; two functions are supplied, startvalues and a older version, startvalues2, the first of which is the default.

na.action

a function to process missing data, if raw data are supplied in the data argument. The default is na.omit, which returns only complete cases; specify na.action=na.pass to get FIML estimates in the presence of missing data from the objectiveFIML and objectiveFIML2 objective functions.

raw

TRUE if S is a raw moment matrix or if a raw moment matrix --- as opposed to a covariance matrix --- is to be computed from data; the default is FALSE unless the na.action argument is set to na.pass.

pattern.number, valid.data.patterns

these arguments pass information about valid (i.e., non-missing) data patterns and normally would not be specified directly by the user.

use.means

When raw data are supplied and intercepts are included in the model, use the observed-variable means as start values for the intercepts; the default is TRUE.

obs.variables

names of observed variables, by default taken from the row names of the covariance or moment matrix S, which may be given directly or generated according to the data and formula arguments.

fixed.x

names (if the ram matrix is given in symbolic form) or indices (if it is in numeric form) of fixed exogenous variables. Specifying these obviates the necessity of having to fix the variances and covariances among these variables (and produces correct degrees of freedom for the model chisquare).

formula

a one-sided formula, to be applied to data to generate the variables for which covariances or raw moments are computed. The default formula is ~., i.e., all of the variables in the data, including an implied intercept; if a covariance matrix is to be computed, the constant is suppressed. In a multigroup model, alternatively a list one one-sided formulas as be given, to be applied individually to the groups.

robust

In sem: if TRUE, then quantities are calculated that can be used to compute robust estimates of coefficient standard errors and robust tests when the model is fit by multinormal maximum likelihood; the default is TRUE when the data argument is TRUE, and this option is only available when the data argument is given. In vcov: if TRUE, return a robust coefficient covariance matrix (if object contains the requisite information).

semmod

a semmod object containing the description of the model; optional, and normally supplied not directly by the user but via the semmod method for sem.

debug

if TRUE, some information is printed to help you debug the symbolic model specification; for example, if a variable name is misspelled, sem will assume that the variable is a (new) latent variable. Information about the optimization will also be printed, but details will vary with the optimizer employed. The default is FALSE.

...

arguments to be passed down, including from sem.default to the optimizer.

param.names

names of the \(t\) free parameters, given in their numerical order; default names are Param1, ..., Paramt. Note: Should not be specified when the model is given in symbolic form.

var.names

names of the \(m\) entries of the \(v\) vector (typically the observed and latent variables --- see below), given in their numerical order; default names are Var1, ..., Varm. Note: Should not be specified when the model is given in symbolic form.

analytic.gradient

if TRUE (the default, except for the objectiveFIML objective function, where, at present, an analytic gradient slows down the computation), then analytic first derivatives are used in the maximization of the likelihood if the optimzer employed will accept them; otherwise numeric derivatives are used, again if the optimizer will compute them.

warn

if TRUE, warnings produced by the optimization function will be printed. This should generally not be necessary, since sem prints its own warning, and saves information about convergence. The default is FALSE.

maxiter

the maximum number of iterations for the optimization of the objective function, to be passed to the optimizer.

par.size

the anticipated size of the free parameters; if "ones", a vector of ones is used; if "startvalues", taken from the start values. You can try changing this argument if you encounter convergence problems. The default is "startvalues" if the largest input variance is at least 100 times the smallest, and "ones" otherwise. Whether this argument is actually used depends upon the optimizer employed.

start.tol, tol

if the magnitude of an automatic start value is less than start.tol, then it is set to start.tol; defaults to 1E-6.

optimizer

a function to be used to minimize the objective function; the default for single-group models is optimizerSem. Alternatives are nlm, which employs the standard R optimizer nlm; optimizerOptim, which employs optim; and optimizerNlminb, which uses nlminb --- or the user can supply an optimizer. For multigroup model, the default is optimizerMsem, and msemOptimizerNlm, based on nlm, is provided as an alternative.

objective

An objective function to be minimized, sometimes called a “fit” function in the SEM literature. The default for single-group models is objectiveML, which produces maximum-likelihood estimates assuming multinormality. An alternative is objectiveGLS, which produced generalized least squares estimates --- or the user can supply an objective function to be minimized. For multigroup models, the default is available is msemObjectiveML for ML estimates and an alternative is msemObjectiveGLS for GLS estiamtes.

cls

primary class to be assigned to the result; normally this is not specified directly, but raither is inferred from the objective function.

ram

numeric RAM matrix.

object

an object of class "sem" or "msem", returned by sem.

standardized

if TRUE, return standardized coefficients.

analytic

return an analytic (as opposed to numeric) estimate of the coefficient covariance matrix; at present only available for the objectiveML objective function. The default is FALSE for this objective function if there are no more than 100 parameters and FALSE otherwise.

group

for a multigroup model, the quoted name of the group variable; if the data argument is given, snd is a single data frame, then this should be a factor in the data set or a variable coercible to a factor, to be used to split the data into groups; otherwise, the name is arbitrary.

groups

a character vector giving the names of the groups; will be ignored if group is a factor in data.

start

if "initial.fit" (the default), start values for a multi-group model are computed by first fitting the intra-group models separately by group; if "startvalues", then start values are computed as for a single-group model. In some cases, the intra-group models may not be identified even if the multi-group model is, and then start="initial.fit" should not be used.

initial.maxiter

if start="initial.fit" for a multi-group model, then initial.maxiter gives the maximum number of iterations for each initial intra-group fit.

Warning

A common error is to fail to specify variance or covariance terms in the model, which are denoted by double-headed arrows, <->.

In general, every observed or latent variable in the model should be associated with a variance or error variance. This may be a free parameter to estimate or a fixed constant (as in the case of a latent exogenous variable for which you wish to fix the variance, e.g., to 1). Again in general, there will be an error variance associated with each endogenous variable in the model (i.e., each variable to which at least one single-headed arrow points --- including observed indicators of latent variables), and a variance associated with each exogenous variable (i.e., each variable that appears only at the tail of single-headed arrows, never at the head).

To my knowledge, the only apparent exception to this rule is for observed variables that are declared to be fixed exogenous variables. In this case, the program generates the necessary (fixed-constant) variances and covariances automatically.

If there are missing variances, a warning message will be printed, and estimation will almost surely fail in some manner. Missing variances might well indicate that there are missing covariances too, but it is not possible to deduce this in a mechanical manner. The specifyModel funciton will by default supply error-variance parameters if these are missing.

Author

John Fox jfox@mcmaster.ca, Zhenghua Nie, and Jarrett Byrnes

Details

The model is set up using either RAM (“reticular action model” -- don't ask!) notation -- a simple format for specifying general structural equation models by coding the “arrows” in the path diagram for the model (see, e.g., McArdle and McDonald, 1984) -- typically using the specifyModel function; in equation format using the specifyEquations function; or, for a simple confirmatory factor analysis model, via the cfa function. In any case, the model is represented internally in RAM format.

The variables in the \(v\) vector in the model (typically, the observed and unobserved variables, but not error variables) are numbered from 1 to \(m\). the RAM matrix contains one row for each (free or constrained) parameter of the model, and may be specified either in symbolic format or in numeric format.

A symbolic ram matrix consists of three columns, as follows:

1. Arrow specification:

This is a simple formula, of the form "A -> B" or, equivalently, "B <- A" for a regression coefficient (i.e., a single-headed or directional arrow); "A <-> A" for a variance or "A <-> B" for a covariance (i.e., a double-headed or bidirectional arrow). Here, A and B are variable names in the model. If a name does not correspond to an observed variable, then it is assumed to be a latent variable. Spaces can appear freely in an arrow specification, and there can be any number of hyphens in the arrows, including zero: Thus, e.g., "A->B", "A --> B", and "A>B" are all legitimate and equivalent.

2. Parameter name:

The name of the regression coefficient, variance, or covariance specified by the arrow. Assigning the same name to two or more arrows results in an equality constraint. Specifying the parameter name as NA produces a fixed parameter.

3. Value:

start value for a free parameter or value of a fixed parameter. If given as NA, sem will compute the start value.

It is simplest to construct the RAM matrix with the specifyModel, specifyEquations, or cfa function, all of which return an object of class semmod, and also incorporate some model-specification convenience shortcuts. This process is illustrated in the examples below.

A numeric ram matrix consists of five columns, as follows:

1. Number of arrow heads:

1 (directed arrow) or 2 (covariance).

2. Arrow to:

index of the variable at the head of a directional arrow, or at one end of a bidirectional arrow. Observed variables should be assigned the numbers 1 to \(n\), where \(n\) is the number of rows/columns in the covariance matrix S, with the indices corresponding to the variables' positions in S. Variable indices above \(n\) represent latent variables.

3. Arrow from:

the index of the variable at the tail of a directional arrow, or at the other end of a bidirectional arrow.

4. Parameter number:

free parameters are numbered from 1 to \(t\), but do not necessarily appear in consecutive order. Fixed parameters are given the number 0. Equality contraints are specified by assigning two or more parameters the same number.

5. Value:

start value for a free parameter, or value of a fixed parameter. If given as NA, the program will compute a start value, by a slight modification of the method described by McDonald and Hartmann (1992). Note: In some circumstances, some start values are selected randomly; this might produce small differences in the parameter estimates when the program is rerun.

The numeric ram matrix is normally generated automatically, not specified directly by the user.

For specifyEquations, each input line is either a regression equation or the specification of a variance or covariance. Regression equations are of the form

y = par1*x1 + par2*x2 + ... + park*xk

where y and the xs are variables in the model (either observed or latent), and the pars are parameters. If a parameter is given as a numeric value (e.g., 1) then it is treated as fixed. Note that no “error” variable is included in the equation; “error variances” are specified via either the covs argument, via V(y) = par (see immediately below), or are added automatically to the model when, as by default, endog.variances=TRUE.

Variances are specified in the form V(var) = par and covariances in the form C(var1, var2) = par, where the vars are variables (observed or unobserved) in the model. The symbols V and C may be in either lower- or upper-case. If par is a numeric value (e.g., 1) then it is treated as fixed. In conformity with the RAM model, a variance or covariance for an endogenous variable in the model is an “error” variance or covariance.

To set a start value for a free parameter, enclose the numeric start value in parentheses after the parameter name, as parameter(value).

sem fits the model by calling the optimizer specified in the optimizer argument to minimize the objective function specified in the objective argument. If the optimization fails to converge, a warning message is printed.

The RAM formulation of the general structural equation model is given by the basic equation $$v = Av + u$$ where \(v\) and \(u\) are vectors of random variables (observed or unobserved), and the parameter matrix \(A\) contains regression coefficients, symbolized by single-headed arrows in a path diagram. Another parameter matrix, $$P = E(uu')$$ contains covariances among the elements of \(u\) (assuming that the elements of \(u\) have zero means). Usually \(v\) contains endogenous and exogenous observed and unobserved variables, but not error variables (see the examples below).

The startvalues function may be called directly, but is usually called by sem.default; startvalues2 is an older version of this function that may be used alternatively; see the startvalues argument to sem.

References

Fox, J. (2006) Structural equation modeling with the sem package in R. Structural Equation Modeling 13:465--486.

Bollen, K. A. (1989) Structural Equations With Latent Variables. Wiley.

Bollen, K. A. and Long, J. S. (eds.) Testing Structural Equation Models, Sage.

McArdle, J. J. and Epstein, D. (1987) Latent growth curves within developmental structural equation models. Child Development 58, 110--133.

McArdle, J. J. and McDonald, R. P. (1984) Some algebraic properties of the reticular action model. British Journal of Mathematical and Statistical Psychology 37, 234--251.

McDonald, R. P. and Hartmann, W. M. (1992) A procedure for obtaining initial values of parameters in the RAM model. Multivariate Behavioral Research 27, 57--76.

Raftery, A. E. (1993) Bayesian model selection in structural equation models. In Bollen, K. A. and Long, J. S. (eds.) Testing Structural Equation Models, Sage.

Raftery, A. E. (1995) Bayesian model selection in social research (with discussion). Sociological Methodology 25, 111--196.

Satorra, A. (2000) Scaled and adjusted restricted tests in multi-sample analysis of moment structures. pp. 233--247 in Heijmans, R.D.H., Pollock, D.S.G. & Satorra, A. (eds.) Innovations in Multivariate Statistical Analysis. A Festschrift for Heinz Neudecker , Kluwer.

See Also

rawMoments, startvalues, objectiveML, objectiveGLS, optimizerNlm, optimizerOptim, optimizerNlminb, nlm, optim, nlminb, specifyModel, specifyEquations, cfa