stmcg / estmeansd

Estimating the Sample Mean and Standard Deviation from Commonly Reported Quantiles in Meta-Analysis
2 stars 2 forks source link

Using bc.mean.sd with dplyr's mutate #2

Closed abduazizR closed 1 year ago

abduazizR commented 1 year ago

Hi,

First of all, thank you so much for the very powerful package. I am wondering if it's possible to utilize its functions to be called inside a mutate function. Here is an example:

library(tidyverse)
library(estmeansd)
tibble(
  age_min = c(10,20),
  age_max = c(50,60),
  age_median = c(30,40),
  n = c(150,160)
) |> 
  mutate(x = bc.mean.sd(min.val = age_min, max.val = age_max, med.val = age_median, n = n) |> _$est.mean )
#> Error in `mutate()`:
#> ℹ In argument: `x = `$`(...)`.
#> Caused by error in `if (is.na(n) | n < 3 | n > 1e+06) ...`:
#> ! the condition has length > 1
#> Backtrace:
#>      ▆
#>   1. ├─dplyr::mutate(...)
#>   2. ├─dplyr:::mutate.data.frame(...)
#>   3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   4. │   ├─base::withCallingHandlers(...)
#>   5. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   6. │     └─mask$eval_all_mutate(quo)
#>   7. │       └─dplyr (local) eval()
#>   8. ├─estmeansd::bc.mean.sd(...)
#>   9. │ └─estmeansd:::check_errors(...)
#>  10. └─base::.handleSimpleError(...)
#>  11.   └─dplyr (local) h(simpleError(msg, call))
#>  12.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

I hope you have solution for this

stmcg commented 1 year ago

Hi,

Thanks for posting.

I'm not very familiar with mutate, as I generally don't write R code with tidyverse packages. Are you trying apply the bc.mean.sd function to a collection of studies to estimate their means, etc? If so, I generally write a for loop to apply the bc.mean.sd function separately to each study (Note that bc.mean.sd does not allow vector inputs, which is likely the cause of the error message). Alternatively, the metamean function in the metamedian package accomplishes this for you, and long-form documentation of the package is available here.

Does this resolve your question? Also, feel free to reach out by email (sean_mcgrath@g.harvard.edu) if you have general questions about meta-analysis of studies reporting medians.

Best, Sean

abduazizR commented 1 year ago

Thank you for the very quick response. I am actually doing meta regression and I need to use age which is in some studies summarizes as mean and in others summarized as medians. I would like to convert the medians to means to proceed with the meta regression step. I have a column for each of element of the bc. mean. sd function. I wanted to create a new column that stores the transformed my mean.

stmcg commented 1 year ago

Got it. That makes sense.

I think the easiest solution would be to write a for loop that iterates over the rows of your data set, where in the ith iteration you apply the bc.mean.sd function to the data in the ith row of your data set. Does that make sense?

stmcg commented 1 year ago

I'm going to mark this as closed as it has been around a week, but please reach out if you have any lingering questions