我正在尝试执行差异中的差异 https://en.wikipedia.org/wiki/Difference_in_differences(使用面板数据和固定效应)使用 Python 和 Pandas 进行分析。我没有经济学背景,我只是想过滤数据并运行我被告知的方法。然而,据我所知,我了解到基本的 diff-in-diffs 模型如下所示:
也就是说,我正在处理一个多变量模型。
下面是 R 中的一个简单示例:
https://thetarzan.wordpress.com/2011/06/20/differences-in-differences-estimation-in-r-and-stata/ https://thetarzan.wordpress.com/2011/06/20/differences-in-differences-estimation-in-r-and-stata/
可以看出,回归将一个因变量和树状观察集作为输入。
我的输入数据如下所示:
Name Permits_13 Score_13 Permits_14 Score_14 Permits_15 Score_15
0 P.S. 015 ROBERTO CLEMENTE 12.0 284 22 279 32 283
1 P.S. 019 ASHER LEVY 18.0 296 51 301 55 308
2 P.S. 020 ANNA SILVER 9.0 294 9 290 10 293
3 P.S. 034 FRANKLIN D. ROOSEVELT 3.0 294 4 292 1 296
4 P.S. 064 ROBERT SIMON 3.0 287 15 288 17 291
5 P.S. 110 FLORENCE NIGHTINGALE 0.0 313 3 306 4 308
6 P.S. 134 HENRIETTA SZOLD 4.0 290 12 292 17 288
7 P.S. 137 JOHN L. BERNSTEIN 4.0 276 12 273 17 274
8 P.S. 140 NATHAN STRAUS 13.0 282 37 284 59 284
9 P.S. 142 AMALIA CASTRO 7.0 290 15 285 25 284
10 P.S. 184M SHUANG WEN 5.0 327 12 327 9 327
通过一些研究,我发现这是在 Pandas 中使用固定效应和面板数据的方法:
Pandas 或 Statsmodels 中的固定效果 https://stackoverflow.com/questions/24195432/fixed-effect-in-pandas-or-statsmodels
我执行了一些转换来获取多索引数据:
rng = pandas.date_range(start=pandas.datetime(2013, 1, 1), periods=3, freq='A')
index = pandas.MultiIndex.from_product([rng, df['Name']], names=['date', 'id'])
d1 = numpy.array(df.ix[:, ['Permits_13', 'Score_13']])
d2 = numpy.array(df.ix[:, ['Permits_14', 'Score_14']])
d3 = numpy.array(df.ix[:, ['Permits_15', 'Score_15']])
data = numpy.concatenate((d1, d2, d3), axis=0)
s = pandas.DataFrame(data, index=index)
s = s.astype('float')
但是,我不知道如何将所有这些变量传递给模型,例如可以在 R 中完成:
reg1 = lm(work ~ post93 + anykids + p93kids.interaction, data = etc)
这里,13、14、15代表2013年、2014年、2015年的数据,我认为应该用来创建面板。
我这样称呼该模型:
reg = PanelOLS(y=s['y'],x=s[['x']],time_effects=True)
这是结果:
(一位经济学家)告诉我,这似乎并不具有固定效应。
--EDIT--
我想验证的是在给定时间的情况下许可数量对分数的影响。许可证的数量就是治疗,它是一个强化治疗.
代码示例可以在这里找到:https://www.dropbox.com/sh/ped312ur604357r/AACQGloHDAy8I2C6HITFzjqza?dl=0 https://www.dropbox.com/sh/ped312ur604357r/AACQGloHDAy8I2C6HITFzjqza?dl=0.