Skip to content

Commit

Permalink
Merge pull request #179 from sfcheung/devel
Browse files Browse the repository at this point in the history
Update to 0.2.3.5
  • Loading branch information
sfcheung authored Sep 28, 2024
2 parents 490b5c9 + b7f816a commit 48199e3
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 22 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.2.3.3
Version: 0.2.3.5
Authors@R:
c(person(given = "Shu Fai",
family = "Cheung",
Expand Down
14 changes: 12 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# manymome 0.2.3.3
# manymome 0.2.3.5

## New Features

Expand All @@ -7,7 +7,9 @@
using `facet_grid()` to plot the
conditional effects when there are
two or more moderators in a path.
(0.2.3.3)
(0.2.3.3, 0.2.3.5)

## Improvement

- If all paths have the same `x`
variables, the argument `x` can be
Expand All @@ -18,6 +20,14 @@
omitted when calling
`total_indirect_effect()`. (0.2.3.2)

- If appropriate, most functions will
automatically convert a single
model fitted by `lm()` to a list by
`lm2list()`. This allows users to
use the output of `lm()` directly,
such as when computing effects in a
moderation-only model. (0.2.3.4)

## Bug Fixes

- Fixed an issue related to
Expand Down
8 changes: 7 additions & 1 deletion R/all_indirect_paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
#' columns, `x`, `y`, and `m`, which can be used by
#' functions such as [indirect_effect()].
#'
#' @param fit A fit object. Either the output of
#' @param fit A fit object. It can be the output of
#' [lavaan::lavaan()] or its wrapper such as [lavaan::sem()],
#' or a list of the output of [lm()] or the output of
#' [lm2list()].
#' If it is a single model fitted by
#' [lm()], it will be automatically converted
#' to a list by [lm2list()].
#'
#' @param exclude A character vector of variables to be excluded
#' in the search, such as control variables.
Expand Down Expand Up @@ -146,6 +149,9 @@ all_indirect_paths <- function(fit = NULL,
x = NULL,
y = NULL,
group = NULL) {
if (!is.null(fit)) {
fit <- auto_lm2list(fit)
}
fit_type <- cond_indirect_check_fit(fit)
if (is.na(fit_type)) {
stop("'fit' is not of a supported type.")
Expand Down
6 changes: 6 additions & 0 deletions R/boot2est_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@

lm2boot_out <- function(outputs, R = 100, seed = NULL,
progress = TRUE) {
if (!missing(outputs)) {
outputs <- auto_lm2list(outputs)
}
out_type <- cond_indirect_check_fit(outputs)
if (out_type != "lm") {
stop("'outputs' must be a list of 'lm()' outputs.")
Expand Down Expand Up @@ -181,6 +184,9 @@ lm2boot_out_parallel <- function(outputs,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
make_cluster_args = list(),
progress = TRUE) {
if (!missing(outputs)) {
outputs <- auto_lm2list(outputs)
}
out_type <- cond_indirect_check_fit(outputs)
if (out_type != "lm") {
stop("'outputs' must be a list of 'lm()' outputs.")
Expand Down
4 changes: 4 additions & 0 deletions R/check_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#' returned by
#' [semTools::runMI()] or
#' its wrapper, such as [semTools::sem.mi()].
#' If it is a single model fitted by
#' [lm()], it will be automatically converted
#' to a list by [lm2list()].
#'
#' @param x Character. The name of
#' predictor at the start of the path.
Expand Down Expand Up @@ -99,6 +102,7 @@ check_path <- function(x,
m = NULL,
fit = NULL,
est = NULL) {
fit <- auto_lm2list(fit)
if (is.null(est)) {
fit_type <- cond_indirect_check_fit(fit)
est <- switch(fit_type,
Expand Down
6 changes: 6 additions & 0 deletions R/cond_indirect.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@
#' returned by
#' [semTools::runMI()] or
#' its wrapper, such as [semTools::sem.mi()].
#' If it is a single model fitted by
#' [lm()], it will be automatically converted
#' to a list by [lm2list()].
#'
#' @param est The output of
#' [lavaan::parameterEstimates()]. If
Expand Down Expand Up @@ -528,6 +531,7 @@ cond_indirect <- function(x,
ci_type = NULL,
group = NULL,
boot_type = c("perc", "bc")) {
fit <- auto_lm2list(fit)
boot_type <- match.arg(boot_type)
fit_type <- cond_indirect_check_fit(fit)
chkpath <- check_path(x = x, y = y, m = m, fit = fit, est = est)
Expand Down Expand Up @@ -827,6 +831,7 @@ indirect_effect <- function(x,
ci_type = NULL,
boot_type = c("perc", "bc"),
group = NULL) {
fit <- auto_lm2list(fit)
boot_type <- match.arg(boot_type)
cond_indirect(x = x,
y = y,
Expand Down Expand Up @@ -975,6 +980,7 @@ cond_indirect_effects <- function(wlevels,
boot_type = c("perc", "bc"),
groups = NULL,
...) {
fit <- auto_lm2list(fit)
boot_type <- match.arg(boot_type)
# Check the number of groups and handle multiple-group models
has_group <- FALSE
Expand Down
8 changes: 7 additions & 1 deletion R/do_boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@
#' the variables in each bootstrap
#' sample.
#'
#' @param fit Either (a) a list of `lm`
#' @param fit It can be (a) a list of `lm`
#' class objects, or the output of
#' [lm2list()] (i.e., an `lm_list`-class
#' object), or (b) the output of
#' [lavaan::sem()].
#' If it is a single model fitted by
#' [lm()], it will be automatically converted
#' to a list by [lm2list()].
#'
#' @param R The number of bootstrap
#' samples. Default is 100.
Expand Down Expand Up @@ -137,6 +140,9 @@ do_boot <- function(fit,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
make_cluster_args = list(),
progress = TRUE) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
fit_type <- cond_indirect_check_fit(fit)
if (fit_type == "lavaan.mi") {
stop("Bootstrapping does not support multiple imputation.")
Expand Down
6 changes: 6 additions & 0 deletions R/do_mc.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ do_mc <- function(fit,
ncores = max(parallel::detectCores(logical = FALSE) - 1, 1),
make_cluster_args = list(),
progress = TRUE) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
fit_type <- cond_indirect_check_fit(fit)
if (fit_type == "lavaan" || fit_type == "lavaan.mi") {
fit0 <- gen_mc_est(fit = fit,
Expand Down Expand Up @@ -161,6 +164,9 @@ gen_mc_est <- function(fit,
seed = NULL) {
# fit_vcov <- tryCatch(lavaan::lavInspect(fit, "vcov"),
# error = function(e) e)
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
fit_vcov <- tryCatch(get_vcov(fit),
error = function(e) e)
if (inherits(fit_vcov, "error")) {
Expand Down
3 changes: 3 additions & 0 deletions R/get_b.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ get_b <- function(x,
fit,
est = NULL,
group_number = NULL) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
if (is.null(est)) {
est <- lav_est(fit, se = FALSE, ci = FALSE)
}
Expand Down
3 changes: 3 additions & 0 deletions R/get_intercept.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
get_intercept <- function(x,
fit,
est = NULL) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
if (is.null(est)) {
est <- lav_est(fit, se = FALSE, ci = FALSE)
}
Expand Down
1 change: 1 addition & 0 deletions R/get_prod.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ get_prod <- function(x,
est = NULL,
data = NULL,
expand = FALSE) {
fit <- auto_lm2list(fit)
if (is.null(est)) {
est <- lav_est(fit, se = FALSE, ci = FALSE)
}
Expand Down
2 changes: 2 additions & 0 deletions R/index_mome.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ index_of_mome <- function(x,
ci_out = NULL,
boot_type = c("perc", "bc"),
...) {
fit <- auto_lm2list(fit)
boot_type <- match.arg(boot_type)
if (is.null(w) || length(w) != 1) {
stop("The path must have exactly one moderator.")
Expand Down Expand Up @@ -329,6 +330,7 @@ index_of_momome <- function(x,
ci_out = NULL,
boot_type = c("perc", "bc"),
...) {
fit <- auto_lm2list(fit)
boot_type <- match.arg(boot_type)
if (is.null(w) || is.null(z) ||
length(w) != 1 || length(z) != 1) {
Expand Down
2 changes: 1 addition & 1 deletion R/indirect.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ indirect_i <- function(x,
# Need these arguments to compute SE
# - est_vcov
# - df_residual

fit <- auto_lm2list(fit)
if (is.null(est)) {
est <- lav_est(fit)
}
Expand Down
1 change: 1 addition & 0 deletions R/indirect_proportion.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ indirect_proportion <- function(x,
m = NULL,
fit = NULL) {
# Support for boot tentatively added but not enabled.
fit <- auto_lm2list(fit)
boot_type <- "perc"
if (is.null(m)) {
stop("m cannot be NULL.")
Expand Down
3 changes: 3 additions & 0 deletions R/lm2ptable.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#'

lm2ptable <- function(outputs) {
if (!missing(outputs)) {
outputs <- auto_lm2list(outputs)
}
mm <- merge_model_matrix(outputs)
coefs <- lapply(outputs, coef2lor)
out <- do.call(rbind, coefs)
Expand Down
8 changes: 8 additions & 0 deletions R/lm_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,12 @@ check_cases <- function(x) {
}
}
return(TRUE)
}

auto_lm2list <- function(object) {
if (inherits(object, "lm")) {
out <- lm2list(object)
return(out)
}
object
}
9 changes: 9 additions & 0 deletions R/mod_levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#' returned by
#' [semTools::runMI()] or
#' its wrapper, such as [semTools::sem.mi()].
#' If it is a single model fitted by
#' [lm()], it will be automatically converted
#' to a list by [lm2list()].
#'
#' @param w_type Character. Whether the
#' moderator is a `"numeric"` variable
Expand Down Expand Up @@ -226,6 +229,9 @@ mod_levels <- function(w,
values = NULL,
reference_group_label = NULL,
descending = TRUE) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
fit_type <- cond_indirect_check_fit(fit)
if (fit_type == "lavaan") {
if (lavaan::lavTech(fit, "ngroups") > 1) {
Expand Down Expand Up @@ -317,6 +323,9 @@ mod_levels_list <- function(...,
prefix = NULL,
descending = TRUE,
merge = FALSE) {
if (!missing(fit)) {
fit <- auto_lm2list(fit)
}
x <- list(...)
p <- length(x)
if (is.null(sd_from_mean)) {
Expand Down
20 changes: 15 additions & 5 deletions R/plotmod.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@
#' if [ggplot2::facet_grid()] is not
#' used.
#'
#' @param digits The number of decimal
#' places to be printed for numerical
#' moderators when `facet_grid` is used.
#' Default is 4.
#'
#' @param ... Additional arguments.
#' Ignored.
#'
Expand Down Expand Up @@ -284,6 +289,7 @@ plot.cond_indirect_effects <- function(
facet_grid_rows = NULL,
facet_grid_args = list(as.table = FALSE,
labeller = "label_both"),
digits = 4,
...
) {
has_groups <- cond_indirect_effects_has_groups(x)
Expand Down Expand Up @@ -617,11 +623,14 @@ plot.cond_indirect_effects <- function(
w_names_in <- NULL
}
plot_df_tmp <- w_to_numeric(plot_df,
w_names = w_names)
w_names = w_names,
digits = digits)
plot_df_xstart_tmp <- w_to_numeric(plot_df_xstart,
w_names = w_names)
w_names = w_names,
digits = digits)
plot_df_xend_tmp <- w_to_numeric(plot_df_xend,
w_names = w_names)
w_names = w_names,
digits = digits)
plot_df_xstart_end <- plot_df_xstart_tmp
plot_df_xstart_end[paste0(x, "___end")] <- plot_df_xend_tmp[, x, drop = TRUE]
plot_df_xstart_end[paste0(y, "___end")] <- plot_df_xend_tmp[, y, drop = TRUE]
Expand Down Expand Up @@ -702,8 +711,9 @@ w_to_numeric <- function(xx,
for (i in colnames(xx)) {
if (is.numeric(xx[, i]) &&
(i %in% w_names)) {
xx[, i] <- as.factor(round(xx[, i],
digits = digits))
xx[, i] <- as.factor(formatC(xx[, i],
digits = digits,
format = "f"))
}
}
xx
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.2.3.3, updated on 2024-09-27, [release history](https://sfcheung.github.io/manymome/news/index.html))
(Version 0.2.3.5, updated on 2024-09-28, [release history](https://sfcheung.github.io/manymome/news/index.html))

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

Expand Down
7 changes: 5 additions & 2 deletions man/all_indirect_paths.Rd

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

5 changes: 4 additions & 1 deletion man/check_path.Rd

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

5 changes: 4 additions & 1 deletion man/cond_indirect.Rd

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

Loading

0 comments on commit 48199e3

Please sign in to comment.