Class SemIm
- All Implemented Interfaces:
Simulator
,ISemIm
,Im
,TetradSerializable
,Serializable
Let V be the set of variables in the model. The freeParameters of the model are as follows: (a) the list of linear coefficients for all edges u-->v in the model, where u, v are in V, (b) the list of variances for all variables in V, (c) the list of all error covariances d<->e, where d an e are exogenous terms in the model (either exogenous variables or error terms for endogenous variables), and (d) the list of means for all variables in V.
It is important to note that the likelihood functions this class calculates do not depend on variable means. They depend only on edge coefficients and error covariances. Hence, variable means are treated differently from edge coefficients and error covariances in the model.
Reference: Bollen, K. A. (1989). Structural Equations with Latent Variables. New York: John Wiley and Sons.
- Version:
- $Id: $Id
- Author:
- Frank Wimberly, Ricardo Silva, josephramsey
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCopy constructor.Constructs a new SEM IM from a SEM PM.SemIm
(SemPm semPm, ICovarianceMatrix covMatrix) Constructs a SEM model using the given SEM PM and sample covariance matrix.SemIm
(SemPm semPm, SemIm oldSemIm, Parameters parameters) Constructs a new SEM IM from the given SEM PM, using the old SEM IM and params object to guide the choice of parameter values.SemIm
(SemPm semPm, Parameters params) Constructs a new SEM IM from the given SEM PM, using the given params object to guide the choice of parameter values.SemIm
(SemPm semPm, List<Node> variableNodes, List<Node> measuredNodes, Matrix edgeCoef, double[] variableMeansStdDev) Constructor for SemIm. -
Method Summary
Modifier and TypeMethodDescriptionboolean
existsEdgeCoef
(Node x, Node y) existsEdgeCoef.double
getBicScore.double
getCfi()
getCfi.double
getChiSquare.Getter for the fieldedgeCoef
.double
getEdgeCoef
(Edge edge) Getter for the fieldedgeCoef
.double
getEdgeCoef
(Node x, Node y) Getter for the fieldedgeCoef
.Getter for the fielderrCovar
.double
getErrCovar
(Node x, Node y) Getter for the fielderrCovar
.double
getErrVar.Getter for the fieldfixedParameters
.Getter for the fieldfreeParameters
.double[]
getFreeParamValues.getImplCovar
(boolean recalculate) getImplCovar.getImplCovar
(List<Node> nodes) Getter for the fieldimplCovar
.getImplCovarMeas.double
getIntercept
(Node node) Calculates the intercept for a given node.double
Calculates the mean value associated with a givenNode
.double[]
getMeans()
getMeans.double
getMeanStdDev
(Node node) Calculates the mean standard deviation for the given node.The list of measured nodes for the semPm.int
getNumFixedParams.int
getNumFreeParams.int
Getter for the fieldnumRandomCalls
.getParameterNames.Getter for the fieldparams
.double
getParamValue
(Node nodeA, Node nodeB) getParamValue.double
getParamValue
(Parameter parameter) Retrieves the value associated with the given parameter.double
getPValue.double
getPValue.double
getRmsea()
getRmsea.getSampleCovar.int
Getter for the fieldsampleSize
.double
getScore()
The value of the maximum likelihood function for the getModel the model (Bollen 107).getSemPm()
Getter for the fieldsemPm
.double
getStandardError
(Parameter parameter, int maxFreeParams) getStandardError.double
getStdDev.double
getTotalEffect
(Node x, Node y) Calculates the total effect between two nodes.double
The negative of the log likelihood function for the getModel model, with the constant chopped off.double
getTValue.double[]
Getter for the fieldvariableMeans
.getVariableNode
(String name) getVariableNode.The list of measured and latent nodes for the semPm.double
getVariance
(Node node, Matrix implCovar) Returns the variance for a given node.void
Iterates through all freeParameters, picking values for them from the distributions that have been set for them.boolean
isCyclic()
isCyclic.boolean
isEstimated.boolean
isParameterBoundsEnforced.boolean
isSimulatedPositiveDataOnly.listUnmeasuredLatents.static SemIm
retainValues
(SemIm semIm, SemGraph graph) Constructs a new SEM IM with the given graph, retaining parameter values fromsemIm
for nodes of the same name and edges connecting nodes of the same names.static SemIm
Generates a simple exemplar of this class to test serialization.void
setCovMatrix
(ICovarianceMatrix covMatrix) Sets the sample covariance matrix for this Sem as a submatrix of the given matrix.void
setDataSet
(DataSet dataSet) Calculates the covariance matrix of the given DataSet and sets the sample covariance matrix for this model to a subset of it.void
setEdgeCoef
(Node x, Node y, double value) Sets the coefficient value for the edge between two nodes in the graph.void
setErrCovar
(Node x, double value) Setter for the fielderrCovar
.void
setErrCovar
(Node x, Node y, double value) Setter for the fielderrCovar
.void
Sets the error variance value for a specific node in the model's structural equation.void
setEstimated
(boolean estimated) Setter for the fieldestimated
.void
setFixedParamValue
(Parameter parameter, double value) Sets the fixed value for a specified parameter in the model.void
setFreeParamValues
(double[] params) Sets the values of the free freeParameters (in the order in which they appear in getFreeParameters()) to the values contained in the given array.void
setIntercept
(Node node, double intercept) Sets the intercept for a specified node in the SEM model.void
Sets the mean value for a given node in the variableNodes list.void
setMeanStandardDeviation
(Node node, double mean) Sets the mean associated with the given node.void
setParameterBoundsEnforced
(boolean parameterBoundsEnforced) setParameterBoundsEnforced.void
setParams
(Parameters params) Setter for the fieldparams
.void
setParamValue
(Node nodeA, Node nodeB, double value) setParamValue.void
setParamValue
(Parameter parameter, double value) Sets the value of a parameter in the model.void
setScoreType
(ScoreType scoreType) Setter for the fieldscoreType
.simulateData
(int sampleSize, boolean latentDataSaved) Simulates data from the model associated with this object.simulateDataCholesky
(int sampleSize, boolean latentDataSaved) Simulates data from this Sem using a Cholesky decomposition of the implied covariance matrix.simulateDataRecursive
(int sampleSize, boolean latentDataSaved) simulateDataRecursive.simulateDataReducedForm
(int sampleSize, boolean latentDataSaved) simulateDataReducedForm.simulateOneRecord.toString()
toString.updatedIm.
-
Constructor Details
-
SemIm
-
SemIm
Constructs a new SEM IM from the given SEM PM, using the given params object to guide the choice of parameter values.- Parameters:
semPm
- aSemPm
objectparams
- aParameters
object
-
SemIm
Constructs a new SEM IM from the given SEM PM, using the old SEM IM and params object to guide the choice of parameter values. If old values are retained, they are gotten from the old SEM IM.- Parameters:
semPm
- aSemPm
objectoldSemIm
- aSemIm
objectparameters
- aParameters
object
-
SemIm
Constructs a SEM model using the given SEM PM and sample covariance matrix.- Parameters:
semPm
- aSemPm
objectcovMatrix
- aICovarianceMatrix
object
-
SemIm
Copy constructor.- Parameters:
semIm
- aSemIm
object- Throws:
RuntimeException
- if the given SemIm cannot be serialized and deserialized correctly.
-
SemIm
-
-
Method Details
-
getParameterNames
-
retainValues
Constructs a new SEM IM with the given graph, retaining parameter values fromsemIm
for nodes of the same name and edges connecting nodes of the same names.- Parameters:
semIm
- The old SEM IM.graph
- The graph for the new SEM IM.- Returns:
- The new SEM IM, retaining values from
semIm
.
-
serializableInstance
-
updatedIm
-
setCovMatrix
Sets the sample covariance matrix for this Sem as a submatrix of the given matrix. The variable names used in the SemPm for this model must all appear in this CovarianceMatrix.- Parameters:
covMatrix
- aICovarianceMatrix
object
-
setDataSet
-
getSemPm
-
getFreeParamValues
public double[] getFreeParamValues()getFreeParamValues.
- Specified by:
getFreeParamValues
in interfaceISemIm
- Returns:
- an array containing the getModel values for the free freeParameters, in the order in which the freeParameters appear in getFreeParameters(). That is, getFreeParamValues()[i] is the value for getFreeParameters()[i].
-
setFreeParamValues
public void setFreeParamValues(double[] params) Sets the values of the free freeParameters (in the order in which they appear in getFreeParameters()) to the values contained in the given array. That is, params[i] is the value for getFreeParameters()[i].- Specified by:
setFreeParamValues
in interfaceISemIm
- Parameters:
params
- an array of objects
-
getParamValue
Retrieves the value associated with the given parameter.- Specified by:
getParamValue
in interfaceISemIm
- Parameters:
parameter
- The parameter for which to retrieve the value.- Returns:
- The value associated with the parameter.
- Throws:
NullPointerException
- if the parameter is null.IllegalArgumentException
- if the parameter is not present in the model.
-
setParamValue
Sets the value of a parameter in the model.- Specified by:
setParamValue
in interfaceISemIm
- Parameters:
parameter
- the parameter to set the value forvalue
- the value to set for the parameter- Throws:
IllegalArgumentException
- if the parameter cannot be set in this model
-
setFixedParamValue
Sets the fixed value for a specified parameter in the model.- Specified by:
setFixedParamValue
in interfaceISemIm
- Parameters:
parameter
- the parameter whose value is to be set. Must be aParameter
object.value
- the new value for the parameter. Must be a double.- Throws:
IllegalArgumentException
- if the parameter is not a fixed parameter in the model.
-
getErrVar
-
getErrCovar
-
getEdgeCoef
-
getEdgeCoef
-
setErrVar
Sets the error variance value for a specific node in the model's structural equation.- Specified by:
setErrVar
in interfaceISemIm
- Parameters:
x
- The node for which the error variance should be set.value
- The value to set as the error variance.- Throws:
NullPointerException
- If the given node is null.IllegalArgumentException
- If the given value is not a valid error variance.
-
setEdgeCoef
Sets the coefficient value for the edge between two nodes in the graph.- Specified by:
setEdgeCoef
in interfaceISemIm
- Parameters:
x
- The first node in the edge. Must be aNode
object.y
- The second node in the edge. Must be aNode
object.value
- The value of the coefficient. Must be a double.
-
existsEdgeCoef
-
setErrCovar
-
setErrCovar
-
setMean
-
setMeanStandardDeviation
-
setIntercept
Sets the intercept for a specified node in the SEM model.- Specified by:
setIntercept
in interfaceISemIm
- Parameters:
node
- aNode
object representing the nodeintercept
- a double value representing the new intercept to be set- Throws:
UnsupportedOperationException
- if the SEM model is cyclic
-
getIntercept
Calculates the intercept for a given node.- Specified by:
getIntercept
in interfaceISemIm
- Parameters:
node
- the node for which to calculate the intercept- Returns:
- the intercept value
-
getMean
-
getMeans
public double[] getMeans()getMeans.
- Returns:
- the means for variables in order.
-
getMeanStdDev
Calculates the mean standard deviation for the given node.- Specified by:
getMeanStdDev
in interfaceISemIm
- Parameters:
node
- the node for which to calculate the mean standard deviation- Returns:
- the mean standard deviation of the node
-
getVariance
Returns the variance for a given node.- Specified by:
getVariance
in interfaceISemIm
- Parameters:
node
- The node for which the variance is calculated. Must be aNode
object.implCovar
- The implementation covariance matrix. Must be aMatrix
object.- Returns:
- The variance value.
-
getStdDev
-
getParamValue
getParamValue.
Gets the value of a single free parameter to the given value, where the free parameter is specified by the endpoint nodes of its edge in the w graph. Note that coefficient freeParameters connect elements of getVariableNodes(), whereas variance and covariance freeParameters connect elements of getExogenousNodes(). (For variance freeParameters, nodeA and nodeB are the same.)
- Specified by:
getParamValue
in interfaceISemIm
- Parameters:
nodeA
- aNode
objectnodeB
- aNode
object- Returns:
- a double
-
setParamValue
setParamValue.
Sets the value of a single free parameter to the given value, where the free parameter is specified by the endpoint nodes of its edge in the graph. Note that coefficient freeParameters connect elements of getVariableNodes(), whereas variance and covariance freeParameters connect elements of getExogenousNodes(). (For variance freeParameters, nodeA and nodeB are the same.)
- Specified by:
setParamValue
in interfaceISemIm
- Parameters:
nodeA
- aNode
objectnodeB
- aNode
objectvalue
- a double
-
getFreeParameters
Getter for the field
freeParameters
.- Specified by:
getFreeParameters
in interfaceISemIm
- Returns:
- the (unmodifiable) list of free freeParameters in the model.
-
getNumFreeParams
public int getNumFreeParams()getNumFreeParams.
- Specified by:
getNumFreeParams
in interfaceISemIm
- Returns:
- the number of free freeParameters.
-
getFixedParameters
Getter for the field
fixedParameters
.- Specified by:
getFixedParameters
in interfaceISemIm
- Returns:
- the (unmodifiable) list of fixed freeParameters in the model.
-
getNumFixedParams
public int getNumFixedParams()getNumFixedParams.
- Returns:
- the number of free freeParameters.
-
getVariableNodes
The list of measured and latent nodes for the semPm. (Unmodifiable.)- Specified by:
getVariableNodes
in interfaceISemIm
- Returns:
- a
List
object
-
getMeasuredNodes
The list of measured nodes for the semPm. (Unmodifiable.)- Specified by:
getMeasuredNodes
in interfaceISemIm
- Returns:
- a
List
object
-
getSampleSize
public int getSampleSize()Getter for the field
sampleSize
.- Specified by:
getSampleSize
in interfaceISemIm
- Returns:
- the sample size (that is, the sample size of the CovarianceMatrix provided at construction time).
-
getEdgeCoef
Getter for the field
edgeCoef
.- Returns:
- a copy of the matrix of edge coefficients. Note that edgeCoefC[i][j] is the coefficient of the edge from getVariableNodes().get(i) to getVariableNodes().get(j), or 0.0 if this edge is not in the graph. The values of these may be changed, but the array itself may not.
-
getErrCovar
Getter for the field
errCovar
.- Returns:
- a copy of the matrix of error covariances. Note that errCovar[i][j] is the covariance of the error term of getExoNodes().get(i) and getExoNodes().get(j), with the special case (duh!) that errCovar[i][i] is the variance of getExoNodes.get(i). The values of these may be changed, but the array itself may not.
-
getImplCovar
getImplCovar.
- Specified by:
getImplCovar
in interfaceISemIm
- Parameters:
recalculate
- a boolean- Returns:
- a
Matrix
object
-
getImplCovarMeas
getImplCovarMeas.
- Specified by:
getImplCovarMeas
in interfaceISemIm
- Returns:
- a copy of the implied covariance matrix over the measured variables only.
-
getSampleCovar
getSampleCovar.
- Returns:
- a copy of the sample covariance matrix, or null if no sample covar has been set.
-
getScore
-
getTruncLL
public double getTruncLL()The negative of the log likelihood function for the getModel model, with the constant chopped off. (Bollen 134). This is an alternative, more efficient, optimization function to Fml which produces the same result when minimized.- Returns:
- a double
-
getBicScore
public double getBicScore()getBicScore.
- Specified by:
getBicScore
in interfaceISemIm
- Returns:
- BIC score, calculated as chisq - dof. This is equal to getFullBicScore() up to a constant.
-
getRmsea
-
getCfi
-
getChiSquare
public double getChiSquare()getChiSquare.
- Specified by:
getChiSquare
in interfaceISemIm
- Returns:
- the chi square value for the model.
-
getPValue
-
simulateData
Simulates data from the model associated with this object.This simulate method uses the implied covariance metrix directly to simulate data, instead of going tier by tier. It should work for cyclic graphs as well as acyclic graphs.
- Specified by:
simulateData
in interfaceSimulator
- Parameters:
sampleSize
- the number of rows to simulate.latentDataSaved
- if true, latent variables are saved in the data set.- Returns:
- the simulated data set.
-
setScoreType
-
simulateDataCholesky
Simulates data from this Sem using a Cholesky decomposition of the implied covariance matrix. This method works even when the underlying graph is cyclic.- Parameters:
sampleSize
- the number of rows of data to simulate.latentDataSaved
- True iff data for latents should be saved.- Returns:
- a
DataSet
object
-
simulateDataRecursive
-
simulateDataReducedForm
-
simulateOneRecord
-
initializeValues
public void initializeValues()Iterates through all freeParameters, picking values for them from the distributions that have been set for them. -
getStandardError
getStandardError.
- Specified by:
getStandardError
in interfaceISemIm
- Parameters:
parameter
- aParameter
objectmaxFreeParams
- a int- Returns:
- a double
-
listUnmeasuredLatents
listUnmeasuredLatents.
- Specified by:
listUnmeasuredLatents
in interfaceISemIm
- Returns:
- a
List
object
-
getTValue
-
getPValue
-
isParameterBoundsEnforced
public boolean isParameterBoundsEnforced()isParameterBoundsEnforced.
- Specified by:
isParameterBoundsEnforced
in interfaceISemIm
- Returns:
- a boolean
-
setParameterBoundsEnforced
public void setParameterBoundsEnforced(boolean parameterBoundsEnforced) setParameterBoundsEnforced.
- Specified by:
setParameterBoundsEnforced
in interfaceISemIm
- Parameters:
parameterBoundsEnforced
- a boolean
-
isEstimated
public boolean isEstimated()isEstimated.
- Specified by:
isEstimated
in interfaceISemIm
- Returns:
- a boolean
-
setEstimated
public void setEstimated(boolean estimated) Setter for the field
estimated
.- Parameters:
estimated
- a boolean
-
isCyclic
-
getVariableNode
getVariableNode.
- Parameters:
name
- aString
object- Returns:
- the variable by the given name, or null if none exists.
- Throws:
NullPointerException
- if name is null.
-
toString
-
getParams
-
setParams
Setter for the field
params
.- Parameters:
params
- aParameters
object
-
getVariableMeans
public double[] getVariableMeans()Getter for the field
variableMeans
.- Returns:
- an array of objects
-
isSimulatedPositiveDataOnly
public boolean isSimulatedPositiveDataOnly()isSimulatedPositiveDataOnly.
- Specified by:
isSimulatedPositiveDataOnly
in interfaceISemIm
- Returns:
- a boolean
-
getImplCovar
-
getNumRandomCalls
public int getNumRandomCalls()Getter for the field
numRandomCalls
.- Returns:
- a int
-
getTotalEffect
-