循环遍历行以在R中运行元分析。

huangapple go评论73阅读模式
英文:

loop through rows to run meta analysis in R

问题

I have a dataframe of summary statistics results [b/se/p] for different studies [different exposures]

Create the sample dataframe

dat <- data.frame(
  Study = rep(c("Study 1", "Study 2", "Study 3", "Study 4", "Study 5"), each = 2),
  Stage = rep(c("Discovery", "Replication"), 5),
  b = c(0.1, 0.2, 0.15, 0.3, 0.05, 0.1, 0.25, 0.4, 0.08, 0.15),
  se = c(0.05, 0.06, 0.07, 0.08, 0.09, 0.06, 0.07, 0.08, 0.1, 0.12))

Print the dataframe

print(dat)

I want to run a meta-analysis for each pair of studies - so for 'study 1' or 'study 2', meta analysing the two sets of results - one result being Discovery and the other being Replication.

I have tried:

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c("Discovery", "Replication")) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    estimate = stage_data$b,
    se = stage_data$se,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] <- meta_result
}

# Print the meta-analysis results
print(meta_results)

However I get the error

Error in meta::metagen(estimate = stage_data$b, se = stage_data$se, data = stage_data) : 
  argument 2 matches multiple formal arguments

So I don't think I am specifying correctly that there is a set of results for each stage for each study.

英文:

I have a dataframe of summary statistics results [b/se/p] for different studies [different exposures]

Create the sample dataframe

dat <- data.frame(
  Study = rep(c("Study 1", "Study 2", "Study 3", "Study 4", "Study 5"), each = 2),
  Stage = rep(c("Discovery", "Replication"), 5),
  b = c(0.1, 0.2, 0.15, 0.3, 0.05, 0.1, 0.25, 0.4, 0.08, 0.15),
  se = c(0.05, 0.06, 0.07, 0.08, 0.09, 0.06, 0.07, 0.08, 0.1, 0.12))

Print the dataframe

print(dat)

I want to run a meta analysis for each pair of studies - so for 'study 1' or 'study 2', meta analysing the two sets of results - one result being Discovery and the other being Replication.

I have tried:

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c("Discovery", "Replication")) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    estimate = stage_data$b,
    se = stage_data$se,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] <- meta_result
}

# Print the meta-analysis results
print(meta_results)

However I get the error

Error in meta::metagen(estimate = stage_data$b, se = stage_data$se, data = stage_data) : 
  argument 2 matches multiple formal arguments

So I don't think I am specifying correctly that there is a set of results for each stage for each study.

答案1

得分: 2

If you check ?meta::metagen, you'll see that there isn't a estimate and se arguments. The correct argument names are TE and seTE.

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c("Discovery", "Replication")) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] <- meta_result
}

Result:

$Discovery
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.1316 [0.0706; 0.1927] 4.23 < 0.0001
Random effects model 0.1321 [0.0661; 0.1981] 3.92 < 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0007 [0.0000; 0.0447]; tau = 0.0260 [0.0000; 0.2113]
 I^2 = 9.5% [0.0%; 81.2%]; H = 1.05 [1.00; 2.30]

Test of heterogeneity:
    Q d.f. p-value
 4.42    4  0.3524

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

$Replication
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.2167 [0.1527; 0.2807] 6.63 < 0.0001
Random effects model 0.2285 [0.1201; 0.3368] 4.13 < 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0092 [0.0000; 0.1134]; tau = 0.0959 [0.0000; 0.3368]
 I^2 = 61.9% [0.0%; 85.7%]; H = 1.62 [1.00; 2.64]

Test of heterogeneity:
     Q d.f. p-value
10.50    4  0.0327

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

Bonus: what does the error mean?

R has partial matching for function parameters. That's why you can do seq(1, by = 1, length = 10), even though seq doesn't have a length argument, only a length.out argument. R has matched the length to length.out. In your case, as R couldn't find a se argument, it tried matching, but found two possible matches for that word, and that's what the error means.

I recommend avoiding partial matching. Check this SO question: https://stackoverflow.com/questions/15264994/prevent-partial-argument-matching

Edit

Changing the order of the loop:

# Create an empty list to store the meta-analysis results
meta_results <- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (study in unique(dat$Study)) {
  # Filter the data for the current stage
  stage_data <- dat[dat$Study == study, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result <- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[study]] <- meta_result
}

# Print the meta-analysis results
print(meta_results)
英文:

If you check ?meta::metagen, you'll see that there isn't a estimate and se arguments. The correct argument names are TE and seTE.

# Create an empty list to store the meta-analysis results
meta_results &lt;- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (stage in c(&quot;Discovery&quot;, &quot;Replication&quot;)) {
  # Filter the data for the current stage
  stage_data &lt;- dat[dat$Stage == stage, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result &lt;- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[stage]] &lt;- meta_result
}

Result:

$Discovery
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.1316 [0.0706; 0.1927] 4.23 &lt; 0.0001
Random effects model 0.1321 [0.0661; 0.1981] 3.92 &lt; 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0007 [0.0000; 0.0447]; tau = 0.0260 [0.0000; 0.2113]
 I^2 = 9.5% [0.0%; 81.2%]; H = 1.05 [1.00; 2.30]

Test of heterogeneity:
    Q d.f. p-value
 4.42    4  0.3524

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

$Replication
Number of studies combined: k = 5

                                      95%-CI    z  p-value
Common effect model  0.2167 [0.1527; 0.2807] 6.63 &lt; 0.0001
Random effects model 0.2285 [0.1201; 0.3368] 4.13 &lt; 0.0001

Quantifying heterogeneity:
 tau^2 = 0.0092 [0.0000; 0.1134]; tau = 0.0959 [0.0000; 0.3368]
 I^2 = 61.9% [0.0%; 85.7%]; H = 1.62 [1.00; 2.64]

Test of heterogeneity:
     Q d.f. p-value
 10.50    4  0.0327

Details on meta-analytical method:
- Inverse variance method
- Restricted maximum-likelihood estimator for tau^2
- Q-Profile method for confidence interval of tau^2 and tau

Bonus: what does the error mean?

R has partial matching for function parameters. That's why you can do seq(1, by = 1, length = 10), even though seq doesn't have a length argument, only a length.out argument. R has matched the length to length.out. In your case, as R couldn't find a se argument, it tried matching, but found two possible matches for that word, and that's what the error means.

I recommend avoiding partial matching. Check this SO question: https://stackoverflow.com/questions/15264994/prevent-partial-argument-matching

Edit

Changing the order of the loop:

# Create an empty list to store the meta-analysis results
meta_results &lt;- list()

# Perform meta-analysis for each stage (Discovery and Replication)
for (study in unique(dat$Study)) {
  # Filter the data for the current stage
  stage_data &lt;- dat[dat$Study == study, ]
  
  # Run the meta-analysis using appropriate functions from the meta package
  meta_result &lt;- meta::metagen(
    TE = Effect,
    seTE = Std_Error,
    data = stage_data
  )
  
  # Store the meta-analysis result in the list
  meta_results[[study]] &lt;- meta_result
}

# Print the meta-analysis results
print(meta_results)

答案2

得分: 1

你也可以通过使用 purrr 来简化,例如:

dat |&gt; 
  split(dat$Study) |&gt; 
  purrr::map(~meta::metagen(
    TE = b,
    seTE = se,
    data = .x
  ))
英文:

You could also simplify by using purrr, e.g.

dat |&gt; 
  split(dat$Study) |&gt; 
  purrr::map(~meta::metagen(
    TE = b,
    seTE = se,
    data = .x
  ))

huangapple
  • 本文由 发表于 2023年5月10日 20:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定