Code Monkey home page Code Monkey logo

semmcci's Introduction

semmcci

Ivan Jacob Agaloos Pesigan 2024-08-20

CRAN Status R-Universe Status DOI Make Project R-CMD-check R Package Test Coverage Lint R Package Package Website (GitHub Pages) Compile LaTeX Shell Check pages-build-deployment codecov

Installation

You can install the CRAN release of semmcci with:

install.packages("semmcci")

You can install the development version of semmcci from GitHub with:

if (!require("remotes")) install.packages("remotes")
remotes::install_github("jeksterslab/semmcci")

Description

In the Monte Carlo method, a sampling distribution of parameter estimates is generated from the multivariate normal distribution using the parameter estimates and the sampling variance-covariance matrix. Confidence intervals for defined parameters are generated by obtaining percentiles corresponding to 100(1 - α)% from the generated sampling distribution, where α is the significance level.

Monte Carlo confidence intervals for free and defined parameters in models fitted in the structural equation modeling package lavaan can be generated using the semmcci package. The package has three main functions, namely, MC(), MCMI(), and MCStd(). The output of lavaan is passed as the first argument to the MC() function or the MCMI() function to generate Monte Carlo confidence intervals. Monte Carlo confidence intervals for the standardized estimates can also be generated by passing the output of the MC() function or the MCMI() function to the MCStd() function. A description of the package and code examples are presented in Pesigan and Cheung (2023: https://doi.org/10.3758/s13428-023-02114-4).

Example

A common application of the Monte Carlo method is to generate confidence intervals for the indirect effect. In the simple mediation model, variable X has an effect on variable Y, through a mediating variable M. This mediating or indirect effect is a product of path coefficients from the fitted model.

library(semmcci)
library(lavaan)

Data

summary(df)
#>        X                  M                  Y           
#>  Min.   :-4.00294   Min.   :-3.35169   Min.   :-3.20074  
#>  1st Qu.:-0.57582   1st Qu.:-0.72116   1st Qu.:-0.61172  
#>  Median : 0.07574   Median :-0.06034   Median : 0.10346  
#>  Mean   : 0.04190   Mean   :-0.04597   Mean   : 0.06846  
#>  3rd Qu.: 0.69890   3rd Qu.: 0.62467   3rd Qu.: 0.74076  
#>  Max.   : 3.51560   Max.   : 2.98118   Max.   : 2.94785  
#>  NA's   :100        NA's   :100        NA's   :100

Model Specification

The indirect effect is defined by the product of the slopes of paths X to M labeled as a and M to Y labeled as b. In this example, we are interested in the confidence intervals of indirect defined as the product of a and b using the := operator in the lavaan model syntax.

model <- "
  Y ~ cp * X + b * M
  M ~ a * X
  X ~~ X
  indirect := a * b
  direct := cp
  total := cp + (a * b)
"

Monte Carlo Confidence Intervals

We can now fit the model using the sem() function from lavaan. We use full-information maximum likelihood to deal with missing values.

fit <- sem(data = df, model = model, missing = "fiml")

The fit lavaan object can then be passed to the MC() function to generate Monte Carlo confidence intervals.

mc <- MC(fit, R = 20000L, alpha = 0.05)
mc
#> Monte Carlo Confidence Intervals
#>              est     se     R    2.5%   97.5%
#> cp        0.2151 0.0320 20000  0.1514  0.2772
#> b         0.5042 0.0322 20000  0.4411  0.5676
#> a         0.4938 0.0299 20000  0.4352  0.5521
#> X~~X      0.9773 0.0459 20000  0.8883  1.0684
#> Y~~Y      0.6097 0.0301 20000  0.5511  0.6681
#> M~~M      0.7513 0.0362 20000  0.6796  0.8235
#> Y~1       0.0808 0.0267 20000  0.0292  0.1322
#> M~1      -0.0641 0.0291 20000 -0.1206 -0.0073
#> X~1       0.0398 0.0323 20000 -0.0241  0.1030
#> indirect  0.2490 0.0217 20000  0.2081  0.2933
#> direct    0.2151 0.0320 20000  0.1514  0.2772
#> total     0.4641 0.0307 20000  0.4032  0.5238

Monte Carlo Confidence Intervals - Multiple Imputation

The MCMI() function can be used to handle missing values using multiple imputation. The MCMI() accepts the output of mice::mice(), Amelia::amelia(), or a list of multiply imputed data sets. In this example, we impute multivariate missing data under the normal model.

mi <- mice::mice(
  df,
  method = "norm",
  m = 100,
  print = FALSE,
  seed = 42
)

We fit the model using lavaan using the default listwise deletion.

fit <- sem(data = df, model = model)

The fit lavaan object and mi object can then be passed to the MCMI() function to generate Monte Carlo confidence intervals.

mcmi <- MCMI(fit, mi = mi, R = 20000L, alpha = 0.05, seed = 42)
mcmi
#> Monte Carlo Confidence Intervals (Multiple Imputation Estimates)
#>             est     se     R   2.5%  97.5%
#> cp       0.2156 0.0326 20000 0.1520 0.2795
#> b        0.5029 0.0318 20000 0.4405 0.5653
#> a        0.4928 0.0305 20000 0.4331 0.5528
#> X~~X     0.9783 0.0466 20000 0.8872 1.0698
#> Y~~Y     0.6089 0.0299 20000 0.5501 0.6675
#> M~~M     0.7524 0.0361 20000 0.6805 0.8227
#> indirect 0.2479 0.0223 20000 0.2056 0.2927
#> direct   0.2156 0.0326 20000 0.1520 0.2795
#> total    0.4634 0.0310 20000 0.4027 0.5245

Standardized Monte Carlo Confidence Intervals

Standardized Monte Carlo Confidence intervals can be generated by passing the result of the MC() function or the MCMI() function to MCStd().

MCStd(mc, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>              est     se     R   2.5%  97.5%
#> cp        0.2115 0.0311 20000 0.1493 0.2709
#> b         0.4988 0.0289 20000 0.4407 0.5539
#> a         0.4907 0.0262 20000 0.4382 0.5408
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.6030 0.0266 20000 0.5499 0.6544
#> M~~M      0.7592 0.0257 20000 0.7076 0.8080
#> indirect  0.0804 0.0194 20000 0.2075 0.2836
#> direct   -0.0644 0.0311 20000 0.1493 0.2709
#> total     0.0403 0.0270 20000 0.4015 0.5066
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2302 0.0317 20000 0.1493 0.2736
#> b        0.4847 0.0291 20000 0.4400 0.5534
#> a        0.4715 0.0267 20000 0.4367 0.5410
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.6068 0.0266 20000 0.5500 0.6546
#> M~~M     0.7777 0.0262 20000 0.7073 0.8093
#> indirect 0.2286 0.0201 20000 0.2049 0.2839
#> direct   0.2302 0.0317 20000 0.1493 0.2736
#> total    0.4588 0.0273 20000 0.4014 0.5089

Documentation

See GitHub Pages for package documentation.

Citation

To cite semmcci in publications, please cite Pesigan & Cheung (2023).

References

MacKinnon, D. P., Lockwood, C. M., & Williams, J. (2004). Confidence limits for the indirect effect: Distribution of the product and resampling methods. Multivariate Behavioral Research, 39(1), 99–128. https://doi.org/10.1207/s15327906mbr3901_4

Pesigan, I. J. A., & Cheung, S. F. (2023). Monte Carlo confidence intervals for the indirect effect with missing data. Behavior Research Methods, 56(3), 1678–1696. https://doi.org/10.3758/s13428-023-02114-4

Preacher, K. J., & Selig, J. P. (2012). Advantages of Monte Carlo confidence intervals for indirect effects. Communication Methods and Measures, 6(2), 77–98. https://doi.org/10.1080/19312458.2012.679848

Tofighi, D., & Kelley, K. (2019). Indirect effects in sequential mediation models: Evaluating methods for hypothesis testing and confidence interval formation. Multivariate Behavioral Research, 55(2), 188–210. https://doi.org/10.1080/00273171.2019.1618545

Tofighi, D., & MacKinnon, D. P. (2015). Monte Carlo confidence intervals for complex functions of indirect effects. Structural Equation Modeling: A Multidisciplinary Journal, 23(2), 194–205. https://doi.org/10.1080/10705511.2015.1057284

semmcci's People

Contributors

jeksterslab avatar jeksterslabds avatar sfcheung avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

semmcci's Issues

TODO: Unit Tests

Write appropriate unit tests for different models and use cases.

Preparation for CRAN submission

First release:

Prepare for release:

  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • Review pkgdown reference index for, e.g., missing topics
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('major')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.