Skip to content

Commit

Permalink
Merge pull request #144 from sfcheung/devel
Browse files Browse the repository at this point in the history
0.1.14.10: Change the default of `use_implied_stats` to `TRUE` and a note will be printed in the plot
  • Loading branch information
sfcheung authored Apr 3, 2024
2 parents a36bf66 + c782a0f commit 59c7aca
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: manymome
Title: Mediation, Moderation and Moderated-Mediation After Model Fitting
Version: 0.1.14.9
Version: 0.1.14.10
Authors@R:
c(person(given = "Shu Fai",
family = "Cheung",
Expand Down
11 changes: 6 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# manymome 0.1.14.9
# manymome 0.1.14.10

## New Features

Expand Down Expand Up @@ -41,11 +41,12 @@
makes more sense for multigroup
models, in which the distribution of
variables are allowed to be different
between groups. If desired, users
can use the model implied statistics
to determine the means and SDs, which
between groups. Since 0.1.14.10,
by default, the model implied statistics
are used to determine the means and SDs
used in drawing he plot. This approach
is useful when equality constraints
are present. (0.1.14.2, 0.1.14.6)
are present. (0.1.14.2, 0.1.14.6, 0.1.14.10)

- The `plot`-method of
`cond_indirect_effects`-class objects
Expand Down
13 changes: 10 additions & 3 deletions R/plotmod.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@
#'
#' @param use_implied_stats For a
#' multigroup model, if `TRUE`,
#' the default,
#' model implied statistics will be
#' used in computing the means and SDs,
#' which take into equality constraints,
#' if any.
#' If `FALSE`, the default, then the raw
#' If `FALSE`, then the raw
#' data is
#' used to compute the means and SDs.
#' For latent variables, model implied
Expand Down Expand Up @@ -263,7 +264,7 @@ plot.cond_indirect_effects <- function(
line_width = 1,
point_size = 5,
graph_type = c("default", "tumble"),
use_implied_stats = FALSE,
use_implied_stats = TRUE,
...
) {
has_groups <- cond_indirect_effects_has_groups(x)
Expand Down Expand Up @@ -298,6 +299,7 @@ plot.cond_indirect_effects <- function(
stop("x_method cannot be 'percentile' if x is a latent variable.")
}
graph_type <- match.arg(graph_type)
graph_type_original <- graph_type
if (has_groups && graph_type == "default") {
# warning("Only tumble graph is supported for multiple group models. ",
# "Changed graph_type to 'tumble'.")
Expand Down Expand Up @@ -586,11 +588,16 @@ plot.cond_indirect_effects <- function(
cap_txt <- cap_std
}
}
if (has_groups && graph_type == "default") {
if (has_groups && graph_type_original == "default") {
cap_txt <- paste0(cap_txt,
"\n",
"Graph type is set to tumble for multiple group models.")
}
if (use_implied_stats && fit_type != "lm") {
cap_txt <- paste0(cap_txt,
"\n",
"Model implied means and SDs are used in drawing the points'.")
}
out <- p +
ggplot2::labs(title = title,
caption = cap_txt) +
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![R-CMD-check](https://github.com/sfcheung/manymome/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sfcheung/manymome/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

(Version 0.1.14.9, updated on 2024-04-01, [release history](https://sfcheung.github.io/manymome/news/index.html))
(Version 0.1.14.10, updated on 2024-04-01, [release history](https://sfcheung.github.io/manymome/news/index.html))

# manymome <img src="man/figures/logo.png" align="right" height="150" />

Expand Down
5 changes: 3 additions & 2 deletions man/plot.cond_indirect_effects.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rebuild_vignettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ setwd(base_dir)
base_dir <- getwd()

setwd("vignettes/articles")
knitr::knit("med_mg.Rmd.originaL", output = "med_mg.Rmd")
knitr::knit("med_mg.Rmd.original", output = "med_mg.Rmd")

setwd(base_dir)
6 changes: 3 additions & 3 deletions tests/testthat/test_mg_boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,10 @@ tmp3 <- cond_indirect_effects(x = "x",
y = "m1",
fit = fit_med_meq3)

p1a <- plot(tmp1)
p2a <- plot(tmp2)
p1a <- plot(tmp1, use_implied_stats = FALSE)
p2a <- plot(tmp2, use_implied_stats = FALSE)
p2b <- plot(tmp2, use_implied_stats = TRUE)
p3a <- plot(tmp3)
p3a <- plot(tmp3, use_implied_stats = FALSE)
p3b <- plot(tmp3, use_implied_stats = TRUE)

test_that("plot.cond_indirect_effects: multiple groups, implied stats", {
Expand Down
Binary file modified vignettes/articles/manymome_draw_mod_mg-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 28 additions & 11 deletions vignettes/articles/med_mg.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Multigroup Models With Mediation Effects"
author: "Shu Fai Cheung & Sing-Hang Cheung"
date: "2024-03-31"
date: "2024-04-03"
output:
html_document:
fig.align: "center"
Expand Down Expand Up @@ -204,7 +204,7 @@ fit_boot_out <- do_boot(fit = fit,
seed = 53253,
ncores = 8)
#> 8 processes started to run bootstrapping.
#> The expected CPU time is about 0 second(s).
#> The expected CPU time is about 7.5 second(s).
```

# Estimate Indirect Effects
Expand Down Expand Up @@ -889,20 +889,37 @@ for multigroup models:
Note that, for multigroup models, the *tumble*
graph proposed by @bodner_tumble_2016 will
always be used. The position of a line for
a group is determined by the descriptive statistics
of this group (e.g, means and SDs). For example,
a group is determined by the model implied
means and SDs of this group. If no equality
constraints imposed, these means and SDs are
close to the sample means and SDs.
For example,
the line segment of `"Group A"` is far to the right
because `"Group A"` has a larger mean of `m` than
`"Group B"`:
`"Group B"`.

These are the model implied means and SDs:


```r
by(dat$m, dat$group, mean)
#> dat$group: Group A
#> [1] 18.43357
#> ------------------------------------------------------------
#> dat$group: Group B
#> [1] 13.42327
# Model implied means
lavInspect(fit, "mean.ov")
#> $`Group A`
#> m y x c1 c2
#> 18.434 17.973 9.985 2.055 4.883
#>
#> $`Group B`
#> m y x c1 c2
#> 13.423 18.915 10.046 2.138 5.088

# Model implied SDs
tmp <- lavInspect(fit, "cov.ov")
sqrt(diag(tmp[["Group A"]]))
#> m y x c1 c2
#> 1.4916076 2.2158085 1.1055723 0.9687943 1.0741628
sqrt(diag(tmp[["Group B"]]))
#> m y x c1 c2
#> 1.2142143 2.5838604 1.0094330 0.9518064 1.0594076
```

It would be misleading if the two lines are plotted on the
Expand Down
19 changes: 15 additions & 4 deletions vignettes/articles/med_mg.Rmd.original
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,25 @@ plot(path1)
Note that, for multigroup models, the *tumble*
graph proposed by @bodner_tumble_2016 will
always be used. The position of a line for
a group is determined by the descriptive statistics
of this group (e.g, means and SDs). For example,
a group is determined by the model implied
means and SDs of this group. If no equality
constraints imposed, these means and SDs are
close to the sample means and SDs.
For example,
the line segment of `"Group A"` is far to the right
because `"Group A"` has a larger mean of `m` than
`"Group B"`:
`"Group B"`.

These are the model implied means and SDs:

```{r}
by(dat$m, dat$group, mean)
# Model implied means
lavInspect(fit, "mean.ov")

# Model implied SDs
tmp <- lavInspect(fit, "cov.ov")
sqrt(diag(tmp[["Group A"]]))
sqrt(diag(tmp[["Group B"]]))
```

It would be misleading if the two lines are plotted on the
Expand Down

0 comments on commit 59c7aca

Please sign in to comment.