Guest User

Untitled

a guest
Dec 13th, 2018
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. ---
  2. title: "Cross-lag demo"
  3. output: html_notebook
  4. ---
  5.  
  6. First set it all up.
  7.  
  8. ```{r}
  9. library(lavaan)
  10.  
  11. sampsize <- 500
  12.  
  13. lower <- '
  14. 1
  15. .7 1
  16. .4 .3 1
  17. .3 .4 .5 1 '
  18.  
  19. longit_data <- getCov(lower, names = c("x1", "x2", "y1", "y2"))
  20. ```
  21.  
  22. Fit a cross-lag model.
  23.  
  24. ```{r}
  25. crosslag_model <- '
  26. x2 ~ x1 + y1
  27. y2 ~ x1 + y1
  28. x1 ~~ y1
  29. x2 ~~ y2
  30. '
  31. fit_crosslag_model <- sem(crosslag_model, sample.cov = longit_data, sample.nobs = sampsize)
  32. summary(fit_crosslag_model, standardized = TRUE)
  33. ```
  34.  
  35. Now fit a correlated-trait model.
  36.  
  37. ```{r}
  38. trait_model <- '
  39. X_latent =~ a*x1 + a*x2
  40. Y_latent =~ b*y1 + b*y2
  41. X_latent ~~ Y_latent
  42. x1 ~~ y1
  43. x2 ~~ y2
  44. '
  45. fit_trait_model <- sem(trait_model, sample.cov = longit_data, sample.nobs = sampsize, std.lv = TRUE)
  46. summary(fit_trait_model, standardized = TRUE)
  47. ```
Add Comment
Please, Sign In to add comment