Wednesday, October 18, 2017

latent change score model in R


For those interested... I made R implement LCS, it's actually pretty flexible... enjoy!

#as with Mplus code, I suggest you rename your T1 and T2 variables obsY1 and obsY2 first
install.packages("lavaan") #you need to install lavaan first
library(lavaan) #you need to call in lavaan first
#in lavaan the model and data are 2 entities, I like this, they become connected after defining the model

#first the model
LCS2waves <- ' LatY1 =~ 1*obsY1 #define latents behind the observed
obsY1~~ measerr*obsY1 # same measurement error measerr across time
obsY1~0*1 #intercept of 1-indicator@0 to identify the mean of the latent; if free obsY1~1
LatY2 =~ 1*obsY2; #define latents behind the observed
obsY2~~ measerr*obsY2 #same measurement error measerr across time
obsY2~0*1  #intercept of 1-indicator@0 to identify the mean of the latent; if free obsY1~1
#LCS part
LCS21 =~ 1*obsY2 #define LCS on the second variable in subtraction
LatY2 =~ 1*LatY1; # autoregression AR1 @1
LatY2~~0*LatY2 #all LatY2 variance is explained, no error left
LatY1~1 # LatY1 mean of first estimated
LatY2~0*1 # LatY2 intercept =0 so LCS21 mean can be identified
#LCS21~~0*LCS21 #no need to set it to 0 per se, but this commonly becomes <0
LCS21~1 # LCS21 mean or intercept estimated
#this is the mean of change, IF nothing points to it, if anything points to it, need to center them to interpret this intercept
LCS21~LatY1 #proportional growth, this is not needed, if you take it out you will have a covariance between them estimated
'


#now one can fit the model above to some (any) data
fit <- sem(model = LCS2waves, data = YOURDATA)

# show results
summary(fit, fit.measures=TRUE)

#mine showed something like:

lavaan (0.5-23.1097) converged normally after  46 iterations

  Number of observations                            61

  Estimator                                         ML
  Minimum Function Test Statistic                   NA
  Degrees of freedom                                -1
  Minimum Function Value               0.0000000000000

User model versus baseline model:

  Comparative Fit Index (CFI)                       NA
  Tucker-Lewis Index (TLI)                          NA

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)               -409.528
  Loglikelihood unrestricted model (H1)       -409.528

  Number of free parameters                          6
  Akaike (AIC)                                 831.056
  Bayesian (BIC)                               843.721
  Sample-size adjusted Bayesian (BIC)          824.846

Root Mean Square Error of Approximation:

  RMSEA                                             NA
  90 Percent Confidence Interval             NA     NA
  P-value RMSEA <= 0.05                             NA

Standardized Root Mean Square Residual:

  SRMR                                           0.000

Parameter Estimates:

  Information                                 Expected
  Standard Errors                             Standard

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  LatY1 =~                                            
    obsY1             1.000                           
  LatY2 =~                                            
    obsY2             1.000                           
  LCS21 =~                                            
    obsY2             1.000                           
  LatY2 =~                                            
    LatY1             1.000                           

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)
  LCS21 ~                                             
    LatY1             0.568    0.423    1.341    0.180

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)
   .obsY1             0.000                           
   .obsY2             0.000                           
    LatY1             9.131    0.912   10.009    0.000
    LatY2             0.000                           
   .LCS21             3.569    3.742    0.954    0.340

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .obsY1   (msrr)   51.146   14.017    3.649    0.000
   .obsY2   (msrr)   51.146   14.017    3.649    0.000
    LatY2             0.000                           
    LatY1            -0.376   10.659   -0.035    0.972
   .LCS21            -5.233   14.091   -0.371    0.710

Tuesday, February 14, 2017

A simple DAG

According to a discussion on SEMNET, the model below 'lacks any testable implications' or one 'cannot detect any testable implications', URL.

I like to 'see' things before deciding, so I submitted it to daggityR, simple task in fact, see code below, but 1st the visual ('plot', called in R), and its 'implications' (called 'impliedConditionalIndependencies' in R); this happens to be the simplest' mediation (called Barron-Kenny, BK, see Frontiers commentary for clarifications).
impliedConditionalIndependencies(NoImplic)

