analyze_robust_inference

source

analyze_robust_inference(
    result_or_model,
    param,
    *,
    method='ritest',
    reps=1000,
    cluster=None,
    seed=0,
)

Run robust inference on one coefficient via randomization inference or wild bootstrap.

Parameters

Name Type Description Default
result_or_model Any A fitted model or a result object carrying .models. required
param str The coefficient name to test (H0: param = 0). required
method Literal['ritest', 'wildboot'] "ritest" (randomization inference, native to pyfixest) or "wildboot" (wild cluster bootstrap, which requires the optional wildboottest package). 'ritest'
reps int Number of resamples / bootstrap replications. 1000
cluster str | None Optional cluster variable for the resampling. None
seed int Seed for reproducibility. 0

Returns

Name Type Description
RobustInferenceResult method, param, estimate, p_value, conf_int, reps and the underlying raw pyfixest output.

Examples

Basic — fit a regression (the data dictionary supplies the readable labels) and test the slope on trade openness via randomization inference:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
model = ex.analyze_regression_table(
    df,
    dvs="gini_regional",
    idvs=["log_gdp_pc", "trade_share"],
)
result = ex.analyze_robust_inference(model, "trade_share", reps=200, seed=0)
result.p_value

  0%|          | 0/200 [00:00<?, ?it/s]
  6%|▌         | 12/200 [00:00<00:01, 118.81it/s]
 12%|█▎        | 25/200 [00:00<00:01, 120.07it/s]
 19%|█▉        | 38/200 [00:00<00:01, 120.09it/s]
 26%|██▌       | 51/200 [00:00<00:01, 121.32it/s]
 32%|███▏      | 64/200 [00:00<00:01, 120.67it/s]
 38%|███▊      | 77/200 [00:00<00:01, 120.73it/s]
 45%|████▌     | 90/200 [00:00<00:00, 118.92it/s]
 51%|█████     | 102/200 [00:00<00:00, 118.52it/s]
 57%|█████▋    | 114/200 [00:00<00:00, 118.43it/s]
 63%|██████▎   | 126/200 [00:01<00:00, 118.84it/s]
 69%|██████▉   | 138/200 [00:01<00:00, 117.98it/s]
 75%|███████▌  | 150/200 [00:01<00:00, 118.14it/s]
 81%|████████  | 162/200 [00:01<00:00, 118.31it/s]
 88%|████████▊ | 175/200 [00:01<00:00, 118.99it/s]
 94%|█████████▎| 187/200 [00:01<00:00, 118.50it/s]
100%|██████████| 200/200 [00:01<00:00, 118.98it/s]
100%|██████████| 200/200 [00:01<00:00, 119.06it/s]
0.0

Advanced — cluster the randomization inference by country (resampling permutes treatment within clusters; the cluster column must be numeric, so encode the country labels as integer codes first), then read the estimate and the raw pyfixest output:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(load_kuznets(), load_kuznets_data_def(), set_panel=True)
df["country_id"] = df["country"].factorize()[0]
model = ex.analyze_regression_table(
    df,
    dvs="gini_regional",
    idvs=["log_gdp_pc", "trade_share"],
    clusters=["country_id"],
)
result = ex.analyze_robust_inference(
    model, "trade_share", cluster="country_id", reps=200, seed=0
)
result.estimate
result.raw

  0%|          | 0/200 [00:00<?, ?it/s]
  7%|▋         | 14/200 [00:00<00:01, 134.75it/s]
 14%|█▍        | 28/200 [00:00<00:01, 134.43it/s]
 21%|██        | 42/200 [00:00<00:01, 133.69it/s]
 28%|██▊       | 56/200 [00:00<00:01, 134.20it/s]
 35%|███▌      | 70/200 [00:00<00:00, 134.46it/s]
 42%|████▏     | 84/200 [00:00<00:00, 135.32it/s]
 49%|████▉     | 98/200 [00:00<00:00, 135.62it/s]
 56%|█████▌    | 112/200 [00:00<00:00, 135.67it/s]
 63%|██████▎   | 126/200 [00:00<00:00, 135.91it/s]
 70%|███████   | 140/200 [00:01<00:00, 136.20it/s]
 77%|███████▋  | 154/200 [00:01<00:00, 135.67it/s]
 84%|████████▍ | 168/200 [00:01<00:00, 135.61it/s]
 91%|█████████ | 182/200 [00:01<00:00, 135.94it/s]
 98%|█████████▊| 196/200 [00:01<00:00, 136.74it/s]
100%|██████████| 200/200 [00:01<00:00, 135.54it/s]
H0                             trade_share=0
ri-type                      randomization-c
Estimate                 -0.0590146075590053
Pr(>|t|)                            0.145729
Std. Error (Pr(>|t|))               0.041141
2.5% (Pr(>|t|))                     0.078058
97.5% (Pr(>|t|))                    0.213399
Cluster                           country_id
dtype: object