wviechtb / metafor

A meta-analysis package for R
https://www.metafor-project.org
230 stars 52 forks source link

Bug re: conv.wald() function - Error: Cannot find the object/variable ('x[[yi.name]]') specified for the 'yi' argument.** #71

Closed jessistanbul closed 1 year ago

jessistanbul commented 1 year ago

Classification: Bug Report

(Pick one of: Bug Report, Feature Request, Enhancement Suggestion)

General questions about the use of the metafor package should not be asked here, but on the r-sig-meta-analysis mailing list (https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis). Anything posted here should really be related to the development of the package (including potential bug reports).

Summary

Dear developer,

I ran into the following error from R when using conv.wald() function.

I am trying to do a meta-analysis using the metafor package. I have 14 studies to be pooled together, out of which 10 studies came in 2*2 table format. So I used escalc() function to generate yi and vi. This step worked out fine, with the following codes:

macro.ovwt.m<-escalc(measure='OR',ai=t1d1,bi=t1d0,ci=t0d1,di=t0d0,data =macro.ovwt)

Screenshot 2023-04-27 at 8 36 37 PM

For the remaining 4 studies (currently missing yi and vi in the table), the data came in OR and 95% CI. So I wanted to use conv.wald() to convert those ORs and 95% CIs and get yi and vi to make the table complete. I wrote the following codes with reference from this website (https://wviechtb.github.io/metafor/reference/conv.wald.html) - and this is where I ran into the problem.

macro.ovwt.m<-conv.wald(out=oddsratio, ci.lb=lower, ci.ub=upper, data=macro.ovwt.m,transf=log) Error: Cannot find the object/variable ('x[[yi.name]]') specified for the 'yi' argument.

I tried to write codes following the codes from the same website (https://wviechtb.github.io/metafor/reference/conv.wald.html) to manually generate a data frame, with just those 4 studies corresponding to four rows. By doing this, I was able to manually calculate yi and vi. Output was given below.

dat<-data.frame(study=1:4, oddsratio=c(2.06,1.16,0.93,1.54),lower=c(1.50,0.75,0.21,0.74),upper=c(2.84,1.82,4.12,3.17) )

dat <- conv.wald(out=oddsratio, ci.lb=lower, ci.ub=upper, data=dat, transf=log) dat

Yes - I understand that I could output the previous table (with yi and vi missing for the four studies) as a csv. file and then manually fill in the table, but I realize this is not ideal and can be manually prone to errors and burdensome as the number of studies gets more.

Could you please help me to see why I got the error message when running the conv.wald() function? My next step would be to use res.macro.ovwt<-rma(yi,vi,data=macro.ovwt.m,method='DL') for the meta-analysis and then to generate forest plot.

Thank you so much for your help!! Really appreciate it. :)

-Jessie

Reproducible Example (if applicable)

If applicable, please provide a minimal and fully reproducible example. Remove any superfluous code that is not pertinent to the issue at hand and provide a small dataset together with the code so that it can actually be run (the dput() function is extremely useful for this; or use one of the datasets that comes with the metafor package). See also:

# use this for posting code (if applicable)

macro.ovwt<-structure(list(y = c("macrosomia", "macrosomia", "macrosomia", 
"macrosomia", "macrosomia", "macrosomia", "macrosomia", "macrosomia", 
"macrosomia", "macrosomia", "macrosomia", "macrosomia", "macrosomia", 
"macrosomia"), x = c("ovwt", "ovwt", "ovwt", "ovwt", "ovwt", 
"ovwt", "ovwt", "ovwt", "ovwt", "ovwt", "ovwt", "ovwt", "ovwt", 
"ovwt"), study.id = c(1116L, 1993L, 4385L, 5029L, 5850L, 6069L, 
3776L, 2647L, 6071L, 3934L, 3533L, 5001L, 5534L, 3836L), author = c("Sun", 
"Nobumoto", "Leng", "Ouzounian", "Tavares", "Wang", "Hashemipour", 
"Barquiel", "Wang", "Ijas", "Fuka", "Olmos", "Scifres", "Hilden"
), country = c("China", "Japan", "China", "USA", "Brazil", "China", 
"Iran", "Spain", "China", "Finland", "Fiji", "Chile", "USA", 
"Sweden"), year = c(2014L, 2015L, 2015L, 2011L, 2019L, 2015L, 
2018L, 2018L, 2018L, 2019L, 2020L, 2012L, 2015L, 2016L), ntot = c(1310L, 
491L, 1263L, 1502L, 116L, 587L, 305L, 3284L, 601L, 5680L, 235L, 
251L, 1344L, 13057L), cont_expo = c(NA, NA, 1L, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), sensitivity = c(NA, 1L, NA, NA, 
NA, 1L, NA, NA, NA, NA, NA, 1L, NA, NA), n_expo = c(3L, 2L, 3L, 
3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, NA, NA), expo_cutoff = c("24, 24-28, 28+ (obese)", 
"25, 25+", "24, 24-28, 28+", "25, 25-30, 30+", "25, 25-30, 30+", 
"25, 25+", "25, 25-30, 30+", "25, 25-30, 30+", "24, 24-28, 28+", 
"25, 25-30, 30+", "25, 25-30, 30+", "25, 25+", "25, 25-30, 30+", 
"25, 25-30, 30+"), updated_ntot = c(1132L, 491L, NA, 1023L, 67L, 
587L, 246L, 2921L, 502L, 3643L, 75L, 251L, 707L, 8086L), adjusted_or = c(0L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L), t0d1 = c(7L, 
3L, NA, NA, 0L, 30L, 7L, 56L, 31L, 48L, NA, 6L, NA, 169L), t0d0 = c(762L, 
406L, NA, NA, 32L, 443L, 56L, 2100L, 363L, 1711L, NA, 119L, NA, 
3945L), control = c(769L, 409L, NA, NA, 32L, 473L, 63L, 2156L, 
394L, 1759L, NA, 125L, NA, 4114L), t1d1 = c(80L, 1L, NA, NA, 
5L, 19L, 35L, 24L, 9L, 83L, NA, 15L, NA, 274L), t1d0 = c(283L, 
81L, NA, NA, 30L, 95L, 148L, 741L, 99L, 1801L, NA, 111L, NA, 
3698L), treat = c(363L, 82L, NA, NA, 35L, 114L, 183L, 765L, 108L, 
1884L, NA, 126L, NA, 3972L), oddsratio = c(1.64, 1.67, 2.06, 
1.16, NA, NA, NA, NA, NA, NA, 0.93, NA, 1.54, NA), lower = c(1.14, 
0.17, 1.5, 0.75, NA, NA, NA, NA, NA, NA, 0.21, NA, 0.74, NA), 
    upper = c(2.36, 16.3, 2.84, 1.82, NA, NA, NA, NA, NA, NA, 
    4.12, NA, 3.17, NA)), row.names = c(NA, 14L), class = "data.frame")

macro.ovwt.m<-escalc(measure='OR',ai=t1d1,bi=t1d0,ci=t0d1,di=t0d0,data =macro.ovwt)
macro.ovwt.m<-conv.wald(out=oddsratio, ci.lb=lower, ci.ub=upper, data=macro.ovwt.m,transf=log)

##below codes will manually produce the yi and vi for the four studies:
dat<-data.frame(study=1:4,
                oddsratio=c(2.06,1.16,0.93,1.54),lower=c(1.50,0.75,0.21,0.74),upper=c(2.84,1.82,4.12,3.17)
                )

dat

  study oddsratio lower upper
1     1      2.06  1.50  2.84
2     2      1.16  0.75  1.82
3     3      0.93  0.21  4.12
4     4      1.54  0.74  3.17

dat <- conv.wald(out=oddsratio, ci.lb=lower, ci.ub=upper, data=dat, transf=log)
dat

  study oddsratio lower upper      yi     vi 
1     1      2.06  1.50  2.84  0.7227 0.0265 
2     2      1.16  0.75  1.82  0.1484 0.0511 
3     3      0.93  0.21  4.12 -0.0726 0.5766 
4     4      1.54  0.74  3.17  0.4318 0.1377 

# use this for posting output (if applicable)

## Notes

Describe any debugging steps you've taken yourself. If you've found a workaround, please provide it here.

## sessionInfo()

Post output of `sessionInfo()` below:

# put output here
wviechtb commented 1 year ago

Please post a fully reproducible example that generates the error.

jessistanbul commented 1 year ago

Hey,

How can I 'upload' the dataset here to run the codes and get the error?

wviechtb commented 1 year ago

You can't. Use dput(). See the links that are provided in the issue template.

jessistanbul commented 1 year ago

Hi!

I just edited my question by virtually putting the dataset up by using the dput() function. I was able to reproduce the error. Could you please take a look? Thanks!

wviechtb commented 1 year ago

Thanks - much better! Took me a while to track down where this bug is coming from. Without going into the details, if you rename your x variable, then it will work. So just run names(macro.ovwt)[which(names(macro.ovwt) == "x")] <- "xvar" first. A bit difficult to explain why this is needed for now, but I can fix this bug in the next / development version of the package. Thanks for reporting this because it made me realize something I had not considered before.

jessistanbul commented 1 year ago

Hi. Thanks for your response! I just tried, and it worked!

I really, really appreciate your timely help. Your package is really helpful.