M _||_ UX | X
M _||_ UY
UM _||_ UX
UM _||_ UY
UM _||_ X
UM _||_ Y | M, X
UX _||_ UY
UX _||_ Y | X
UY _||_ X

adjustmentSets( NoImplic, "X", "Y", type="all" )

 {}
 { UM }
 { UX }
 { UM, UX }
 { UY }
 { UM, UY }
 { UX, UY }
 { UM, UX, UY }

adjustmentSets( NoImplic, "M", "Y", type="all" )

 { X }
 { UM, X }
 { UX, X }
 { UM, UX, X }
 { UY, X }
 { UM, UY, X }
 { UX, UY, X }
 { UM, UX, UY, X }

# how to build it:
library(dagitty)

NoImplic <- dagitty('dag {
UX [pos="0,1"]
X [pos="1,1"]
UM [pos="1,0"]
M [pos="2,0"]
UY [pos="2.5,1.5"]
Y [pos="3,1"]
UX-> X -> M ->Y <- X
UM -> M
UY -> Y
}')
plot(NoImplic)

~~~~~~~~
Coman, E. N., F. Thoemmes and J. Fifield (2017). Commentary: Causal Effects in Mediation Modeling: An Introduction with Applications to Latent Variables. Frontiers in Psychology 8(151). http://journal.frontiersin.org/article/10.3389/fpsyg.2017.00151/full

Monday, February 6, 2017

DAGs-simple

A recent paper (Fischer,  Dietz, & Antonakis, 2017) proposed a 'specious mediation' model, which is one combination of causal effects flowing in a peculiar pattern; the 'causal story' is told like:
"As an example, perceived justice has been found to mediate the impact of monitoring methods on organizational citizenship behavior (Niehoff & Moorman, 1993). An alternative mediator is trust in the supervisor (Pillai, Schriesheim, & Williams, 1999), which is likely also affected by monitoring and related to perceived justice. Then, if trust but not justice perceptions affected organizational citizenship behaviors, justice perceptions would appear as a significant mediator if trust is not included in the model. Findings from the model excluding trust are not interpretable, because the effect of trust on the parameter estimate for perceived justice is unknown: Perceived justice might be a mediator, although likely with less of an impact than what the estimate suggests, or worse, it might be a specious mediator.
Such a specious mediator can be uncovered only by including true and correlated mediators,such as trust in the model. Given that a cause such as monitoring might have a multitude of proximal effects that are also correlated with trust and perceived justice and that have an effect on organizational citizenship behavior, the risk of ignoring relevant mediators and detecting specious mediators is highly prevalent." [p. 15-16]

Hope I captured it well... One can inspect such DAGs formally, and easily, with daggityR, see code below, see then what it can tell us beyond the informal analysis above.

Here's the original figure, and the R generated one (I took the liberty of labeling in Fig. 4 the variable names from the example, hope they match, although some statements above make me pause, like "perceived justice has been found to mediate the impact of monitoring methods on organizational citizenship behavior", so this is (and then not) a mediator, plus " trust in the supervisor [..is.]  related to perceived justice.", yet the figure says they appear 'related' only because of common cause 'Omitted').



#if daggity is not installed, there, you need to install it
install.packages("dagitty")

#this line below calls the daggity module up
library(dagitty)


# Fischer '17 specious mediation
# the 1st 6 lines simply position variables in a bi-dimensional Y(X) space, you can move them around
#I positioned Omitted to the left of 'mediators' because it is a cause of them

SpeciousF <- dagitty('dag {
Monitoring [pos="0,1"]
PerceivedJustice [pos="2,2"]
TrustinSupervisor [pos="2,0"]
Omitted  [pos="1,1"]
OrgCitizenshipBehavior [pos="3,1"]
#these next 3 lines specify the causal structure, which-causes-which
Monitoring   -> TrustinSupervisor -> OrgCitizenshipBehavior  <- Omitted
TrustinSupervisor <- Omitted
Monitoring  -> PerceivedJustice<- Omitted
}')

# now you can plot the DAG you just created above, see below figure
plot(SpeciousF )




# one can inspect implications of the DAG below
> impliedConditionalIndependencies( SpeciousF )

#there are a few independencies implied by the DAG below:

