Skip to content

Commit

Permalink
Merge pull request #17 from sfcheung/devel
Browse files Browse the repository at this point in the history
0.3.2: Update for SE-based CI and p-value
  • Loading branch information
sfcheung authored Dec 9, 2024
2 parents c421a89 + f456fc5 commit 32e7397
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 21 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: manymome.table
Title: Publication-Ready Tables for 'manymome' Results
Version: 0.3.0
Version: 0.3.2
Authors@R:
c(person(given = "Shu Fai",
family = "Cheung",
Expand All @@ -20,7 +20,7 @@ BugReports: https://github.com/sfcheung/manymome.table/issues
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Suggests:
knitr,
rmarkdown,
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# manymome.table 0.3.2

- The optional arguments (`...`) of
`as_flextable.cond_indirect_effects()`
and `as_flextable.indirect_list()`
are now passed to `flextable::autofit()`
when preparing the final output.
(0.3.1)

- Updated to print SE-based confidence
intervals and *p*-values when a path
has no mediator and effect not
standardized. (0.3.2)

# manymome.table 0.3.0

- Minor changes to the `README.md`
Expand Down
65 changes: 53 additions & 12 deletions R/as_flextable.cond_indirect_effects.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,20 @@
#' `<[pcut]`, `"[pcut]"` replaced by
#' the value of `pcut`. Default is .001.
#'
#' @param level The level of confidence
#' for the confidence intervals computed
#' from the original standard errors
#' (e.g., the standard errors in
#' [stats::lm()] or `lavaan`).
#' Used only for paths without mediators
#' and both x- and y-variables are not
#' standardized. Default is .95.
#'
#' @param ... Additional arguments.
#' Ignored.
#' To be passed to [flextable::autofit()]
#' in preparing the final table. For
#' example, if some lines are too lone
#' and wrapped, try adding `add_w = .2`.
#'
#' @examples
#'
Expand Down Expand Up @@ -172,6 +183,7 @@ as_flextable.cond_indirect_effects <- function(x,
show_indicators = FALSE,
show_path = TRUE,
pcut = .001,
level = .95,
...) {
# TODO: Remove after an update to manymome
indirect_raw_ci <- FALSE
Expand All @@ -182,15 +194,44 @@ as_flextable.cond_indirect_effects <- function(x,
x_i <- full_output[[1]]
std_x <- isTRUE(x_i$standardized_x)
std_y <- isTRUE(x_i$standardized_y)

x_list <- list2indirect_list(full_output)
coef0 <- manymome::indirect_effects_from_list(x_list,
add_sig = FALSE,
pvalue = pvalue,
se = se)
# x_list <- list2indirect_list(full_output)
# coef0 <- manymome::indirect_effects_from_list(x_list,
# add_sig = FALSE,
# pvalue = pvalue,
# se = se)
coef0 <- as.data.frame(x,
digits = TRUE,
add_sig = FALSE,
pvalue = pvalue,
pvalue_digits = pval_digits,
se = se,
level = level,
to_string = FALSE)
# Fix the column names
if (std_x || std_y) {
colnames(coef0)[colnames(coef0) %in% "ind"] <- "std"
coef0 <- coef0[, -which(colnames(coef0) == "ind")]
colnames(coef0)[colnames(coef0) %in% "std"] <- "std"
}
# Remove component path estimates
j <- max(match(c("ind", "std", "CI.lo", "CI.hi", "SE", "pvalue"),
colnames(coef0)), na.rm = TRUE)
if (j < ncol(coef0)) {
coef0 <- coef0[, seq_len(j), drop = FALSE]
}

# Remove the `Stat` column, for consistency.
# TODO:
# - May keep this column in the future.
j <- match("Stat", colnames(coef0))
if (!is.na(j)) {
coef0 <- coef0[, -j, drop = FALSE]
}

# Remove the columns with moderator values.
# Add them using this function, for compatibility.
j <- min(match(c("ind", "std"), colnames(coef0)), na.rm = TRUE)
if (j > 1) {
coef0 <- coef0[, -seq_len(j - 1), drop = FALSE]
}

x0 <- x_i$x
Expand All @@ -215,9 +256,10 @@ as_flextable.cond_indirect_effects <- function(x,
has_pvalue <- "pvalue" %in% colnames(coef0)
has_ci <- "CI.lo" %in% colnames(coef0)
has_se <- "SE" %in% colnames(coef0)

if (has_ci) {
level <- x_i$level
if (!is.null(x_i$level)) {
level <- x_i$level
}
level_str <- paste0(format(level * 100),
"% CI")
} else {
Expand Down Expand Up @@ -289,7 +331,6 @@ as_flextable.cond_indirect_effects <- function(x,
w_df <- w_df[, !tmp, drop = FALSE]
}
coef0 <- cbind(w_df, coef0)

ft <- flextable::flextable(coef0)

# Format Cells
Expand Down Expand Up @@ -500,6 +541,6 @@ as_flextable.cond_indirect_effects <- function(x,
colwidths = flextable::ncol_keys(ft))
}

ft <- flextable::autofit(ft)
ft <- flextable::autofit(ft, ...)
ft
}
7 changes: 5 additions & 2 deletions R/as_flextable.indirect_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@
#' the value of `pcut`. Default is .001.
#'
#' @param ... Additional arguments.
#' Ignored.
#' To be passed to [flextable::autofit()]
#' in preparing the final table. For
#' example, if some lines are too lone
#' and wrapped, try adding `add_w = .2`.
#'
#' @examples
#'
Expand Down Expand Up @@ -452,6 +455,6 @@ as_flextable.indirect_list <- function(x,
part = "footer")
}

ft <- flextable::autofit(ft)
ft <- flextable::autofit(ft, ...)
ft
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![R-CMD-check](https://github.com/sfcheung/manymome.table/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sfcheung/manymome.table/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

(Version 0.3.0, updated on 2024-03-04, [release history](https://sfcheung.github.io/manymome.table/news/index.html))
(Version 0.3.2, updated on 2024-12-09, [release history](https://sfcheung.github.io/manymome.table/news/index.html))

# manymome.table

Expand Down
55 changes: 55 additions & 0 deletions inst/tinytest/test_cond_effect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
if (requireNamespace("officer", quietly = TRUE) &&
length(unclass(packageVersion("manymome.table"))[[1]]) == 4) {

library(tinytest)
library(manymome)
library(officer)
library(flextable)

dat <- data_med_mod_a
lm_m <- lm(m ~ x*w + c1 + c2, dat)
lm_y <- lm(y ~ m + x + c1 + c2, dat)
fit_lm <- lm2list(lm_m, lm_y)

out_xm_on_w <- cond_effects(wlevels = "w",
x = "x",
y = "m",
fit = fit_lm)

std_xm_on_w <- cond_effects(wlevels = "w",
x = "x",
y = "m",
standardized_x = TRUE,
fit = fit_lm)

tmp1 <- as_flextable(out_xm_on_w,
var_labels = c(w = "Moderator"),
se = TRUE,
pvalue = TRUE,
digits = 3,
level = .90)

smp1 <- as_flextable(std_xm_on_w,
var_labels = c(w = "Moderator"),
se = TRUE,
pvalue = TRUE,
digits = 4)

tmp <- to_html(tmp1)
expect_equal(ncol_keys(tmp1), 7)
expect_match(tmp, "[Moderator]")
expect_match(tmp, "(Moderator)")
expect_match(tmp, "90% CI")
expect_match(tmp, "SE")
expect_match(tmp, formatC(coef(out_xm_on_w)[1], digits = 3, format = "f"))
expect_match(tmp, formatC(confint(out_xm_on_w, level = .90)[2, 2], digits = 3, format = "f"))

smp <- to_html(smp1)
expect_equal(ncol_keys(smp1), 4)
expect_match(smp, "[Moderator]")
expect_match(smp, "(Moderator)")
expect_false(grepl("90% CI", smp, fixed = TRUE))
expect_false(grepl("SE", smp, fixed = TRUE))
expect_match(smp, formatC(coef(std_xm_on_w)[1], digits = 3, format = "f"))

}
5 changes: 3 additions & 2 deletions inst/tinytest/test_cond_ind.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ tmp4 <- formatC(tmp3$pvalue[3], digits = 5, format = "f")
tmp4 <- gsub("^0.", ".", tmp4)
expect_match(tmp, tmp4)


# In the latest version of manymome,
# SE-based CI will be included for moderation-only unstandardized paths.
tmp1 <- as_flextable(outmo_1, digits = 4)
tmp <- to_html(tmp1)
expect_equal(ncol_keys(tmp1), 2)
expect_equal(ncol_keys(tmp1), 5)
expect_match(tmp, "[gp]")
expect_false(grepl("gpgp2", tmp, fixed = TRUE))
expect_match(tmp, formatC(coef(outmo_1)[1], digits = 4, format = "f"))
Expand Down
15 changes: 14 additions & 1 deletion man/as_flextable.cond_indirect_effects.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/as_flextable.indirect_list.Rd

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

0 comments on commit 32e7397

Please sign in to comment.