Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Panelview Function #736

Merged
merged 9 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/difference-in-differences.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pf.panelview(
ylab="Cohort",
xlab="Year",
title="Treatment Assignment Cohorts",
figsize=(0.5, 0.5),
figsize=(6, 5),
)
```

Expand All @@ -78,7 +78,7 @@ pf.panelview(
ylab="Cohort",
xlab="Year",
title="Treatment Assignment Cohorts",
figsize=(0.5, 0.5),
figsize=(6, 5),
)
```

Expand All @@ -98,7 +98,7 @@ pf.panelview(
ylab="Unit",
xlab="Year",
title="Treatment Assignment (all units)",
figsize=(0.5, 0.5),
figsize=(6, 5),
)
```

Expand All @@ -118,8 +118,9 @@ pf.panelview(
time="year",
treat="treat",
collapse_to_cohort=True,
title = "Outcome Plot",
figsize=(2, 0.75),
title="Outcome Plot",
legend=True,
figsize=(7, 2.5),
)
```

Expand All @@ -143,7 +144,8 @@ pf.panelview(
treat="treat",
subsamp=100,
title = "Outcome Plot",
figsize=(2, 0.75),
legend=True,
figsize=(7, 2.5),
)
```

Expand Down
14 changes: 11 additions & 3 deletions pyfixest/did/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def panelview(
time=time,
treat=treat,
outcome=outcome,
collapse_to_cohort=collapse_to_cohort,
ax=ax,
xlab=xlab,
ylab=ylab,
Expand All @@ -161,6 +162,7 @@ def panelview(
ax=ax,
xlab=xlab,
ylab=ylab,
figsize=figsize,
legend=legend,
noticks=noticks,
title=title,
Expand Down Expand Up @@ -211,6 +213,7 @@ def get_treatment_start(x: pd.DataFrame) -> pd.Timestamp:
)

data_agg = data_agg.rename(columns={"treatment_start": unit})
data_agg[unit] = data_agg[unit].fillna("no_treatment")
data = data_agg.copy()
data_pivot = data_agg.pivot(index=unit, columns=time, values=outcome)

Expand All @@ -224,6 +227,7 @@ def _plot_panelview_output_plot(
time: str,
treat: str,
outcome: str,
collapse_to_cohort: Optional[bool] = None,
ax: Optional[plt.Axes] = None,
xlab: Optional[str] = None,
ylab: Optional[str] = None,
Expand All @@ -234,10 +238,13 @@ def _plot_panelview_output_plot(
figsize: Optional[tuple] = (11, 3),
) -> plt.Axes:
if not ax:
f, ax = plt.subplots(figsize=figsize, dpi=300)
f, ax = plt.subplots(figsize=figsize)
for unit_id in data_pivot.index:
unit_data = data_pivot.loc[unit_id]
treatment_times = data[(data[unit] == unit_id) & (data[treat])][time]
if collapse_to_cohort:
treatment_times = data[(data[time] == unit_id) & (data[treat])][time]
else:
treatment_times = data[(data[unit] == unit_id) & (data[treat])][time]

# If the unit never receives treatment, plot the line in grey
if treatment_times.empty:
Expand Down Expand Up @@ -323,12 +330,13 @@ def _plot_panelview(
ax: Optional[plt.Axes] = None,
xlab: Optional[str] = None,
ylab: Optional[str] = None,
figsize: Optional[tuple] = (11, 3),
legend: Optional[bool] = False,
noticks: Optional[bool] = False,
title: Optional[str] = None,
) -> plt.Axes:
if not ax:
f, ax = plt.subplots()
f, ax = plt.subplots(figsize=figsize)
cax = ax.matshow(treatment_quilt, cmap="viridis", aspect="auto")
f.colorbar(cax) if legend else None
ax.set_xlabel(xlab) if xlab else None
Expand Down
Loading