Skip to contents

Compiles a theory's constructs and propositions into lavaan model syntax written for lavaan::sem(). Constructs with measurement indicators become a latent measurement model (=~), propositions become labelled structural paths (~) and covariances (~~), the covariances the theory rules out are fixed at zero and the effects the theory's claims are about become defined parameters (:=). Comments say what to check before a fit. The output is deterministic.

Usage

tf_compile_sem(theory)

Arguments

theory

A theory object (named list), such as one from tf_read().

Value

The lavaan model syntax as a single string (LF line endings, single trailing newline).

The structural model

Each proposition becomes a line in file order: a labelled regression (to ~ b_from_to*from) for causes, increases, decreases and mediates, and a covariance (from ~~ to) for associates. A moderates proposition gives the moderator's main effect, to ~ b_from_to*from, unless a path relation already writes that line. A comment follows on the product term, which has to be added by hand: to ~ x:from for an observed predictor x, and lavaan::sam() or the modsem package when the variables are latent.

A regression of a variable on itself only rescales its disturbance, so a linear SEM cannot estimate it. A relation from a construct to itself is therefore left out, with a comment that names the proposition, although tf_implications() and tf_simulate() keep it. lavaan refuses a repeated line, so a second proposition on one ordered pair writes a comment in place of a second regression.

Every regression carries a label, b_<from>_<to> in the names the syntax uses, so that the claims about it can be tested. lavaan reads one label on two paths as a constraint that the two are equal, so a label that another name already holds takes the first free suffix, _2, _3 and so on: x -> y_z and x_y -> z give b_x_y_z and b_x_y_z_2. A comment block then lists the sign each increases and decreases proposition states as an inequality on its label, such as # b_x_y > 0 (p1, increases). The fit does not impose them.

Identification

A note on identification follows the structural model. When the paths among the constructs form no loop and no two constructs are joined by both a path and a covariance (a bow), the path coefficients are identified for almost all values of the parameters (Brito & Pearl, 2002), provided that the measurement model identifies the constructs. That has to be checked separately. Otherwise a comment flags each feedback loop, since a non-recursive model may not be identified (Bollen, 1989), and the panic-network example is not. A further comment names each bow, whose path may not be identified unless the theory supplies an instrument for its cause. Whether it does is not checked.

Effects the claims are about

Each prediction or phenomenon whose claim is an effect of x on y gets a comment that restates it, the claims on one pair together. When no feedback loop lies on a path from x to y, effect_<x>_<y> := <sum> then defines the effect as the sum over the directed paths from x to y of the product of the labels along each path, so that lavaan estimates the quantity the claim is about. In a linear model, that sum is the total effect, whatever loops the rest of the graph holds. The paths are found depth first from x, taking the successors of each construct in construct order. When no path leads from x to y, a comment says that the model fixes the effect at zero. When more than 100 paths do, or a construct on a path from x to y, x and y included, lies on a feedback loop, a comment says that no parameter is defined. A claim that cannot be evaluated as written is passed over.

Covariances fixed at zero

The propositions form the graph that tf_implications() reads. Unless an association joins them, two of its constructs that are both exogenous, with no proposition pointing into either, are unrelated by the theory's account. Unless an association joins them, two that are both terminal, with neither pointing to another construct, are related only through the causes the theory names: their disturbances are uncorrelated, so they are independent once those causes are held fixed. By default, lavaan::sem() frees the covariance of two exogenous latent variables and of two terminal variables, and takes that of two exogenous observed variables from the data. A fit with those defaults could not refute the claim. In the modality-switching example, two of the six independencies tf_implications() derives would go untested. The syntax therefore ends with a block that fixes each such covariance at zero (a ~~ 0*b), which for two terminal constructs is the covariance of their disturbances, and deleting the block restores lavaan's defaults. A pair with a moderator keeps those defaults, because the product term changes the moderator's role. A relation from a construct to itself does not count here, since the syntax writes no path for it.

lavaan takes the covariances of observed exogenous variables from the data only until a covariance line names one of them. It then treats that variable as random and fixes at zero each of its covariances that the syntax does not write. Where a zero line names such a variable, the block therefore ends by freeing each covariance between two observed exogenous variables that no association names, when either is a moderator (x ~~ m). The moderator then keeps lavaan's defaults.

Before fitting

