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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents the result of a cyclic simulation.static final recordRepresents the result of a computation or analysis, encapsulating the following: - The shrinkage mode used in an independent test. -
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 TypeMethodDescriptionbooleanexistsEdgeCoef(Node x, Node y) Determines whether an edge coefficient exists between two given nodes.doublegetBicScore.doublegetCfi()getCfi.doublegetChiSquare.Getter for the fieldedgeCoef.doublegetEdgeCoef(Edge edge) Getter for the fieldedgeCoef.doublegetEdgeCoef(Node x, Node y) Getter for the fieldedgeCoef.Getter for the fielderrCovar.doublegetErrCovar(Node x, Node y) Getter for the fielderrCovar.doublegetErrVar.Getter for the fieldfixedParameters.Getter for the fieldfreeParameters.double[]getFreeParamValues.getImplCovar(boolean recalculate) getImplCovar.getImplCovar(List<Node> nodes) Getter for the fieldimplCovar.getImplCovarMeas.doublegetIntercept(Node node) Calculates the intercept for a given node.doubleCalculates the mean value associated with a givenNode.double[]getMeans()getMeans.doublegetMeanStdDev(Node node) Calculates the mean standard deviation for the given node.The list of measured nodes for the semPm.intgetNumFixedParams.intgetNumFreeParams.intGetter for the fieldnumRandomCalls.getParameterNames.Getter for the fieldparams.doublegetParamValue(Node nodeA, Node nodeB) getParamValue.doublegetParamValue(Parameter parameter) Retrieves the value associated with the given parameter.doublegetPValue.doublegetPValue.doublegetRmsea()getRmsea.getSampleCovar.intGetter for the fieldsampleSize.doublegetScore()The value of the maximum likelihood function for the getModel the model (Bollen 107).getSemPm()Getter for the fieldsemPm.doublegetStandardError(Parameter parameter, int maxFreeParams) getStandardError.doublegetStdDev.doublegetTotalEffect(Node x, Node y) Calculates the total effect between two nodes.doubleThe negative of the log likelihood function for the getModel model, with the constant chopped off.doublegetTValue.double[]Getter for the fieldvariableMeans.getVariableNode(String name) getVariableNode.The list of measured and latent nodes for the semPm.doublegetVariance(Node node, Matrix implCovar) Returns the variance for a given node.voidIterates through all freeParameters, picking values for them from the distributions that have been set for them.booleanisCyclic()isCyclic.booleanisEstimated.booleanisParameterBoundsEnforced.booleanisSimulatedPositiveDataOnly.listUnmeasuredLatents.static SemImretainValues(SemIm semIm, SemGraph graph) Constructs a new SEM IM with the given graph, retaining parameter values fromsemImfor nodes of the same name and edges connecting nodes of the same names.static SemImGenerates a simple exemplar of this class to test serialization.voidsetCovMatrix(ICovarianceMatrix covMatrix) Sets the sample covariance matrix for this Sem as a submatrix of the given matrix.voidsetDataSet(DataSet dataSet) Calculates the covariance matrix of the given DataSet and sets the sample covariance matrix for this model to a subset of it.voidsetEdgeCoef(Node x, Node y, double value) Sets the coefficient value for the edge between two nodes in the graph.voidsetErrCovar(Node x, double value) Deprecated.Use setErrVar(x, value) for variances, or setErrCovar(x, y, value) for covariances.voidsetErrCovar(Node x, Node y, double value) Setter for the fielderrCovar.voidSets the error variance value for a specific node in the model's structural equation.voidsetEstimated(boolean estimated) Setter for the fieldestimated.voidsetFixedParamValue(Parameter parameter, double value) Sets the fixed value for a specified parameter in the model.voidsetFreeParamValues(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.voidsetIntercept(Node node, double intercept) Sets the intercept for a specified node in the SEM model.voidSets the mean value for a given node in the variableNodes list.voidsetMeanStandardDeviation(Node node, double stdDev) Sets the standard deviation value for the specified node.voidsetParameterBoundsEnforced(boolean parameterBoundsEnforced) setParameterBoundsEnforced.voidsetParams(Parameters params) Setter for the fieldparams.voidsetParamValue(Node nodeA, Node nodeB, double value) setParamValue.voidsetParamValue(Parameter parameter, double value) Sets the value of a parameter in the model.voidsetScoreType(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.static @NotNull SemIm.ResultsimulatePossibleShrinkage(Parameters params, Graph g) Simulates possible shrinkage scenarios for a given graph and parameters.toString()toString.updatedIm.
-
Constructor Details
-
SemIm
Constructs a new SEM IM from a SEM PM.- Parameters:
semPm- aSemPmobject
-
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- aSemPmobjectparams- aParametersobject
-
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- aSemPmobjectoldSemIm- aSemImobjectparameters- aParametersobject
-
SemIm
Constructs a SEM model using the given SEM PM and sample covariance matrix.- Parameters:
semPm- aSemPmobjectcovMatrix- aICovarianceMatrixobject
-
SemIm
Copy constructor.- Parameters:
semIm- aSemImobject- Throws:
RuntimeException- if the given SemIm cannot be serialized and deserialized correctly.
-
SemIm
public SemIm(SemPm semPm, List<Node> variableNodes, List<Node> measuredNodes, Matrix edgeCoef, double[] variableMeansStdDev) Constructor for SemIm.
-
-
Method Details
-
getParameterNames
getParameterNames.
- Returns:
- a
Listobject
-
retainValues
Constructs a new SEM IM with the given graph, retaining parameter values fromsemImfor 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
Generates a simple exemplar of this class to test serialization.- Returns:
- a
SemImobject
-
simulatePossibleShrinkage
Simulates possible shrinkage scenarios for a given graph and parameters. This method determines the shrinkage mode, validates input parameters, identifies the cyclic coefficient style, and simulates data accordingly.- Parameters:
params- The input parameters containing configuration for shrinkage mode, cyclic coefficient style, regularization lambda, and other simulation parameters.g- The graph on which the shrinkage simulation is performed.- Returns:
- A Result object containing the shrinkage mode, simulated dataset, sample size, and the SEM (Structural Equation Model) instance representing the data-generating process.
-
updatedIm
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- aICovarianceMatrixobject
-
setDataSet
Calculates the covariance matrix of the given DataSet and sets the sample covariance matrix for this model to a subset of it. The measured variable names used in the SemPm for this model must all appear in this data set.- Parameters:
dataSet- aDataSetobject
-
getSemPm
Getter for the field
semPm. -
getFreeParamValues
public double[] getFreeParamValues()getFreeParamValues.
- Specified by:
getFreeParamValuesin 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:
setFreeParamValuesin interfaceISemIm- Parameters:
params- an array of objects
-
getParamValue
Retrieves the value associated with the given parameter.- Specified by:
getParamValuein 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:
setParamValuein 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:
setFixedParamValuein interfaceISemIm- Parameters:
parameter- the parameter whose value is to be set. Must be aParameterobject.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
getErrVar.
- Parameters:
x- aNodeobject- Returns:
- a double
-
getErrCovar
Getter for the field
errCovar. -
getEdgeCoef
Getter for the field
edgeCoef. -
getEdgeCoef
Getter for the field
edgeCoef.- Parameters:
edge- aEdgeobject- Returns:
- a double
-
setErrVar
Sets the error variance value for a specific node in the model's structural equation.- Specified by:
setErrVarin 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:
setEdgeCoefin interfaceISemIm- Parameters:
x- The first node in the edge. Must be aNodeobject.y- The second node in the edge. Must be aNodeobject.value- The value of the coefficient. Must be a double.
-
existsEdgeCoef
Determines whether an edge coefficient exists between two given nodes.- Parameters:
x- the first node to checky- the second node to check- Returns:
- true if there exists a coefficient parameter for the edge from x to y, false otherwise
-
setErrCovar
Deprecated.Use setErrVar(x, value) for variances, or setErrCovar(x, y, value) for covariances.Sets the error covariance value for the specified node. This method is deprecated and may be removed in future versions. Use alternative methods for setting error covariance values.- Parameters:
x- the target node for which the error covariance value is to be setvalue- the error covariance value to assign
-
setErrCovar
Setter for the field
errCovar. -
setMean
Sets the mean value for a given node in the variableNodes list. -
setMeanStandardDeviation
Sets the standard deviation value for the specified node.- Parameters:
node- The node for which the standard deviation is being set.stdDev- The standard deviation value to be assigned to the provided node.
-
setIntercept
Sets the intercept for a specified node in the SEM model.- Specified by:
setInterceptin interfaceISemIm- Parameters:
node- aNodeobject 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:
getInterceptin interfaceISemIm- Parameters:
node- the node for which to calculate the intercept- Returns:
- the intercept value
-
getMean
Calculates the mean value associated with a givenNode. -
getMeans
public double[] getMeans()getMeans.
- Returns:
- the means for variables in order.
-
getMeanStdDev
Calculates the mean standard deviation for the given node.- Specified by:
getMeanStdDevin 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:
getVariancein interfaceISemIm- Parameters:
node- The node for which the variance is calculated. Must be aNodeobject.implCovar- The implementation covariance matrix. Must be aMatrixobject.- Returns:
- The variance value.
-
getStdDev
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:
getParamValuein interfaceISemIm- Parameters:
nodeA- aNodeobjectnodeB- aNodeobject- 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:
setParamValuein interfaceISemIm- Parameters:
nodeA- aNodeobjectnodeB- aNodeobjectvalue- a double
-
getFreeParameters
Getter for the field
freeParameters.- Specified by:
getFreeParametersin interfaceISemIm- Returns:
- the (unmodifiable) list of free freeParameters in the model.
-
getNumFreeParams
public int getNumFreeParams()getNumFreeParams.
- Specified by:
getNumFreeParamsin interfaceISemIm- Returns:
- the number of free freeParameters.
-
getFixedParameters
Getter for the field
fixedParameters.- Specified by:
getFixedParametersin 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:
getVariableNodesin interfaceISemIm- Returns:
- a
Listobject
-
getMeasuredNodes
The list of measured nodes for the semPm. (Unmodifiable.)- Specified by:
getMeasuredNodesin interfaceISemIm- Returns:
- a
Listobject
-
getSampleSize
public int getSampleSize()Getter for the field
sampleSize.- Specified by:
getSampleSizein 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:
getImplCovarin interfaceISemIm- Parameters:
recalculate- a boolean- Returns:
- a
Matrixobject
-
getImplCovarMeas
getImplCovarMeas.
- Specified by:
getImplCovarMeasin 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
public double getScore()The value of the maximum likelihood function for the getModel the model (Bollen 107). To optimize, this should be minimized. -
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:
getBicScorein interfaceISemIm- Returns:
- BIC score, calculated as chisq - dof. This is equal to getFullBicScore() up to a constant.
-
getRmsea
public double getRmsea()getRmsea.
-
getCfi
public double getCfi()getCfi.
-
getChiSquare
public double getChiSquare()getChiSquare.
- Specified by:
getChiSquarein interfaceISemIm- Returns:
- the chi square value for the model.
-
getPValue
public double getPValue()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:
simulateDatain 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
Setter for the field
scoreType.- Parameters:
scoreType- aScoreTypeobject
-
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
DataSetobject
-
simulateDataRecursive
simulateDataRecursive.
- Parameters:
sampleSize- a intlatentDataSaved- a boolean- Returns:
- a
DataSetobject
-
simulateDataReducedForm
simulateDataReducedForm.
- Parameters:
sampleSize- a intlatentDataSaved- a boolean- Returns:
- a
DataSetobject
-
simulateOneRecord
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:
getStandardErrorin interfaceISemIm- Parameters:
parameter- aParameterobjectmaxFreeParams- a int- Returns:
- a double
-
listUnmeasuredLatents
listUnmeasuredLatents.
- Specified by:
listUnmeasuredLatentsin interfaceISemIm- Returns:
- a
Listobject
-
getTValue
getTValue.
-
getPValue
getPValue.
-
isParameterBoundsEnforced
public boolean isParameterBoundsEnforced()isParameterBoundsEnforced.
- Specified by:
isParameterBoundsEnforcedin interfaceISemIm- Returns:
- a boolean
-
setParameterBoundsEnforced
public void setParameterBoundsEnforced(boolean parameterBoundsEnforced) setParameterBoundsEnforced.
- Specified by:
setParameterBoundsEnforcedin interfaceISemIm- Parameters:
parameterBoundsEnforced- a boolean
-
isEstimated
public boolean isEstimated()isEstimated.
- Specified by:
isEstimatedin interfaceISemIm- Returns:
- a boolean
-
setEstimated
public void setEstimated(boolean estimated) Setter for the field
estimated.- Parameters:
estimated- a boolean
-
isCyclic
public boolean isCyclic()isCyclic.
-
getVariableNode
getVariableNode.
- Parameters:
name- aStringobject- Returns:
- the variable by the given name, or null if none exists.
- Throws:
NullPointerException- if name is null.
-
toString
toString.
-
getParams
Getter for the field
params.- Returns:
- a
Parametersobject
-
setParams
Setter for the field
params.- Parameters:
params- aParametersobject
-
getVariableMeans
public double[] getVariableMeans()Getter for the field
variableMeans.- Returns:
- an array of objects
-
isSimulatedPositiveDataOnly
public boolean isSimulatedPositiveDataOnly()isSimulatedPositiveDataOnly.
- Specified by:
isSimulatedPositiveDataOnlyin interfaceISemIm- Returns:
- a boolean
-
getImplCovar
Getter for the field
implCovar. -
getNumRandomCalls
public int getNumRandomCalls()Getter for the field
numRandomCalls.- Returns:
- a int
-
getTotalEffect
Calculates the total effect between two nodes.- Parameters:
x- the source nodey- the target node- Returns:
- the total effect from node x to node y
-