【python因果推断库15】使用 sci-kit learn 模型进行回归断点分析

目录

导入数据

线性模型和主效应模型

线性模型、主效应模型和交互作用模型

使用bandwidth


python 复制代码
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ExpSineSquared, WhiteKernel
from sklearn.linear_model import LinearRegression

import causalpy as cp
%config InlineBackend.figure_format = 'retina'

导入数据

python 复制代码
data = cp.load_data("rd")
data.head()

线性模型和主效应模型

python 复制代码
result = cp.skl_experiments.RegressionDiscontinuity(
    data,
    formula="y ~ 1 + x + treated",
    model=LinearRegression(),
    treatment_threshold=0.5,
)
fig, ax = result.plot()
python 复制代码
result.summary(round_to=3)
复制代码
Difference in Differences experiment
Formula: y ~ 1 + x + treated
Running variable: x
Threshold on running variable: 0.5

Results:
Discontinuity at threshold = 0.19
Model coefficients:
  Intercept      	         0
  treated[T.True]	      0.19
  x              	      1.23

线性模型、主效应模型和交互作用模型

python 复制代码
result = cp.skl_experiments.RegressionDiscontinuity(
    data,
    formula="y ~ 1 + x + treated + x:treated",
    model=LinearRegression(),
    treatment_threshold=0.5,
)
result.plot();

虽然我们可以看到这样做并不能很好地拟合数据,几乎肯定高估了阈值处的不连续性。

python 复制代码
result.summary(round_to=3)
复制代码
Difference in Differences experiment
Formula: y ~ 1 + x + treated + x:treated
Running variable: x
Threshold on running variable: 0.5

Results:
Discontinuity at threshold = 0.92
Model coefficients:
  Intercept        	         0
  treated[T.True]  	      2.47
  x                	      1.32
  x:treated[T.True]	     -3.11

使用bandwidth

我们处理这个问题的一种方法是使用 `bandwidth` 参数。这将只对阈值附近的一定带宽内的数据进行拟合。如果 x 是连续变量,那么模型将只对满足 的数据进行拟合。

python 复制代码
result = cp.skl_experiments.RegressionDiscontinuity(
    data,
    formula="y ~ 1 + x + treated + x:treated",
    model=LinearRegression(),
    treatment_threshold=0.5,
    bandwidth=0.3,
)

result.plot();

我们甚至可以走得更远,只为接近阈值的数据拟合截距。但很明显,这将涉及更多的估计误差,因为我们使用的数据较少。

python 复制代码
result = cp.skl_experiments.RegressionDiscontinuity(
    data,
    formula="y ~ 1 + treated",
    model=LinearRegression(),
    treatment_threshold=0.5,
    bandwidth=0.3,
)

result.plot();
相关推荐
chen_song_几秒前
深度神经网络训练参数优化概论
人工智能·深度学习·神经网络·机器学习·dnn
大模型真好玩1 分钟前
轻松搞定年度报告可视化,五分钟用 AntV + Trae Solo 快速构建智能图表生成器!
前端·人工智能·trae
非著名架构师1 分钟前
智慧城市的“气候大脑“:气象大数据如何优化城市级能源调度与供需平衡
大数据·人工智能·数据分析·智慧城市·风光功率预测·高精度天气预报数据
doris6102 分钟前
制造企业如何实现设备全生命周期管理?
大数据·网络·人工智能
却道天凉_好个秋2 分钟前
OpenCV(四十):Shi-Tomasi角点检测
人工智能·opencv·计算机视觉
jllllyuz3 分钟前
基于极限学习机(ELM)的数据分类:原理、实现与优化
人工智能·分类·数据挖掘
专注VB编程开发20年5 分钟前
Activex dll创建调用-Python,Node.js, JAVA主流编程语言操作COM对象
java·开发语言·python·node.js·activex dll
moonsheeper5 分钟前
Prompt优化策略
人工智能·机器学习
海岸线科技5 分钟前
离散制造,工单级成本管控的必然
大数据·人工智能·制造
亚控科技6 分钟前
亚控信创SCADA以全栈国产化方案,筑牢航空燃油安全供应生命线
运维·人工智能·安全·kingscada·亚控科技