<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>Dear Will,</div>
<div> </div>
<div>I misread and did not know that you were working with a list (!) of models. Sorry! So I changed my example a bit. </div>
<div> </div>
<div>
<div>library(metafor)<br/>
</div>
<div>dat <- dat.baskerville2012<br/>
dat</div>
<div> </div>
<div>#create a list of dfs with 10 replications of dat<br/>
dat_list <- replicate(10, list(dat), simplify = FALSE)</div>
<div> </div>
<div># fit random-effects model for each data set in list</div>
<div> </div>
<div># Define a function to apply metafor::rma()<br/>
apply_rma <- function(dat) {<br/>
metafor::rma(dat$smd, dat$se^2, data = dat, method = "ML", digits = 3)<br/>
}</div>
<div> </div>
<div># Apply the function to each element of dat_list using lapply<br/>
res <- lapply(dat_list, apply_rma)</div>
<div> </div>
<div>#run selection model for each list</div>
<div> </div>
<div># Define a function to apply metafor::selmodel()<br/>
apply_selmodel <- function(x) {<br/>
metafor::selmodel(x, type = "beta")<br/>
}</div>
<div> </div>
<div># Apply the function to each element of res using lapply<br/>
sel <- lapply(res, apply_selmodel)</div>
<div> </div>
<div> </div>
<div># Define a function to calculate the confidence interval for tau^2<br/>
calculate_tau2_ci <- function(model) {<br/>
confint(model, tau2 = TRUE)<br/>
}</div>
<div> </div>
<div># Apply the function to each element of sel using lapply<br/>
tau2_ci <- lapply(sel, calculate_tau2_ci)</div>
<div> </div>
<div># Define a function to extract df from a given model<br/>
extract_tau <- function(model) {<br/>
df <- model$random<br/>
return(df)<br/>
}</div>
<div> </div>
<div># Define a function to extract all tau2 from all lists<br/>
extract_all_df <- function(tau2_ci) {<br/>
all_df <- lapply(tau2_ci, extract_df)<br/>
return(all_df)<br/>
}</div>
<div> </div>
<div>all_tau2 <- extract_all_df(tau2_ci)</div>
<div>all_tau2</div>
<div> </div>
<div>#put results in a df</div>
<div>c <- as.data.frame(all_tau2)</div>
</div>
<div>
<div>
<div>Best Regards,</div>
<div> </div>
<div>Tobias</div>
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Gesendet:</b> Sonntag, 10. März 2024 um 23:40 Uhr<br/>
<b>Von:</b> "Will Hopkins via R-sig-meta-analysis" <r-sig-meta-analysis@r-project.org><br/>
<b>An:</b> "'R Special Interest Group for Meta-Analysis'" <r-sig-meta-analysis@r-project.org><br/>
<b>Cc:</b> "Will Hopkins" <willthekiwi@gmail.com><br/>
<b>Betreff:</b> Re: [R-meta] Extracting values from a list of confint() objects</div>
<div name="quoted-content">Tobias, that dataset appears to be for a single meta, not a set of many (simulated) metas for checking bias and coverage of the fixed effects and tau2. Nevertheless I tried the last few steps on my list of confint objects (adjusted_confint).<br/>
<br/>
> #get an overview of the list, so that you can see what you need to extract<br/>
<br/>
> str(adjusted_confint)<br/>
<br/>
List of 2500<br/>
<br/>
$ :List of 2<br/>
<br/>
..$ fixed : num [1:2, 1:3] 1.5 1.74 1.05 1.1 1.95 ...<br/>
<br/>
.. ..- attr(*, "dimnames")=List of 2<br/>
<br/>
.. .. ..$ : chr [1:2] "intrcpt" "mods"<br/>
<br/>
.. .. ..$ : chr [1:3] "estimate" "ci.lb" "ci.ub"<br/>
<br/>
..$ digits: Named num [1:9] 4 4 4 4 4 4 4 4 4<br/>
<br/>
.. ..- attr(*, "names")= chr [1:9] "est" "se" "test" "pval" ...<br/>
<br/>
..- attr(*, "class")= chr "confint.rma"<br/>
<br/>
$ :List of 2<br/>
<br/>
..$ fixed : num [1:2, 1:3] 1.665 1.191 1.148 0.563 2.183 ...<br/>
<br/>
.. ..- attr(*, "dimnames")=List of 2<br/>
<br/>
.. .. ..$ : chr [1:2] "intrcpt" "mods"<br/>
<br/>
.. .. ..$ : chr [1:3] "estimate" "ci.lb" "ci.ub"<br/>
<br/>
..$ digits: Named num [1:9] 4 4 4 4 4 4 4 4 4<br/>
<br/>
.. ..- attr(*, "names")= chr [1:9] "est" "se" "test" "pval" ...<br/>
<br/>
..- attr(*, "class")= chr "confint.rma"<br/>
<br/>
etc., etc.<br/>
<br/>
I find this hard to understand, but it looks like the stats for tau2 may not be in there.<br/>
<br/>
The next step didn't work:<br/>
<br/>
> #extract tau2 and tau from list<br/>
<br/>
> adjsel <- as.data.frame(adjusted_confint[["random"]])<br/>
<br/>
> adjsel<br/>
<br/>
data frame with 0 columns and 0 rows<br/>
<br/>
<br/>
<br/>
Nor did the alternative version you suggested:<br/>
<br/>
> adjsel <- as.data.frame(adjusted_confint$random)<br/>
<br/>
> adjsel<br/>
<br/>
data frame with 0 columns and 0 rows<br/>
<br/>
<br/>
<br/>
However, my adjust_conflim was generated with this line of code within a function:<br/>
<br/>
adjusted_confint <- confint(xxx, level=90, fixed=TRUE, random=TRUE, type="PL")<br/>
<br/>
<br/>
<br/>
So I made it:<br/>
<br/>
adjusted_confint <- confint(xxx, level=90, fixed=FALSE, tau2=TRUE, type="PL")<br/>
<br/>
<br/>
<br/>
Now str(adjusted_confint) gave this:<br/>
<br/>
$ :List of 5<br/>
<br/>
..$ random : num [1:2, 1:3] 0.909 0.954 0.303 0.55 2.108 ...<br/>
<br/>
.. ..- attr(*, "dimnames")=List of 2<br/>
<br/>
.. .. ..$ : chr [1:2] "tau^2" "tau"<br/>
<br/>
.. .. ..$ : chr [1:3] "estimate" "ci.lb" "ci.ub"<br/>
<br/>
..$ digits : Named num [1:9] 4 4 4 4 4 4 4 4 4<br/>
<br/>
.. ..- attr(*, "names")= chr [1:9] "est" "se" "test" "pval" ...<br/>
<br/>
..$ ci.null: logi FALSE<br/>
<br/>
..$ lb.sign: chr ""<br/>
<br/>
..$ ub.sign: chr ""<br/>
<br/>
..- attr(*, "class")= chr "confint.rma"<br/>
<br/>
$ :List of 5<br/>
<br/>
etc., etc.<br/>
<br/>
<br/>
<br/>
So we're nearly there. I don’t know how to translate the above information into code that will extract the values of tau^2, tau, and their confidence limits. Your previous suggestions, adjsel <- as.data.frame(adjusted_confint[["random"]]) and adjsel <- as.data.frame(adjusted_confint$random), still gave:<br/>
<br/>
data frame with 0 columns and 0 rows<br/>
<br/>
And my attempts to use sapply() still don't work. I hope you can suggest something else, or point out any silly mistake I might be making.<br/>
<br/>
<br/>
<br/>
Thanks<br/>
<br/>
<br/>
<br/>
Will<br/>
<br/>
<br/>
<br/>
From: R-sig-meta-analysis <r-sig-meta-analysis-bounces@r-project.org> On Behalf Of Tobias Saueressig via R-sig-meta-analysis<br/>
Sent: Monday, March 11, 2024 12:42 AM<br/>
To: r-sig-meta-analysis@r-project.org<br/>
Cc: Tobias Saueressig <t.saueressig@gmx.de><br/>
Subject: Re: [R-meta] Extracting values from a list of confint() objects<br/>
<br/>
<br/>
<br/>
Dear Will,<br/>
<br/>
<br/>
<br/>
you can do the following to get the values:<br/>
<br/>
<br/>
<br/>
install.packages("metafor")<br/>
<br/>
library(metafor)<br/>
<br/>
<br/>
<br/>
dat <- dat.baskerville2012<br/>
dat<br/>
<br/>
<br/>
<br/>
# fit random-effects model<br/>
res <- rma(smd, se^2, data=dat, method="ML", digits=3)<br/>
res<br/>
<br/>
<br/>
<br/>
sel <- selmodel(res, type="beta")<br/>
sel<br/>
<br/>
<br/>
<br/>
#calculate confidence interval for tau2<br/>
a <- confint(sel,tau2=TRUE)<br/>
<br/>
<br/>
<br/>
#get an overview of the list, so that you can see what you need to extract<br/>
str(a)<br/>
<br/>
<br/>
<br/>
#extract tau2 and tau from list<br/>
c <- as.data.frame(a[["random"]])<br/>
c<br/>
<br/>
<br/>
<br/>
#you can also use $ to get access<br/>
d <- as.data.frame(a$random)<br/>
d<br/>
<br/>
<br/>
<br/>
#now you can export the data e.g. to excel<br/>
<br/>
install.packages("openxlsx")<br/>
<br/>
library(openxlsx)<br/>
<br/>
<br/>
<br/>
# Export data to an Excel file<br/>
<br/>
write.xlsx(d, "tau2_data.xlsx", rowNames = FALSE)<br/>
<br/>
<br/>
<br/>
Best Regards,<br/>
<br/>
<br/>
<br/>
Tobias<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
Gesendet: Sonntag, 10. März 2024 um 05:58 Uhr<br/>
Von: "Will Hopkins via R-sig-meta-analysis" < <mailto:r-sig-meta-analysis@r-project.org> r-sig-meta-analysis@r-project.org><br/>
An: "'R Special Interest Group for Meta-Analysis'" < <mailto:r-sig-meta-analysis@r-project.org> r-sig-meta-analysis@r-project.org><br/>
Cc: "Will Hopkins" < <mailto:willthekiwi@gmail.com> willthekiwi@gmail.com><br/>
Betreff: [R-meta] Extracting values from a list of confint() objects<br/>
<br/>
With the help of ChatGPT, I have managed to extract values from a list of<br/>
rma() objects and values from a list of selmodel() objects derived from the<br/>
list of rma() objects. However, I cannot extract values from a list of<br/>
confint() objects derived from the selmodel() objects. I hope I am using the<br/>
right jargon here. If not, please be gentle, as I am still on the steep part<br/>
of the learning curve.<br/>
<br/>
<br/>
<br/>
For example, this code (mysteriously) extracts tau2 from meta_results, which<br/>
is a list of rma objects:<br/>
<br/>
tau2 <- sapply(meta_results, function(x) x$tau2).<br/>
<br/>
<br/>
<br/>
And this code (equally mysteriously) extracts the SE of tau2 from<br/>
adjusted_meta, which is a list of selmodel objects derived by applying<br/>
selmodel() to meta_results:<br/>
<br/>
seltau2se <- sapply(adjusted_meta, function(x) x$se.tau2).<br/>
<br/>
<br/>
<br/>
But I have a list I have called adjusted_confint, which is a list of confint<br/>
objects derived by applying confint to the list adjusted_meta. I don't know<br/>
how to use sapply on this list to extract the confidence limits for tau2<br/>
that are contained within this list. I have "copied blindly" the syntax of<br/>
the other sapply statements without success. Basically I don't know what to<br/>
put for the x$..., and everything I have tried fails. ChatGPT was not<br/>
helpful here. I hope a real maven can help here. Thanks.<br/>
<br/>
<br/>
<br/>
Will<br/>
<br/>
<br/>
[[alternative HTML version deleted]]<br/>
<br/>
_______________________________________________<br/>
R-sig-meta-analysis mailing list @ <mailto:R-sig-meta-analysis@r-project.org> R-sig-meta-analysis@r-project.org<br/>
To manage your subscription to this mailing list, go to:<br/>
<<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis</a>> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis</a><br/>
<br/>
<br/>
[[alternative HTML version deleted]]<br/>
<br/>
_______________________________________________<br/>
R-sig-meta-analysis mailing list @ R-sig-meta-analysis@r-project.org<br/>
To manage your subscription to this mailing list, go to:<br/>
<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis</a></div>
</div>
</div>
</div></div></body></html>