Skip to content

Commit

Permalink
Update Examples in Docs (#724)
Browse files Browse the repository at this point in the history
* make sure all examples are run in docs

* adjust panelview plot size
  • Loading branch information
s3alfisc authored Nov 22, 2024
1 parent c08d447 commit 77e7080
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 690 deletions.
650 changes: 0 additions & 650 deletions docs/changelog.quarto_ipynb

This file was deleted.

5 changes: 3 additions & 2 deletions docs/difference-in-differences.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pf.panelview(
treat="treat",
collapse_to_cohort=True,
title = "Outcome Plot",
figsize=(1, 0.5),
figsize=(2, 0.75),
)
```

Expand All @@ -142,7 +142,8 @@ pf.panelview(
time="year",
treat="treat",
subsamp=100,
title = "Outcome Plot"
title = "Outcome Plot",
figsize=(2, 0.75),
)
```

Expand Down
5 changes: 3 additions & 2 deletions pyfixest/did/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ def panelview(
Examples
--------
```python
```{python}
import pandas as pd
import numpy as np
import pyfixest as pf
df_het = pd.read_csv("pd.read_csv("pyfixest/did/data/df_het.csv")
url = "https://raw.githubusercontent.com/py-econometrics/pyfixest/master/pyfixest/did/data/df_het.csv"
df_het = pd.read_csv(url)
# Inspect treatment assignment
pf.panelview(
Expand Down
33 changes: 33 additions & 0 deletions pyfixest/estimation/demean_.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,39 @@ def demean(
tuple[numpy.ndarray, bool]
A tuple containing the demeaned array of shape (n_samples, n_features)
and a boolean indicating whether the algorithm converged successfully.
Examples
--------
```{python}
import numpy as np
import pyfixest as pf
from pyfixest.utils.dgps import get_blw
from pyfixest.estimation.demean_ import demean
from formulaic import model_matrix
fml = "y ~ treat | state + year"
data = get_blw()
data.head()
Y, rhs = model_matrix(fml, data)
X = rhs[0].drop(columns="Intercept")
fe = rhs[1].drop(columns="Intercept")
YX = np.concatenate([Y, X], axis=1)
# to numpy
Y = Y.to_numpy()
X = X.to_numpy()
YX = np.concatenate([Y, X], axis=1)
fe = fe.to_numpy().astype(int) # demean requires fixed effects as ints!
YX_demeaned, success = demean(YX, fe, weights = np.ones(YX.shape[0]))
Y_demeaned = YX_demeaned[:, 0]
X_demeaned = YX_demeaned[:, 1:]
print(np.linalg.lstsq(X_demeaned, Y_demeaned, rcond=None)[0])
print(pf.feols(fml, data).coef())
```
"""
n_samples, n_features = x.shape
n_factors = flist.shape[1]
Expand Down
4 changes: 1 addition & 3 deletions pyfixest/estimation/feiv_.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ def IV_Diag(self, statistics: Optional[list[str]] = None):
print("(Unadjusted) F stat :", F_stat_pf)
print("Effective F stat :", F_stat_eff_pf)
# The example above generates the following results
# (Unadjusted) F stat : 52.81535560457482
# Effective F stat : 48.661542741328205
```
"""
# Set default statistics
iv_diag_stat = ["f_stat", "effective_f"]
Expand Down
32 changes: 13 additions & 19 deletions pyfixest/estimation/feols_.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,31 +984,25 @@ def wald_test(self, R=None, q=None, distribution="F"):
Examples
--------
```{python}
import numpy as np
import pandas as pd
import pyfixest as pf
from pyfixest.estimation.estimation import feols
data = pd.read_csv("pyfixest/did/data/df_het.csv")
data = data.iloc[1:3000]
data = pf.get_data()
fit = pf.feols("Y ~ X1 + X2| f1", data, vcov={"CRV1": "f1"}, ssc=pf.ssc(adj=False))
R = np.array([[1,-1]] )
q = np.array([0.0])
fml = "dep_var ~ treat"
fit = feols(fml, data, vcov={"CRV1": "year"}, ssc=ssc(adj=False))
# Wald test
fit.wald_test(R=R, q=q, distribution = "chi2")
f_stat = fit._f_statistic
p_stat = fit._p_value
print(f"Python f_stat: {f_stat}")
print(f"Python p_stat: {p_stat}")
# The code above produces the following results :
# Python f_stat: 256.55432910297003
# Python p_stat: 9.67406627744023e-58
```
"""
_vcov = self._vcov
_N = self._N
Expand Down Expand Up @@ -1332,15 +1326,15 @@ def ccv(
Examples
--------
```python
from pyfixest.estimation import feols
from pyfixest.utils import get_data
```{python}
import pyfixest as pf
import numpy as np
data = get_data()
data["D1"] = np.random.choice([0, 1], size=data.shape[0])
data = pf.get_data()
data["D"] = np.random.choice([0, 1], size=data.shape[0])
fit = feols("Y ~ D", data=data, vcov={"CRV1": "group_id"})
fit.ccv(treatment="D", pk=0.05, gk=0.5, n_splits=8, seed=123).head()
fit = pf.feols("Y ~ D", data=data, vcov={"CRV1": "group_id"})
fit.ccv(treatment="D", pk=0.05, qk=0.5, n_splits=8, seed=123).head()
```
"""
assert (
Expand Down Expand Up @@ -1873,7 +1867,7 @@ def confint(
Examples
--------
```python
```{python}
from pyfixest.utils import get_data
from pyfixest.estimation import feols
Expand Down
14 changes: 14 additions & 0 deletions pyfixest/estimation/model_matrix_fixest_.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ def model_matrix_fixest(
List of variables interacted with i() syntax, None if not applicable.
- 'X_is_empty' : bool
Flag indicating whether X is empty.
Examples
--------
```{python}
import pyfixest as pf
from pyfixest.estimation.model_matrix_fixest_ import model_matrix_fixest
data = pf.get_data()
fit = pf.feols("Y ~ X1 + f1 + f2", data=data)
FixestFormula = fit.FixestFormula
mm = model_matrix_fixest(FixestFormula, data)
mm
```
"""
FixestFormula.check_syntax()

Expand Down
30 changes: 16 additions & 14 deletions pyfixest/estimation/multcomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ def bonferroni(models: ModelInputType, param: str) -> pd.DataFrame:
Examples
--------
```python
from pyfixest.estimation import feols
```{python}
import pyfixest as pf
from pyfixest.utils import get_data
from pyfixest.multcomp import bonferroni
data = get_data().dropna()
fit1 = feols("Y ~ X1", data=data)
fit2 = feols("Y ~ X1 + X2", data=data)
bonf_df = bonferroni([fit1, fit2], param="X1")
fit1 = pf.feols("Y ~ X1", data=data)
fit2 = pf.feols("Y ~ X1 + X2", data=data)
bonf_df = pf.bonferroni([fit1, fit2], param="X1")
bonf_df
```
"""
Expand Down Expand Up @@ -106,18 +105,21 @@ def rwolf(
Examples
--------
```python
from pyfixest.estimation import feols
```{python}
import pyfixest as pf
from pyfixest.utils import get_data
from pyfixest.multcomp import rwolf
data = get_data().dropna()
fit = feols("Y ~ Y2 + X1 + X2", data=data)
rwolf(fit.to_list(), "X1", reps=9999, seed=123)
fit = pf.feols("Y ~ Y2 + X1 + X2", data=data)
pf.rwolf(fit, "X1", reps=9999, seed=123)
fit1 = pf.feols("Y ~ X1", data=data)
fit2 = pf.feols("Y ~ X1 + X2", data=data)
rwolf_df = pf.rwolf([fit1, fit2], "X1", reps=9999, seed=123)
# use randomization inference
rwolf_df = pf.rwolf([fit1, fit2], "X1", reps=9999, seed=123, sampling_method = "ri")
fit1 = feols("Y ~ X1", data=data)
fit2 = feols("Y ~ X1 + X2", data=data)
rwolf_df = rwolf([fit1, fit2], "X1", reps=9999, seed=123)
rwolf_df
```
"""
Expand Down
27 changes: 27 additions & 0 deletions pyfixest/report/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ def etable(
pandas.DataFrame
A styled DataFrame with the coefficients and standard errors of the models.
When output is "tex", the LaTeX code is returned as a string.
Examples
--------
For more examples, take a look at the [regression tables and summary statistics vignette](https://py-econometrics.github.io/pyfixest/table-layout.html).
```{python}
import pyfixest as pf
# load data
df = pf.get_data()
fit1 = pf.feols("Y~X1 + X2 | f1", df)
fit2 = pf.feols("Y~X1 + X2 | f1 + f2", df)
pf.etable([fit1, fit2])
```
"""
if signif_code is None:
signif_code = [0.001, 0.01, 0.05]
Expand Down Expand Up @@ -1189,6 +1204,18 @@ def dtable(
Returns
-------
A table in the specified format.
Examples
--------
For more examples, take a look at the [regression tables and summary statistics vignette](https://py-econometrics.github.io/pyfixest/table-layout.html).
```{python}
import pyfixest as pf
# load data
df = pf.get_data()
pf.dtable(df, vars = ["Y", "X1", "X2", "f1"])
```
"""
if stats is None:
stats = ["count", "mean", "std"]
Expand Down

0 comments on commit 77e7080

Please sign in to comment.