Monitoring _||_ Omitted
Monitoring _||_ OrgCitizenshipBehavior | Omitted, TrustinSupervisor
OrgCitizenshipBehavior _||_ PerceivedJustice | Monitoring, Omitted
OrgCitizenshipBehavior _||_ PerceivedJustice | Omitted, TrustinSupervisor
PerceivedJustice _||_ TrustinSupervisor | Monitoring, Omitted

#this shows what you HAVE to adjust to estimate the causal effect of Monitoring on OrgCitizenshipBehavior, with {} meaning 'nothing"

adjustmentSets( SpeciousF, "Monitoring", "OrgCitizenshipBehavior ", type="all" )
> adjustmentSets( SpeciousF, "Monitoring", "OrgCitizenshipBehavior", type="all" )
 {}
 { Omitted }
 { Omitted, PerceivedJustice }


Fischer, T., Dietz, J., & Antonakis, J. , 2017, Leadership Process Models. Journal of Management, 0(0), 0149206316682830. http://dx.doi.org/10.1177/0149206316682830

Thursday, December 29, 2016

Single/multiple and cause/effect indicators

A brief visual note on the possibility of combining cause and effect indicators of latent variables (LV). Ken Bollen noted this possibility of a LV with 1 of each type of indicators in 1984 (evidently this model is not identified as such):

He mentioned such 'mixed' measures several times, see e.g. the depression CES-D one:
"items appear to be a mixture of effect and causal indicators. For instance, "I felt depressed" and "I felt sad" appear to be effect indicators of depressed mood. That is, we expect that a change in the latent depression variable leads to a change in responses to these items. However, "I felt lonely" could be a causal indicator of the same construct in that loneliness may cause depression rather than vice versa. Alternatively, loneliness could be a separate dimension that requires several indicators to measure it. To further complicate things, one could argue that some items are reciprocally related to depression.
For example, individuals may become depressed because they think people dislike them, which makes them appear unattractive, thus other people may avoid them and actually dislike them (i.e., the low affect may be unattractive and offputting)." [1:311]

Note 1:
Les Hayduk explored the 'reciprocally related' ('reactive') indicators in detail in [5]:

Note 2:
Les also advanced the 'finite cycling' feedback/nonrecursive models in [6], which simply says that infinite cycling is not realistic, so one can conceive of: 1. 1 full cycle; 2. 1 & 1/2 cycle; 3. 2 full cycles; etc. (although more than 2 cycles around the effects add up very little). See also in this [6]  great language on why/how one splits an effect η1 -> η2 into 2 such effects, see below the quote, referring to his Fig. 1 below:




“To identify the estimates of the effects in a model like Figure 1 (in the absence of additional equality, proportionality, or other constraints on β21 and β12) a variable like ξ1 is required (Rigdon, 1995). The direct effect of ξ1 on η1, and the important absence of a direct effect of ξ1 on η2, implies that the covariance between ξ1 on η2 arises from the basic indirect effect of ξ1 on η2 (namely γ11 β21), which is enhanced by the loop L = β21 β12. The relegation of β12 to a purely enhancing role in accounting for the covariance between ξ1 and η2 differentiates or disentangles β21 from β12 and makes it possible to obtain separate estimates of these effects. In the Figure 1 model, ξ2 similarly contributes to disentangling the reciprocal effects, so as long as the γ11 and γ22 effects are substantial, the β21 and β12 effects in this model should be overidentified (given the independence of the disturbance terms).” [6: 660-661].


1. Bollen, K. A., & Lennox, R. (1991). Conventional wisdom on measurement: A structural equation perspective. Psychological Bulletin, 110(2), 305-314. doi:10.1037/0033-2909.110.2.305
2. Bollen, K. (1984). Multiple indicators: internal consistency or no necessary relationship? Quality and Quantity, 18(4), 377-385.
3. Bollen, K. A., Glanville, J. L., & Stecklov, G. (2001). Socioeconomic status and class in studies of fertility and health in developing countries. Annual Review of Sociology, 153-185.
4. Bollen, K. A., & Bauldry, S. (2011). Three Cs in measurement models: Causal indicators, composite indicators, and covariates. Psychological Methods, 16(3), 265-284. doi:10.1037/a0024448
5. Hayduk, L. A., Robinson, H. P., Cummings, G. G., Boadu, K., Verbeek, E. L., & Perks, T. A. (2007). The weird world, and equally weird measurement models: Reactive indicators and the validity revolution. Structural Equation Modeling: A Multidisciplinary Journal, 14(2), 280-310. 
6. Hayduk, L. A. (2009). Finite Feedback Cycling in Structural Equation Models. Structural Equation Modeling: A Multidisciplinary Journal, 16(4), 658-675. 

