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.
- 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) -
Method Summary
Modifier and TypeMethodDescriptionboolean
existsEdgeCoef
(Node x, Node y) double
double
getCfi()
double
double
getEdgeCoef
(Edge edge) double
getEdgeCoef
(Node x, Node y) double
getErrCovar
(Node x, Node y) double
double[]
getImplCovar
(boolean recalculate) getImplCovar
(List<Node> nodes) double
getIntercept
(Node node) double
double[]
getMeans()
double
getMeanStdDev
(Node node) The list of measured nodes for the semPm.int
int
int
double
getParamValue
(Node nodeA, Node nodeB) 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.double
getParamValue
(Parameter parameter) Gets the value of a single free parameter, or Double.NaN if the parameter is not in thisdouble
double
double
getRmsea()
int
double
getScore()
The value of the maximum likelihood function for the getModel the model (Bollen 107).getSemPm()
double
getStandardError
(Parameter parameter, int maxFreeParams) double
double
The negative of the log likelihood function for the getModel model, with the constant chopped off.double
double[]
getVariableNode
(String name) The list of measured and latent nodes for the semPm.double
getVariance
(Node node, Matrix implCovar) void
Iterates through all freeParameters, picking values for them from the distributions that have been set for them.boolean
isCyclic()
boolean
boolean
boolean
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) void
setErrCovar
(Node x, double value) void
setErrCovar
(Node x, Node y, double value) void
void
setEstimated
(boolean estimated) void
setFixedParamValue
(Parameter parameter, double value) Sets the value of a single free parameter to the given value.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.void
Sets the mean associated with the given node.void
setMeanStandardDeviation
(Node node, double mean) Sets the mean associated with the given node.void
setParameterBoundsEnforced
(boolean parameterBoundsEnforced) void
setParams
(Parameters params) void
setParamValue
(Node nodeA, Node nodeB, double value) 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.void
setParamValue
(Parameter parameter, double value) Sets the value of a single free parameter to the given value.void
setScoreType
(ScoreType scoreType) simulateData
(int sampleSize, boolean latentDataSaved) This simulate method uses the implied covariance metrix directly to simulate data, instead of going tier by tier.simulateDataCholesky
(int sampleSize, boolean latentDataSaved) Simulates data from this Sem using a Cholesky decomposition of the implied covariance matrix.simulateDataRecursive
(int sampleSize, boolean latentDataSaved) simulateDataReducedForm
(int sampleSize, boolean latentDataSaved) toString()
-
Constructor Details
-
SemIm
Constructs a new SEM IM from a SEM PM. -
SemIm
Constructs a new SEM IM from the given SEM PM, using the given params object to guide the choice of parameter values. -
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. -
SemIm
Constructs a SEM model using the given SEM PM and sample covariance matrix. -
SemIm
Copy constructor.- 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
Generates a simple exemplar of this class to test serialization. -
updatedIm
- Returns:
- a variant of the getModel model with the given covariance matrix and means. Used for updating.
-
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. -
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. -
getSemPm
-
getFreeParamValues
public double[] 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
-
getParamValue
Gets the value of a single free parameter, or Double.NaN if the parameter is not in this- Specified by:
getParamValue
in interfaceISemIm
- Throws:
IllegalArgumentException
- if the given parameter is not a free parameter in this model.
-
setParamValue
Sets the value of a single free parameter to the given value.- Specified by:
setParamValue
in interfaceISemIm
- Throws:
IllegalArgumentException
- if the given parameter is not a free parameter in this model.
-
setFixedParamValue
Sets the value of a single free parameter to the given value.- Specified by:
setFixedParamValue
in interfaceISemIm
- Throws:
IllegalArgumentException
- if the given parameter is not a free parameter in this model.
-
getErrVar
-
getErrCovar
-
getEdgeCoef
-
getEdgeCoef
-
setErrVar
-
setEdgeCoef
- Specified by:
setEdgeCoef
in interfaceISemIm
-
existsEdgeCoef
-
setErrCovar
-
setErrCovar
-
setMean
Sets the mean associated with the given node. -
setMeanStandardDeviation
Sets the mean associated with the given node. -
setIntercept
Sets the intercept. For acyclic SEMs only.- Specified by:
setIntercept
in interfaceISemIm
- Throws:
UnsupportedOperationException
- if called on a cyclic SEM.
-
getIntercept
- Specified by:
getIntercept
in interfaceISemIm
- Returns:
- the intercept, for acyclic models, or Double.NaN otherwise.
- Throws:
UnsupportedOperationException
- if called on a cyclic SEM.
-
getMean
-
getMeans
public double[] getMeans()- Returns:
- the means for variables in order.
-
getMeanStdDev
- Specified by:
getMeanStdDev
in interfaceISemIm
- Returns:
- the value of the mean associated with the given node.
-
getVariance
- Specified by:
getVariance
in interfaceISemIm
- Returns:
- the value of the variance associated with the given node.
-
getStdDev
-
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
- Throws:
IllegalArgumentException
- if the given parameter is not a free parameter in this model or if there is no parameter connecting nodeA with nodeB in this model.
-
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
- Throws:
IllegalArgumentException
- if the given parameter is not a free parameter in this model or if there is no parameter connecting nodeA with nodeB in this model, or if value is Double.NaN.
-
getFreeParameters
- Specified by:
getFreeParameters
in interfaceISemIm
- Returns:
- the (unmodifiable) list of free freeParameters in the model.
-
getNumFreeParams
public int getNumFreeParams()- Specified by:
getNumFreeParams
in interfaceISemIm
- Returns:
- the number of free freeParameters.
-
getFixedParameters
- Specified by:
getFixedParameters
in interfaceISemIm
- Returns:
- the (unmodifiable) list of fixed freeParameters in the model.
-
getNumFixedParams
public int getNumFixedParams()- Returns:
- the number of free freeParameters.
-
getVariableNodes
The list of measured and latent nodes for the semPm. (Unmodifiable.)- Specified by:
getVariableNodes
in interfaceISemIm
-
getMeasuredNodes
The list of measured nodes for the semPm. (Unmodifiable.)- Specified by:
getMeasuredNodes
in interfaceISemIm
-
getSampleSize
public int getSampleSize()- Specified by:
getSampleSize
in interfaceISemIm
- Returns:
- the sample size (that is, the sample size of the CovarianceMatrix provided at construction time).
-
getEdgeCoef
- 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
- 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
- Specified by:
getImplCovar
in interfaceISemIm
- Returns:
- a copy of the implied covariance matrix over all the variables.
-
getImplCovarMeas
- Specified by:
getImplCovarMeas
in interfaceISemIm
- Returns:
- a copy of the implied covariance matrix over the measured variables only.
-
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. -
getBicScore
public double getBicScore()- Specified by:
getBicScore
in interfaceISemIm
- Returns:
- BIC score, calculated as chisq - dof. This is equal to getFullBicScore() up to a constant.
-
getRmsea
public double getRmsea() -
getCfi
public double getCfi() -
getChiSquare
public double getChiSquare()- Specified by:
getChiSquare
in interfaceISemIm
- Returns:
- the chi square value for the model.
-
getPValue
public double getPValue() -
simulateData
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.
-
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
- Specified by:
getStandardError
in interfaceISemIm
-
listUnmeasuredLatents
- Specified by:
listUnmeasuredLatents
in interfaceISemIm
-
getTValue
-
getPValue
-
isParameterBoundsEnforced
public boolean isParameterBoundsEnforced()- Specified by:
isParameterBoundsEnforced
in interfaceISemIm
-
setParameterBoundsEnforced
public void setParameterBoundsEnforced(boolean parameterBoundsEnforced) - Specified by:
setParameterBoundsEnforced
in interfaceISemIm
-
isEstimated
public boolean isEstimated()- Specified by:
isEstimated
in interfaceISemIm
-
setEstimated
public void setEstimated(boolean estimated) -
isCyclic
public boolean isCyclic() -
getVariableNode
- Returns:
- the variable by the given name, or null if none exists.
- Throws:
NullPointerException
- if name is null.
-
toString
-
getParams
-
setParams
-
getVariableMeans
public double[] getVariableMeans() -
isSimulatedPositiveDataOnly
public boolean isSimulatedPositiveDataOnly()- Specified by:
isSimulatedPositiveDataOnly
in interfaceISemIm
-
getImplCovar
-
getNumRandomCalls
public int getNumRandomCalls()
-