Skip to content

Commit

Permalink
Merge pull request #62 from egouldo/29-functionalise-ms-sm3
Browse files Browse the repository at this point in the history
Functionalise fit_MA_mv #29
  • Loading branch information
egouldo authored Jul 29, 2024
2 parents 73f9210 + 2e5c78d commit a471343
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 24 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ManyEcoEvo
Title: Meta-analyse data from 'Many-Analysts' style studies
Version: 2.0.0.9000
Version: 2.0.0.9001
Authors@R: c(person(given = "Elliot",
family = "Gould",
email = "elliot.gould@unimelb.edu.au",
Expand Down Expand Up @@ -68,7 +68,7 @@ Remotes:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
URL: https://github.com/egouldo/ManyEcoEvo,
https://egouldo.github.io/ManyEcoEvo/
BugReports: https://github.com/egouldo/ManyEcoEvo/issues
Expand Down
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export(cube_back)
export(divide_back)
export(est_to_zr)
export(exclude_extreme_VZ)
export(fit_MA_mv)
export(fit_boxcox_ratings_cat)
export(fit_boxcox_ratings_cont)
export(fit_boxcox_ratings_ord)
Expand Down Expand Up @@ -103,7 +104,6 @@ export(summarise_variable_counts)
export(validate_predictions)
export(validate_predictions_df_blue_tit)
export(validate_predictions_df_euc)
import(cli)
import(dplyr)
import(ggbeeswarm)
import(ggplot2)
Expand Down Expand Up @@ -134,6 +134,8 @@ importFrom(dplyr,rename)
importFrom(dplyr,right_join)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(pointblank,col_vals_not_null)
importFrom(pointblank,stop_if_not)
importFrom(forcats,fct_relevel)
importFrom(glue,glue)
importFrom(magrittr,"%>%")
Expand All @@ -151,6 +153,7 @@ importFrom(purrr,possibly)
importFrom(purrr,reduce)
importFrom(purrr,reduce2)
importFrom(purrr,set_names)
importFrom(rlang,enquo)
importFrom(rlang,is_na)
importFrom(rlang,is_null)
importFrom(rlang,na_chr)
Expand Down
32 changes: 32 additions & 0 deletions R/fit_MA_mv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Fit Meta-regression with random-effects
#' @description Fit a meta-regression model with random effects using the [metafor::rma.mv()] function from the `metafor` package to a dataframe containing the estimates and variances for the meta-analysis.
#'
#' @param effects_analysis A dataframe containing the estimates and variances for the meta-analysis.
#' @param Z_colname The name of the column containing the estimates.
#' @param VZ_colname The name of the column containing the variances.
#' @param estimate_type The type of estimate to be used in the model. One of "Zr", "y50", "y25", "y75", or "yi".
#'
#' @return A fitted model of class `c("rma.mv","rma")`.
#' @export
#' @import dplyr
#' @importFrom rlang enquo
#' @import metafor
#' @importFrom pointblank stop_if_not
#' @details
#' This function is a wrapper around the [metafor::rma.mv()] function from the `metafor` package. It takes a dataframe containing the estimates and variances for the meta-analysis, the name of the column containing the estimates, the name of the column containing the variances, and the type of estimate to be used in the model. It then fits a metaregression model with random-effects using the [metafor::rma.mv()] function called in [fit_metafor_mv()] and returns the fitted model.
#'
#' Nested random effects are included for `TeamIdentifier` and `TeamIdentifier/study_id`.
#' @examples
#' ManyEcoEvo_results$effects_analysis[[1]] %>%
#' fit_MA_mv(beta_estimate, beta_SE, "Zr")
fit_MA_mv <- function(effects_analysis = data.frame(), Z_colname, VZr_colname, estimate_type = character(1L)){
pointblank::stop_if_not(estimate_type %in% c("Zr", "yi", "y25", "y50", "y75"))

Zr <- effects_analysis %>% dplyr::pull({{Z_colname}})
VZr <- effects_analysis %>% dplyr::pull({{VZr_colname}})
mod <- ManyEcoEvo::fit_metafor_mv(estimate = Zr,
variance = VZr,
estimate_type = estimate_type,
data = effects_analysis)
return(mod)
}
16 changes: 11 additions & 5 deletions R/fit_metafor_mv.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param data Dataframe containing estimates and variances with case id column \code{study_id}.
#'
#' @return Object of class \code{rma.mv}
#' @import metafor
#' @export
#'
#' @examples
Expand All @@ -21,7 +22,7 @@
#' !is.infinite(VZr)) %>%
#' fit_metafor_mv(estimate = .$Zr, variance = .$VZr, estimate_type = "Zr", data = .)
fit_metafor_mv <- function(estimate, variance, estimate_type = character(1L), data){
cli::cli_h2(glue::glue("Fitting multivariate metaregression"))
cli::cli_h2(glue::glue("Fitting metaregression"))
match.arg(estimate_type, choices = c("Zr", "y50", "y25", "y75", "yi"), several.ok = FALSE)

if(estimate_type != "Zr"){ # you need to put SE^2 for y
Expand All @@ -30,24 +31,28 @@ fit_metafor_mv <- function(estimate, variance, estimate_type = character(1L), da

mod <- metafor::rma.mv(yi = estimate, # of type "Zr" or "ymed", "y25", "y75"
V = variance,
random=list(~1|TeamIdentifier/study_id),
random = list(~1|TeamIdentifier/study_id),
data = data,
sparse = TRUE,
# verbose = TRUE,
control=list(optimizer="nloptr", maxeval=1000),
control = list(optimizer="nloptr", maxeval=1000),
slab = data$study_id)
return(mod)
}


#' Fit reduced multivariate metaregression model
#' Fit reduced metaregression model
#'
#' @param estimate Numeric vector
#' @param variance Numeric vector
#' @param estimate_type Character vector of either "Zr", "y50", "y25", "y75".
#' @param data Dataframe containing estimates and variances with case id column \code{study_id}.
#'
#' @return Object of class \code{rma.mv}
#' @import metafor
#' @import dplyr
#' @importFrom glue glue
#' @importFrom cli cli_h2
#' @export
#'
#' @examples
Expand Down Expand Up @@ -80,8 +85,9 @@ fit_metafor_mv_reduced <- function(estimate, variance, estimate_type = character
data = data,
sparse = TRUE,
sigma2 = c(0, NA))

}

poss_fit_metafor_mv <- purrr::possibly(fit_metafor_mv,
otherwise = NA,
quiet = FALSE)
quiet = FALSE)
10 changes: 0 additions & 10 deletions R/meta_analyse_datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ meta_analyse_datasets <- function(MA_data){

cli::cli_h1(text = "Meta-analysing Datasets")

fit_MA_mv <- function(effects_analysis, Z_colname, VZ_colname, estimate_type){
Zr <- effects_analysis %>% pull({{Z_colname}})
VZr <- effects_analysis %>% pull({{VZ_colname}})
mod <- poss_fit_metafor_mv(estimate = Zr,
variance = VZr,
estimate_type = estimate_type,
data = effects_analysis)
return(mod)
}

if( any(str_detect(unique(MA_data$estimate_type), pattern = "Zr")) ){
# Must group by cols else multiple "effects_analysis" elements
# get passed to fit_MA_mv()
Expand Down
37 changes: 37 additions & 0 deletions man/fit_MA_mv.Rd

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

4 changes: 2 additions & 2 deletions man/fit_metafor_mv_reduced.Rd

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

9 changes: 5 additions & 4 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,15 @@
},
"cli": {
"Package": "cli",
"Version": "3.6.2",
"Version": "3.6.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"utils"
],
"Hash": "1216ac65ac55ec0058a6f75d7ca0fd52"
"Hash": "b21916dd77a27642b447374a5d30ecf3"

},
"clipr": {
"Package": "clipr",
Expand Down Expand Up @@ -2143,7 +2144,7 @@
},
"knitr": {
"Package": "knitr",
"Version": "1.46",
"Version": "1.48",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -2155,7 +2156,7 @@
"xfun",
"yaml"
],
"Hash": "6e008ab1d696a5283c79765fa7b56b47"
"Hash": "acf380f300c721da9fde7df115a5f86f"
},
"labeling": {
"Package": "labeling",
Expand Down

0 comments on commit a471343

Please sign in to comment.