<jupyter_start><jupyter_text>Qiita ~ A/Bテストは無理だと言われたあなたへ ~<jupyter_code>import numpy as np
import pandas as pd

from pandas import DataFrame
from plotly.graph_objs import Bar, Figure, Layout
from plotly.offline import iplot, plot

from sklearn.metrics import roc_auc_score
import statsmodels.api as sm<jupyter_output><empty_output><jupyter_text>CM Data<jupyter_code># データの読み込み
df = pd.read_csv('https://github.com/iwanami-datascience/vol3/raw/master/kato%26hoshino/q_data_x.csv')
df.head()
# CM視聴有無でのアプリ利用率の差を計算
diff1 = np.round(np.mean(df[df.cm_dummy == 1].gamedummy.values) - np.mean(df[df.cm_dummy == 0].gamedummy.values), 4)
diff1
# CM視聴有無でのアプリ利用回数の差を計算
diff2 = np.round(np.mean(df[df.cm_dummy == 1].gamecount.values) - np.mean(df[df.cm_dummy == 0].gamecount.values), 2)
diff2 
# CM視聴有無でのアプリ利用時間の差を計算
diff3 = np.round(np.mean(df[df.cm_dummy == 1].gamesecond.values) - np.mean(df[df.cm_dummy == 0].gamesecond.values), 1)
diff3
# 説明変数
cols = ["age", "sex", "TVwatch_day", "marry_dummy", "child_dummy", "inc", "pmoney",
        "area_kanto", "area_tokai", "area_keihanshin",
        "job_dummy1", "job_dummy2", "job_dummy3", "job_dummy4", "job_dummy5", "job_dummy6",
        "fam_str_dummy1", "fam_str_dummy2", "fam_str_dummy3", "fam_str_dummy4"]
df["intercept"] = 1
X = df[cols + ["intercept"]].values

# CM視聴有無ダミー
w = df.cm_dummy.values

# 目的変数群（1:アプリ利用ダミー, 2:アプリ利用回数、3:アプリ利用時間）
y1, y2, y3 = df.gamedummy.values, df.gamecount.values, df.gamesecond.values
# LogisticRegressionにより傾向スコアを推定
glm = sm.Logit(w, X)
result = glm.fit()
ps = result.predict(X)
# c統計量としてAUCをを計算
roc_auc_score(w, ps)
# IPWによりアプリ利用ダミーへのATEを推定
ipwe11 = np.sum((w * y1) / ps) / np.sum(w / ps)  # Treated
ipwe10 = np.sum(((1 - w) * y1) / (1.0 - ps)) / np.sum((1 - w) / (1.0 - ps))  # Control
ate1 = np.round(ipwe11 - ipwe10, 4)
ate1
# IPWによりアプリ利用回数へのATEを推定
ipwe21 = np.sum((w * y2) / ps) / np.sum(w / ps)  # Treated
ipwe20 = np.sum(((1 - w) * y2) / (1 - ps)) / np.sum((1 - w) / (1 - ps))  # Control
ate2 = np.round(ipwe21 - ipwe20, 2)
ate2
# IPWによりアプリ利用時間へのATEを推定
ipwe31 = np.sum((w * y3) / ps) / np.sum(w / ps)
ipwe30 = np.sum(((1 - w) * y3) / (1.0 - ps)) / np.sum((1 - w) / (1.0 - ps))
ate3 = np.round(ipwe31 - ipwe30, 1)
ate3<jupyter_output><empty_output><jupyter_text>結果の描画<jupyter_code>bar11 = Bar(x=["アプリ利用有無"], y=[diff1], text=[diff1], textposition="auto", name="補正前")
bar12 = Bar(x=["アプリ利用有無"], y=[ate1], text=[ate1], textposition="auto", name="補正後")
fig1 = Figure(data=[bar11, bar12], layout=Layout(barmode="group"))
iplot(fig1)
bar21 = Bar(x=["アプリ利用回数"], y=[diff2], text=[diff2], textposition="auto", name="補正前")
bar22 = Bar(x=["アプリ利用回数"], y=[ate2], text=[np.round(ate2, 2)], textposition="auto", name="補正後")
fig2 = Figure(data=[bar21, bar22], layout=Layout(barmode="group"))
iplot(fig2)
bar31 = Bar(x=["アプリ利用時間"], y=[diff3], text=[diff3], textposition="auto", name="補正前")
bar32 = Bar(x=["アプリ利用時間"], y=[ate3], text=[ate3], textposition="auto", name="補正後")
fig3 = Figure(data=[bar31, bar32], layout=Layout(barmode="group"))
iplot(fig3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19612352168199737, 'fraction_numerical': 0.04369250985545335, 'mean_word_length': 5.437632135306554, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '282970', 'license': 'MIT', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1491, 'n_words': 232, 'path': 'causal_inference/causal_inference_observational_study.ipynb', 'repo_name': 'smn-ailab/ysaito-qiita', 'size': '3044'}