Meta-analysis in Python

Introduction

Python also has a package called Python Meta that performs meta-analysis, but for the purpose of studying, this time I will implement a meta-analysis with pandas. I did.

There are two main types of meta-analysis models, ** Fixed Effect Model ** and ** Random Effects Model **, so let's implement each one.

Sample data

This time, I will experiment using the following data.

import pandas as pd
import numpy as np
data = pd.DataFrame({
    "g": [0.12, 0.23, 0.34, 0.45, 0.42, 0.39, 0.49, 0.65, 0.76, 0.87],
    "V": [0.01, 0.04, 0.03, 0.02, 0.01, 0.02, 0.03, 0.04, 0.02, 0.01]
})

Here, g is Hedges'g (= effect size) and V is the variance of effect size.

Fixed effects model

In the fixed effects model, the reciprocal of the variance of the effect size is simply the weight of the item. Therefore, the average effect size to be calculated is calculated as the effect size of each item weighted and divided by the total weight. The calculation formula is as follows. Even Python can be written in 3 lines.

#Fixed effects model
data['W'] = 1 / data.V
data['Wg'] = data['g'] * data['W']
result = data['Wg'].sum() / data['W'].sum()
result
>> 0.4776

Random effects model

The random effects model makes the weight calculation a bit more complicated. In order to take into account the variability of effect sizes between studies, we take the average effect size and then incorporate the deviation of each item into the weight calculation. The calculation formula is as follows.

g_hat = data.g.mean()
Q = (data.W * (data.g - g_hat)**2).sum()
data['W2'] = data.W ** 2
C = data.W.sum() - (data.W2.sum()/data.W.sum())
d = len(data) - 1
#Inter-research variance
if (Q-d) > 0:
    V_between = (Q - d) / C
else:
    V_between = 0
data['V_str'] = data.V + V_between
data['W_str'] = 1 / data.V_str
result = (data.g * data.W_str).sum() / data.W_str.sum()
result

Other

You can calculate the 95% confidence interval from the sum of the calculated weights in both the fixed effects model and the random effects model.

std_err = np.sqrt(1/data.W_str.sum())
lwr = result - 1.96 * std_err
upr = result + 1.96 * std_err
[lwr, upr]

References

["Introduction to Meta-Analysis for Systematic Review of Psychology / Educational Research" edited by Takeshi Yamada and Toshiya Inoue](https://www.amazon.co.jp/%E3%83%A1%E3%82%BF% E5% 88% 86% E6% 9E% 90% E5% 85% A5% E9% 96% 80-% E5% BF% 83% E7% 90% 86% E3% 83% BB% E6% 95% 99% E8 % 82% B2% E7% A0% 94% E7% A9% B6% E3% 81% AE% E7% B3% BB% E7% B5% B1% E7% 9A% 84% E3% 83% AC% E3% 83 % 93% E3% 83% A5% E3% 83% BC% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AB-% E5% B1% B1% E7% 94% B0-% E5% 89% 9B% E5% 8F% B2 / dp / 4130420720)

Recommended Posts

Meta-analysis in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python