The syntax is a starting point for a fit. Indicator names are the sanitised measurement entries and must match columns of the data, so entries written as column names, such as heart_rate, give syntax that fits as it stands. A comment lists each construct with a single indicator, whose residual variance lavaan::sem() fixes at zero, so the construct is treated as measured without error. Some constructs have to be re-specified by hand. A manipulation is better entered as a coded observed variable and a categorical predictor with more than two levels as dummy variables. A covariate that a measurement entry only describes needs a variable of its own. Once the block names an observed predictor, a product term added by hand needs a free covariance with each observed exogenous variable that a covariance line names. lavaan fixes each one left unwritten at zero, and each takes a line of its own (x:m ~~ x).

A construct without measurement entries is written as an observed variable under its own name. When one association joins such a construct to a construct with indicators and another joins it to a second construct without entries, lavaan's default representation, LISREL, can refuse the model: lavaan 0.6.17 and 0.7.3 stop with an error that names the second association. The same syntax fits with lavaan::sem(..., representation = "RAM").

Names

Every name is one lavaan reads as written. Construct ids that lavaan would refuse or misread, such as self-efficacy, 1arousal, NA or its own keyword efa, are renamed. A comment after the header records each renaming (# renamed for lavaan: 'self-efficacy' -> self_efficacy). An indicator is its measurement entry folded and lower-cased, with each run of other characters made one underscore. i_ goes in front when the result would still be refused, so 7-point Likert rating gives i_7_point_likert_rating. A comment writes each control character of an id, and each character outside the Basic Multilingual Plane, as <U+XXXX>, so that it stays one line that lavaan reads as a comment.

The function stops when two different ids would share a name, since lavaan would merge them. Such a pair is two constructs, two indicators of one construct or a construct and an indicator. A construct and an indicator collide when the construct is measured by its own name or named like an indicator of another construct. An indicator shared by two constructs, a cross-loading, is allowed.

References

Bollen, K. A. (1989). Structural equations with latent variables. Wiley. doi:10.1002/9781118619179

Brito, C., & Pearl, J. (2002). A new identification condition for recursive models with correlated errors. Structural Equation Modeling: A Multidisciplinary Journal, 9(4), 459-474. doi:10.1207/s15328007sem0904_1

See also

tf_implications() for the independencies the zero block encodes, and tf_entailments() for the claims the defined effects estimate.

Examples

# Measurement entries written as column names give syntax that fits as it
# stands. Threat and avoidance both follow from arousal alone, so the
# syntax fixes the covariance of their disturbances at zero. Each path is
# labelled, and the signs the theory states are listed as comments.
theory <- tf_theory("demo-1", "A demonstration theory") |>
  tf_add_construct("c_arousal", "Arousal", "Bodily activation.",
                   measurement = c("heart_rate", "arousal_rating")) |>
  tf_add_construct("c_threat", "Perceived threat", "Appraised danger.",
                   measurement = "threat_rating") |>
  tf_add_construct("c_avoidance", "Avoidance", "Withdrawal from the trigger.",
                   measurement = c("avoidance_task", "avoidance_diary")) |>
  tf_add_proposition("p1", "c_arousal", "c_threat", "increases") |>
  tf_add_proposition("p2", "c_arousal", "c_avoidance", "increases")
cat(tf_compile_sem(theory))
#> # lavaan model generated by theoryforge for demo-1
#> # Written for lavaan::sem(); indicator names are the sanitised measurement entries and must match columns of the data.
#> # Measurement model
#> c_arousal =~ heart_rate + arousal_rating
#> c_threat =~ threat_rating
#> c_avoidance =~ avoidance_task + avoidance_diary
#> # Single-indicator constructs (lavaan fixes the indicator's residual variance at zero, so each is treated as measured without error): c_threat
#> # Structural model
#> c_threat ~ b_c_arousal_c_threat*c_arousal
#> c_avoidance ~ b_c_arousal_c_avoidance*c_arousal
#> # Identification: the paths among the constructs form no loop, and no two constructs are joined by both a path and a covariance (a bow). The path coefficients are therefore generically identified if the measurement model identifies the constructs (Brito & Pearl, 2002). Whether it does has to be checked separately.
#> # Signs the theory states, which the fit does not impose
#> # b_c_arousal_c_threat > 0 (p1, increases)
#> # b_c_arousal_c_avoidance > 0 (p2, increases)
#> # Covariances the theory fixes at zero (lavaan::sem() frees them by default; delete this block to restore its defaults)
#> c_threat ~~ 0*c_avoidance