Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to calculate the double difference Model DID and its corresponding P value

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "how to use Python to calculate the double difference model DID and its corresponding P value". In the actual case operation process, many people will encounter such a dilemma. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

directory

1. DID (Differences in Differences)

2. DID model form

3. OLS polynomial fit

1. DID (Differences in Differences)

Double difference method, which is mainly used to evaluate the effect of policies in sociology. This method requires two "differences" of data. One is the difference before and after the intervention, and this is the difference before and after the experiment itself. Another is the "difference" between the intervention group and the control group. DID uses the difference between these two "differences" to estimate the effect of the intervention. Therefore, as the name implies, it is called double difference method.

The principle is based on a counterfactual framework to assess the change in the observed factor y when the policy occurs and when it does not. If an exogenous policy shock divides the sample into two groups: the Treat group with policy intervention and the Control group without policy intervention (y is not significantly different between the Treat group and the Control group before the policy shock). Then, the change in y of the Control group before and after the policy occurs can be regarded as the state of the Treat group when it is not affected by the policy (the counterfactual result). By comparing the change in the Treat group y (D1) with the change in the Control group y (D2), the actual effect of the policy shock (DD=D1-D2) can be obtained.

Note that the resulting double-difference estimator is unbiased only if y is not significantly different between the Treat group and the Control group before the policy shock (i.e., the parallelism assumption).

As shown below:

The intervention group was A1 before experiment and A2 after experiment. The control group was B1 before experiment and B2 after experiment. For the intervention group, the difference before and after the experiment was A2-A1, and for the control group, the difference after the experiment was B2-B1. The difference (A2-A1)-(B2-B1) is the DID result, causal effect/treatment effect. The parts represented by the effects are processed as shown below.

2. DID model form

is a grouping dummy variable (treatment group =1, control group =0);

is a staged dummy variable (after policy implementation =1, before policy implementation =0);

The interaction term represents the effect of treatment group after the policy is implemented, and its coefficient is the treatment effect that the double difference model focuses on.

3. OLS polynomial fit

According to the DID formula, we can find DID and its P value by using polynomial fitting method. Pyhton's method is as follows: using the ols method from the statsmodels library, you need to prepare the data according to the above formula, t stands for time (before intervention =0, after intervention =1), g stands for grouping (intervention group =1, control group =0), and there is a crossover term tg (t*g is calculated).

The code is as follows:

import statsmodels.formula.api as smfimport pandas as pdv1 =[0.367730,0.377147,0.352539,0.341864,0.29276,0.393443,0.374697,0.346989,0.385783,0.307801]t1 = [0,0,0,0,1,0,0,0,0,1]g1 =[1,1,1,1,1,0,0,0,0,0]tg1 = [0,0,0,0,1,0,0,0,0,0]aa = pd.DataFrame({'t1':t1,'g1':g1,'tg1':tg1,'v1':v1})X = aa[['t1', 'g1','tg1']]y = aa['v1']est = smf.ols(formula='v1 ~ t1 + g1 + tg1', data=aa).fit() y_pred = est.predict(X)aa['v1_pred'] = y_predprint(aa)print(est.summary()) print(est.params)

Prepare data in the following format:

OLS results are summarized as follows:

The coefficient of the cross term is the DID result, dealing with effects. P>| t |A P value less than 0.05 indicates a significant difference.

"How to use Python to calculate the double difference model DID and its corresponding P value" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report