Home > Software engineering >  Python Difference-in-Difference Regression Coefficient plot with 95 interval
Python Difference-in-Difference Regression Coefficient plot with 95 interval

Time:11-25

I want to draw a difference-in-differences coefficient graph with 95ci.

Here is my data frame.

example:
x          coef        stderr     ci_lower    ci_upper
t-3        .0005219    .0004025    -.000759    .0018028
t-2        .0008449    .0004205    -.0004935   .0021833
t-1         0             0             0         0
t          .0009092    .0003047    -.0000604   .0018789
t 1        .0010828    .0002889    .0001634    .0020023
t 2        .0013157    .0001613    .0008022    .0018291
t 3        .001452     .0001755    .0008933    .0020106

Here is the code that I used. But it didn't work and not that informative

plt.errorbar(x=x, y=coef, yerr=ci_lower(?), color="black", capsize=3,
             linestyle="None",
             marker="s", markersize=7, mfc="black", mec="black")

Is there any way to have a regression coef graph like that?

Thanks in advance

CodePudding user response:

You should provide the data and also an example of the plot you like. Your data would be:

df = {'x': {0: 't-3', 1: 't-2', 2: 't-1', 3: 't', 4: 't 1', 5: 't 2', 6: 't 3'},
 'coef': {0: 0.0005219,
  1: 0.0008449,
  2: 0.0,
  3: 0.0009092,
  4: 0.0010828,
  5: 0.0013157,
  6: 0.001452},
 'stderr': {0: 0.0004025,
  1: 0.0004205,
  2: 0.0,
  3: 0.0003047,
  4: 0.0002889,
  5: 0.0001613,
  6: 0.0001755},
 'ci_lower': {0: -0.000759,
  1: -0.0004935,
  2: 0.0,
  3: -6.04e-05,
  4: 0.0001634,
  5: 0.0008022,
  6: 0.0008933},
 'ci_upper': {0: 0.0018028,
  1: 0.0021833,
  2: 0.0,
  3: 0.0018789,
  4: 0.0020023,
  5: 0.0018291,
  6: 0.0020106}}

df = pd.DataFrame(df)

I am guessing it is like:

fig, ax = plt.subplots()
df.plot.scatter(x = "coef", y = "x",ax=ax)
for ix in df.index:
    ax.plot(df[['ci_lower','ci_upper']].loc[ix].values, df.loc[ix,['x','x']].values,c="b")

enter image description here

  • Related