Monday, November 7, 2016

'Causal' mediation great intro

I came across this great writing, which tackles 2 complex topics, causal mediation, and then this g-formula, not an easy feat...

They present a sweeping overview of current 'causal' mediation understanding, wonderfully summarized in some tables, especially 1:

and 2 (a bit 'thicker' for newcomers...)

* Note that g-formula has been incorporated in Stata (SAS too), see: http://www.stata-journal.com/article.html?article=st0238 .

References:
Wang, A., & Arah, O. A. (2015). G-computation demonstration in causal mediation analysis. European Journal of Epidemiology, 30(10), 1119-1127. doi: 10.1007/s10654-015-0100-z http://link.springer.com/article/10.1007/s10654-015-0100-z
Daniel RM, De Stavola BL, Cousens SN. gformula: estimating causal effects in the presence of time-varying confounding or mediation using the g-computation formula. Stata J. 2011;11(4):
479–517.

Wednesday, August 24, 2016

Causal hypotheses:from qualitative to quantitative (SEM style)

The statistical testing of hypotheses about causal sequencing of variables seems to more clearly show nowadays the 2-step process directly stated in Ch. 10 in Conrady & Jouffe, (chapter written with Felix Elwert), process mentioned in my Semnet posting:
1. Causal Identification, followed by
2. Computing the Effect Size.

I found a good example of this approach, within the SEM (Structural Equation Modeling) approach, in a a recent article in Computers in Human Behavior, by Yen & Wu, the sequence is simply: 1. A 'qualitative' (i.e. no numbers!) part: how variables/concepts are expected to 'string themselves' out; and 2. A quantitative part, the post-SEM estimation phase, with numbers attached this time.
1.What one expects:
 2. What one finds (numerically):
References:
Conrady, S. and L. Jouffe, Bayesian Networks and BayesiaLab: 
A Practical Introduction for Researchers. 2015: Bayesia USA: 
free online: http://www.bayesia.com/book
Yen, Y.-S., & Wu, F.-S. (2016). Predicting the adoption of mobile financial services: 
The impacts of perceived mobility and personal habit. Computers in Human Behavior, 
65, 31-42. doi: http://dx.doi.org/10.1016/j.chb.2016.08.017 

Thursday, July 14, 2016

Mediation: from 3 variables to 6 and 27 models

We all know the 3 variable model (X->M->Y & X->Y) with M being an intermediate variable (hence both cause and effect) is 'the simplest' possible 'statistical model', besides the bare-bone 2 variable one (X->Y).
Many have delved into its intricacies however, which are many in fact, dealing with specification alternatives. For instance, as Felix Thoemmes e.g. showed, there are 6 alternative statistically indistinguishable models that link all 3 variables (these are then all 'saturated models': no test of fit to data in SEM can be done).


Another take on this is the Conrady & Jouffe (2015) display of all 27 possible causal specifications one may contemplate when analyzing such 'simple' model (They use N1, N2, N3): this makes clear that the researcher/analyst 'gets married' to a specific causal structure s/he believes to operate behind the data, i.e. that generated it; In BayesiaLab this is obvious by asking the user for direct input ('drawing an arrow', much like in AMOS).


Of course, with >3 variables, the number of possibilities quickly get out of hand! Some can be ruled out by theory and prior findings, others by 'hunches', others by 'strong beliefs' (more so the case in practice, I'd say).

Conrady, S., & Jouffe, L. (2015). Bayesian Networks and BayesiaLab: A Practical Introduction for Researchers. http://www.bayesia.com/book
Thoemmes, F. (2015). Reversing Arrows in Mediation Models Does Not Distinguish Plausible Models. Basic and Applied Social Psychology, 37(4), 226-234. doi:10.1080/01973533.2015.1049351 www.tandfonline.com/doi/pdf/10.1080/01973533.2015.1049351