Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] apa_print()-methods for metafor #299

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ BugReports: https://github.com/crsh/papaja/issues
Depends:
R (>= 2.11.1)
Imports:
base64enc,
bookdown (>= 0.6),
broom (>= 0.3.6),
knitr (>= 1.10.5),
methods,
rmarkdown (>= 1.10),
rmdfiltr,
yaml
base64enc,
bookdown (>= 0.6),
broom (>= 0.5.2.9001),
knitr (>= 1.10.5),
methods,
rmarkdown (>= 1.10),
rmdfiltr,
yaml
Suggests:
tinytex,
afex,
Expand All @@ -48,3 +48,5 @@ Encoding: UTF-8
RoxygenNote: 6.1.1
VignetteBuilder: knitr
Language: en-US
Remotes:
tidymodels/broom
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export(apa_print.glm)
export(apa_print.htest)
export(apa_print.list)
export(apa_print.lm)
export(apa_print.metafor)
export(apa_print.lsmobj)
export(apa_print.summary.Anova.mlm)
export(apa_print.summary.aov)
Expand Down
57 changes: 57 additions & 0 deletions R/apa_print_metafor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Format statistics (APA 6th edition)
#'
#' These methods take \code{rma} objects to create formatted character
#' strings to report the results in accordance with APA manuscript guidelines.
#'
#'
#' @return
#' \code{apa_print.metafor} returns a list containing the following components according to the input:
#'
#' \describe{
#' \item{\code{statistic}}{A named list of character strings giving the test statistic (z), and
#' \emph{p} value for each term.}
#' \item{\code{estimate}}{A named list of character strings giving the descriptive estimates and confidence intervals
#' for each term.}
#' \item{\code{full_result}}{A named list of character strings comprised of \code{estimate} and \code{statistic} for each term.}
#' \item{\code{table$estimate}}{A data.frame containing the study label, estimate, standard error, test statistic for each observed study and the overall meta-analytic estimate with standard error, test statistic, and p value. This table can be passed to \code{\link{apa_table}}.}
#' }
#' #' \item{\code{table$heterogeneity}}{A data.frame containing the number of observations, measure and method employed in the analysis as well as the I^2, Tau^2 Tau^2 standard error and results of Cochran test of heterogeneity. This table can be passed to \code{\link{apa_table}}.}
#' }
#' @family apa_print
#'
apa_print.metafor <- function(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, for S3-method dispatch to work this functions needs to be named after the S3 class it is ment to operate on. In this case, I think, it should be either the specific rma.uni or more general rma depending on how general this method should be. Right?

x
, est_name = NULL
, ci = 0.95
, in_paren = FALSE
, ...
){

if(!is(x, "rma")) stop("Object is not an rma object")
if(!is.null(est_name)) papaja:::validate(est_name, check_class = "character", check_length = 1)
if(is.null(est_name)) est_name <- "b"


apa_res <- papaja:::apa_print_container()

apa_res$estimate <- paste0("$", est_name, "$ = ", round(x$b, x$digits[["est"]]), " $95\\% CI$ = $[", round(x$ci.lb, digits = x$digits[["ci"]]), "$, $", round(x$ci.ub, digits = x$digits[["ci"]]), "]$")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of a general workflow I try to assemble the table element first and build the rest from it. Do you think this could be a sensible approach here too? It may simplify maintenance and may expose commonalities among functions that could be used further reuse code across methods.

apa_res$estimate <- setNames(apa_res$estimate, row.names(x$beta))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I never use setNames() but really should.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest it's just copied from how the variable names are set in metafor itself.


apa_res$statistic <- paste0("$z$ = ", round(x$zval, digits = x$digits[["test"]]), ", $p$ = ", printp(x$pval))
apa_res$statistic <- setNames(apa_res$statistic, row.names(x$beta))

apa_res$full_result <- paste(apa_res$estimate, apa_res$statistic)
apa_res$full_result <- setNames(apa_res$full_result, row.names(x$beta))

apa_res$table$estimate <- broom::tidy(x)

apa_res$table$heterogeneity <- broom::glance(x)
names(apa_res$table$heterogeneity) <- c("k", "measure", "method", "I$^2$", "H$^2$", "$\\tau^2$", "$\\tau^2$ se", "Cochran Q$_e$", "Cochran Q$_e$ $p$", "Cochran Q$_m$", "Cochran Q$_m$ $p$")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a nice next step would be to add variable labels using variable_labels().


apa_res
}