[英]How to compute the confidence interval of the Difference in Differences method using Python?
我正在嘗試分析實驗前后每個用戶的總活躍分鍾數。 在這里,我在實驗前后包含了相關的用戶數據——variant_number = 0 表示對照組,而 1 表示治療組。 具體來說,我對平均值(每個用戶的平均總活躍分鍾數)感興趣。
首先,我計算了治療結果的前后差異和控制結果的前后差異(分別為-183.7 和 19.4)。 在這種情況下,差異的差異 = 203.1。
我想知道如何使用 Python 構建差異差異的 95% 置信區間? (如果需要,我可以提供更多代碼/上下文)
您可以使用線性 model 並測量交互效果(下面的group[T.1]:period[T.pre]
)。 這些模擬數據的平均差異差異為-223.1779
,交互作用的 p 值為 p < 5e-4 非常顯着,95% 置信區間為[-276.360, -169.995]
。
import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
import numpy as np
np.random.seed(14)
minutes_0_pre = np.random.normal(loc=478, scale=1821, size=39776)
minutes_1_pre = np.random.normal(loc=275, scale=1078, size=9921)
minutes_0_post = np.random.normal(loc=458, scale=1653, size=37425)
minutes_1_post = np.random.normal(loc=458, scale=1681, size=9208)
df = pd.DataFrame({'minutes': np.concatenate((minutes_0_pre, minutes_1_pre, minutes_0_post, minutes_1_post)),
'group': np.concatenate((np.repeat(a='0', repeats=minutes_0_pre.size),
np.repeat(a='1', repeats=minutes_1_pre.size),
np.repeat(a='0', repeats=minutes_0_post.size),
np.repeat(a='1', repeats=minutes_1_post.size))),
'period': np.concatenate((np.repeat(a='pre', repeats=minutes_0_pre.size + minutes_1_pre.size),
np.repeat(a='post', repeats=minutes_0_post.size + minutes_1_post.size)))
})
model = smf.glm('minutes ~ group * period', df, family=sm.families.Gaussian()).fit()
print(model.summary())
Output:
Generalized Linear Model Regression Results
==============================================================================
Dep. Variable: minutes No. Observations: 96330
Model: GLM Df Residuals: 96326
Model Family: Gaussian Df Model: 3
Link Function: identity Scale: 2.8182e+06
Method: IRLS Log-Likelihood: -8.5201e+05
Date: Mon, 18 Jan 2021 Deviance: 2.7147e+11
Time: 23:05:53 Pearson chi2: 2.71e+11
No. Iterations: 3
Covariance Type: nonrobust
============================================================================================
coef std err z P>|z| [0.025 0.975]
--------------------------------------------------------------------------------------------
Intercept 456.2792 8.678 52.581 0.000 439.271 473.287
group[T.1] 14.9314 19.529 0.765 0.445 -23.344 53.207
period[T.pre] 21.7417 12.089 1.798 0.072 -1.953 45.437
group[T.1]:period[T.pre] -223.1779 27.134 -8.225 0.000 -276.360 -169.995
============================================================================================
由於您的匯總統計數據顯示您的分布嚴重偏斜,因此自舉實際上是一種更可靠的估計置信區間的方法:
r = 1000
bootstrap = np.zeros(r)
for i in range(0, r):
sample_index = np.random.choice(a=range(0, df.shape[0]), size=df.shape[0], replace=True)
df_sample = df.iloc[sample_index]
model = smf.glm('minutes ~ group * period', df_sample, family=sm.families.Gaussian()).fit()
bootstrap[i] = model.params.iloc[3] # interaction
bootstrap = pd.DataFrame(bootstrap, columns=['interaction'])
print(bootstrap.quantile([0.025, 0.975]).T)
Output:
0.025 0.975
interaction -273.524899 -175.373177
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.