帮我用可视化的方式介绍合成控制分析
- User
帮我用可视化的方式介绍合成控制分析
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
帮我用可视化的方式介绍合成控制分析
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
合成控制法是一种强大的政策评估方法,用于估计某个地区或群体在特定干预(如政策实施)后的效果。下面我将通过可视化方式详细介绍这种方法的核心概念和应用。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import warnings
warnings.filterwarnings('ignore')
# 设置绘图风格
plt.style.use('seaborn-whitegrid')
sns.set_palette("Set2")
# 创建可视化
fig, axs = plt.subplots(2, 2, figsize=(16, 12), gridspec_kw={'height_ratios': [2, 1]})
plt.suptitle('合成控制法 (Synthetic Control Method) 可视化解析', fontsize=18, fontweight='bold')
# 面板1:概念示意图
ax = axs[0, 0]
ax.set_title('1. 核心概念示意图', fontsize=14)
# 绘制处理组
ax.text(0.1, 0.8, "处理组", fontsize=12, ha='center')
ax.plot([0.2, 0.8], [0.8, 0.8], 'r-', lw=3)
ax.plot([0.8, 0.8], [0.8, 0.6], 'r--', lw=2)
ax.plot([0.8, 1.0], [0.6, 0.4], 'r-', lw=3)
# 绘制对照组
for i in range(5):
y_pos = 0.6 - i*0.1
ax.text(0.1, y_pos, f"对照组 {i+1}", fontsize=10, ha='center')
ax.plot([0.2, 0.8], [y_pos, y_pos], 'b-', lw=1)
ax.plot([0.8, 1.0], [y_pos, y_pos-0.1], 'b-', lw=1)
# 绘制合成控制组
ax.text(0.1, 0.3, "合成控制组", fontsize=12, ha='center')
ax.plot([0.2, 0.8], [0.3, 0.3], 'g--', lw=2)
ax.plot([0.8, 1.0], [0.3, 0.2], 'g-', lw=3)
# 添加标记
ax.axvline(x=0.8, color='k', linestyle='--', alpha=0.5)
ax.text(0.8, 0.05, "政策实施时间", fontsize=10, ha='center')
ax.text(0.5, 0.05, "政策前", fontsize=10, ha='center')
ax.text(0.9, 0.05, "政策后", fontsize=10, ha='center')
ax.text(0.9, 0.75, "实际结果", fontsize=10, color='red')
ax.text(0.9, 0.25, "反事实结果", fontsize=10, color='green')
ax.text(0.9, 0.55, "政策效应", fontsize=10, color='purple')
ax.annotate('', xy=(0.9, 0.6), xytext=(0.9, 0.4),
arrowprops=dict(arrowstyle='<->', color='purple', lw=2))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
# 面板2:权重分配
ax = axs[0, 1]
ax.set_title('2. 合成控制组权重分配', fontsize=14)
# 创建示例权重
regions = ['区域A', '区域B', '区域C', '区域D', '区域E']
weights = [0.35, 0.25, 0.15, 0.15, 0.10]
# 绘制权重图
bars = ax.bar(regions, weights, color=sns.color_palette("Set2"))
ax.axhline(y=0, color='k')
ax.set_ylabel('权重')
ax.set_ylim(0, 0.4)
# 添加权重值
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 0.01,
f'{height:.2f}', ha='center', va='bottom')
# 添加说明
ax.text(0.5, -0.2, "合成控制组 = 0.35×A + 0.25×B + 0.15×C + 0.15×D + 0.10×E",
fontsize=12, ha='center', transform=ax.transAxes)
# 面板3:时间序列数据
ax = axs[1, 0]
ax.set_title('3. 政策效果评估', fontsize=14)
# 生成模拟数据
np.random.seed(42)
years = np.arange(1990, 2011)
pre_years = years[years <= 2000]
post_years = years[years >= 2000]
# 处理组数据
treated_pre = 50 + 2*(pre_years - 1990) + np.random.normal(0, 1, len(pre_years))
treated_post = 70 + 1*(post_years - 2000) + np.random.normal(0, 1.5, len(post_years))
treated = np.concatenate([treated_pre, treated_post[1:]])
# 合成控制组数据
synthetic_pre = 48 + 1.9*(pre_years - 1990) + np.random.normal(0, 0.8, len(pre_years))
synthetic_post = 68 + 1.9*(post_years - 2000) + np.random.normal(0, 1, len(post_years))
synthetic = np.concatenate([synthetic_pre, synthetic_post[1:]])
# 绘制时间序列
ax.plot(years, treated, 'ro-', label='处理组(实际)', lw=2, markersize=6)
ax.plot(years, synthetic, 'g--', label='合成控制组(反事实)', lw=2)
ax.axvline(x=2000, color='k', linestyle='--', alpha=0.7)
ax.text(2000.2, 40, '政策实施\n(2000年)', fontsize=10)
# 填充政策效果区域
ax.fill_between(post_years, treated[10:], synthetic[10:], color='purple', alpha=0.2)
ax.text(2005, 65, '估计政策效果', fontsize=12, color='purple')
# 添加图例和标签
ax.legend(loc='upper left')
ax.set_xlabel('年份')
ax.set_ylabel('结果变量')
ax.set_ylim(40, 80)
# 面板4:安慰剂检验
ax = axs[1, 1]
ax.set_title('4. 安慰剂检验', fontsize=14)
# 绘制处理组效应
effect = treated[10:] - synthetic[10:]
ax.plot(post_years, effect, 'r-', lw=2, label='处理组效应')
# 绘制安慰剂效应
for i in range(5):
placebo_effect = np.random.normal(0, 0.8, len(post_years)) * 3
ax.plot(post_years, placebo_effect, 'gray', alpha=0.6)
# 添加参考线
ax.axhline(y=0, color='k', linestyle='-', alpha=0.5)
ax.axvline(x=2000, color='k', linestyle='--', alpha=0.7)
# 添加标签和图例
ax.set_xlabel('年份')
ax.set_ylabel('效应大小')
ax.legend(loc='upper left')
ax.text(2005, max(effect)+1, '处理组效应显著\n高于安慰剂效应', fontsize=10, color='red')
plt.tight_layout()
plt.subplots_adjust(top=0.92)
plt.show()
优势:
局限:
通过这种可视化方式,我们可以清晰理解合成控制法的核心思想、实施步骤和结果解释,为政策评估提供直观的分析工具。