does safely play nice with rma?

unpicking what happened with metafor::rma and purrr::safely

Charles T. Gray https://twitter.com/cantabile
02-25-2019

# packages used in this post
library(tidyverse)
library(metasim)
library(metafor)
library(metabroom)

# for reproducibility
set.seed(38)

lm


x <- seq(0, 10)

# simulate some data
(lm_data <- x %>% {
  tibble(
    x = .,
    y = rnorm(length(.), mean = 1.2 * ., sd = 2)
  )
})

# A tibble: 11 x 2
       x      y
   <int>  <dbl>
 1     0 -0.507
 2     1 -0.911
 3     2  3.77 
 4     3  3.65 
 5     4  1.46 
 6     5  3.03 
 7     6  8.06 
 8     7  8.42 
 9     8 11.4  
10     9  9.98 
11    10 12.1  

# and take a look
lm_data %>% 
  ggplot(aes(x  = x, y = y)) +
  geom_smooth(method = "lm", 
              colour = "darkgrey",
              linetype = "dotted") +
  geom_point() 

Now, I expect to see the same results if I fit a linear model using the lm function. First, let’s see what I can get out with pluck.


# create safe version of lm
safe_lm <- safely(lm, otherwise = "lm model didn't work", quiet = FALSE)

# model data with lm and safely
lm_model <- lm_data %>% lm(y ~ x, data = .) 

safe_lm_model <- lm_data %>% safe_lm(y ~ x, data = .) 

# lm model results
lm_model %>% str(0)

List of 12
 - attr(*, "class")= chr "lm"

# safe lm model results, same but in list with errors
safe_lm_model %>% str(1)

List of 2
 $ result:List of 12
  ..- attr(*, "class")= chr "lm"
 $ error : NULL

rma

So, that all seemed cromulent enough, and just as the log example from the documentation had me following along.

Now to try with metafor::rma.


# simulate meta-analysis data
rma_data <- sim_n(k = 7) %>% 
  sim_stats(wide = TRUE) %>% 
   escalc(
     data = .,
     measure = "SMD",
     m1i = mean_c,
     m2i = mean_i,
     sd1i = sd_c,
     sd2i = sd_i,
     n1i = n_c,
     n2i = n_i
   ) 

# peek at the data
rma_data %>% str(0)

Classes 'escalc' and 'data.frame':  7 obs. of  22 variables:
 - attr(*, "digits")= num 4
 - attr(*, "yi.names")= chr "yi"
 - attr(*, "vi.names")= chr "vi"

# rma model works fine
rma_model <- rma_data %>% 
    rma(yi = yi, vi = vi, data = ., slab = study)

# take a look at results
rma_model %>% metabroom::tidy()

# A tibble: 1 x 6
   coef    se  p_value ci_lb ci_ub  tau2
  <dbl> <dbl>    <dbl> <dbl> <dbl> <dbl>
1 -55.4  4.06 1.84e-42 -63.4 -47.5  103.

# plot data
rma_model %>% forest()

error


# set up safely function
safe_rma <- safely(rma, 
                   otherwise = "rma model didn't work", 
                   quiet = FALSE)
# 
# safe_rma_model <- rma_data %>% 
#     safe_rma(yi = yi, vi = vi, data = ., slab = study)
# 
# 
# # plot data
# safe_rma_model %>% forest()

Trying wrap metafor::rma with purrr::safely results in the following error:


Error: Can't convert a `rma.uni/rma` object to function

So, I have convinced myself, now. For some reason, purrr::safely does not play nicely with metafor::rma.

what to do instead?

I suspect I’ll need to do something with tryCatch. However, I think I’m ready to dive into James’ collateral package.

Citation

For attribution, please cite this work as

Gray (2019, Feb. 25). measured.: does safely play nice with rma?. Retrieved from https://fervent-hypatia-7b7343.netlify.com/posts/2019-02-25-does-safely-play-nice-with-rma/

BibTeX citation

@misc{gray2019does,
  author = {Gray, Charles T.},
  title = {measured.: does safely play nice with rma?},
  url = {https://fervent-hypatia-7b7343.netlify.com/posts/2019-02-25-does-safely-play-nice-with-rma/},
  year = {2019}
}