stargazer 形式の回帰表

入れ子になった 3 つの OLS モデルを横に並べ、係数の下に標準誤差、末尾に適合度の統計量を置いて、学術誌の表のように組みます。

stargazer 形式の回帰表

実証論文には必ずある表です。モデルごとに 1 列、係数を 1 行に、その下に括弧付きの標準誤差を置き、有意性を星で示し、末尾に観測数、R²、自由度調整済み R²、残差標準誤差、F 統計量を並べます。R の stargazer() が出力する形式で、ここでは Great Tables(great_tables.GT)で作り、Econometrica などの学術誌の体裁に整えています。セリフ体、スモールキャピタルの番号付き表題、横罫線のみ(上端、列見出しの下、統計量の上、下端)で、縦罫線はありません。

設定は Stojkov, Veljanovski & Warin (2024, Post-Communist Economies) による、新興欧州の成長、制度、政策要因の研究です。データは説明のためのシミュレーションであり、論文のデータや結果ではありません。n = 220 の国・年(11 か国 × 20 年)です。モデル (1) は制度変数のみ、(2) は投資と貿易開放度を、(3) はインフレと政府消費を加えます。説明変数は標準化しているので、各係数は 1 標準偏差の変化に対する成長率の変化(パーセントポイント)です。モデルは smf.ols() で推定し、表は params、bse、pvalues、rsquared、fvalue などから組み立て、table.show() でブラウザに表示します。

コードおよび図中のテキストは英語です。

regression-table.py · 93 行
ダウンロード GitHub
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
from great_tables import GT, loc, style
# Simulated data, set in the context of Stojkov, Veljanovski & Warin (2024) [stojkov2024growth]

# Data: 11 emerging-European economies × 20 years, six institutional and policy factors
rng = np.random.default_rng(2024)
n = 220
labels = {"rule_law": "Rule of law", "reg_quality": "Regulatory quality",
          "investment": "Investment (% of GDP)", "openness": "Trade openness",
          "inflation": "Inflation", "gov_size": "Government consumption",
          "Intercept": "Constant"}
predictors = list(labels)[:-1]
df = pd.DataFrame(rng.normal(size=(n, 6)), columns=predictors)
df["growth"] = (3 + 0.9 * df.rule_law + 0.45 * df.reg_quality + 0.8 * df.investment
                + 0.15 * df.openness - 0.7 * df.inflation - 0.05 * df.gov_size
                + rng.normal(0, 2.5, n))
df[predictors] = (df[predictors] - df[predictors].mean()) / df[predictors].std()

# Three nested models, each adding a block of predictors
blocks = [["rule_law", "reg_quality"], ["investment", "openness"], ["inflation", "gov_size"]]
models = [smf.ols("growth ~ " + " + ".join(sum(blocks[:k + 1], [])), data=df).fit()
          for k in range(len(blocks))]
cols = [f"({k + 1})" for k in range(len(models))]


def stars(p):
    return "***" if p < .01 else "**" if p < .05 else "*" if p < .1 else ""


def num(x, d=3):                      # typographic minus sign, as in print
    return f"{x:.{d}f}".replace("-", "−")


# Body: a coefficient row and a standard-error row per term, as in stargazer
rows = []
for term, name in labels.items():
    coef = {c: (num(m.params[term]) + stars(m.pvalues[term]) if term in m.params else "")
            for c, m in zip(cols, models)}
    se = {c: (f"({num(m.bse[term])})" if term in m.params else "") for c, m in zip(cols, models)}
    rows += [{"term": name, **coef}, {"term": "", **se}]
n_body = len(rows)
coef_rows, se_rows = list(range(0, n_body, 2)), list(range(1, n_body, 2))

# Foot: fit statistics, one row each
rows += [
    {"term": "Observations", **{c: f"{int(m.nobs)}" for c, m in zip(cols, models)}},
    {"term": "R²", **{c: f"{m.rsquared:.3f}" for c, m in zip(cols, models)}},
    {"term": "Adjusted R²", **{c: f"{m.rsquared_adj:.3f}" for c, m in zip(cols, models)}},
    {"term": "Residual std. error", **{c: f"{np.sqrt(m.scale):.3f} (df = {m.df_resid:.0f})"
                                       for c, m in zip(cols, models)}},
    {"term": "F statistic", **{c: f"{m.fvalue:.2f}{stars(m.f_pvalue)} (df = {m.df_model:.0f}; {m.df_resid:.0f})"
                               for c, m in zip(cols, models)}},
]
tab = pd.DataFrame(rows)

table = (
    GT(tab, rowname_col="term")
    .tab_header(title="TABLE I",
                subtitle="Institutions, policy and growth in emerging Europe")
    .tab_spanner(label="Dependent variable: GDP-per-capita growth (%)", columns=cols, id="dep")
    .cols_align("center", columns=cols)
    .cols_width({"term": "190px", **{c: "185px" for c in cols}})
    .tab_source_note("Notes: OLS estimates; standard errors in parentheses. Predictors are "
                     "standardised, so coefficients are percentage points of growth per 1 SD.")
    .tab_source_note(f"n = {n} country-years (simulated). *p < 0.1; **p < 0.05; ***p < 0.01.")
    # Econometrica look: serif type, small-caps title, horizontal rules only
    .opt_table_font(font=["Latin Modern Roman", "Times New Roman", "Georgia", "serif"])
    .tab_options(
        table_width="745px", table_font_size="15px", table_font_color="#000",
        heading_title_font_size="15px", heading_subtitle_font_size="15px", heading_padding="1px",
        table_border_top_style="hidden", table_border_bottom_style="hidden",
        heading_border_bottom_style="none",
        column_labels_border_top_style="solid", column_labels_border_top_width="1.5px",   # top rule
        column_labels_border_top_color="#000",
        column_labels_border_bottom_style="solid", column_labels_border_bottom_width="1px",
        column_labels_border_bottom_color="#000",
        table_body_hlines_style="hidden", stub_border_style="hidden",
        table_body_border_bottom_style="solid", table_body_border_bottom_width="1.5px",
        table_body_border_bottom_color="#000",
        data_row_padding="4px", source_notes_font_size="13px", source_notes_padding="1px",
    )
    .tab_style(style.css("font-variant: small-caps; padding-bottom: 8px;"), loc.subtitle())
    .tab_style(style.text(style="italic"), loc.spanner_labels(ids=["dep"]))
    # standard error tucked under its coefficient
    .tab_style(style.css("padding-bottom: 0;"), [loc.body(rows=coef_rows), loc.stub(rows=coef_rows)])
    .tab_style(style.css("padding-top: 0;"), [loc.body(rows=se_rows), loc.stub(rows=se_rows)])
    # thin rule between the coefficients and the fit statistics
    .tab_style(style.borders(sides="top", style="solid", weight="1px", color="#000"),
               [loc.body(rows=[n_body]), loc.stub(rows=[n_body])])
)
table.show()

図にはオプションの PyStatlas スタイル を使用しています。同じ見た目にするには plt.style.use("pystatlas.mplstyle") を追加してください。

データの出典

  • Simulated data. Simulated data with a fixed random seed, created for this example.
    No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work.
  • Stojkov, Veljanovski & Warin (2024), European integration and growth in emerging Europe. Stojkov, A., Veljanovski, M., & Warin, T. (2024). European integration and economic growth in emerging Europe: The role of institutions and policy factors. Post-Communist Economies, 36(7), 778–800. https://doi.org/10.1080/14631377.2024.2376974
    The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not the paper's data or results.

これらの例は統計手法を説明するためのものです。項目に別の記載がない限り数値はシミュレーションであり、図のタイトルはこのシミュレーションデータで図が示す内容を述べたものです。引用した論文の知見でも、実質的な問いに対する著者の立場でもありません。結果、背景、データについては、ここに挙げた論文を参照してください。

引用方法

PyStatlas は Thierry Warin が作成・運営しています。研究や業務に役立った場合は、ぜひ引用してください。

Warin, T. (2026). Regression table, stargazer style. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/academic-tables/regression-table/

@misc{warin2026statlas,
  author       = {Warin, Thierry},
  title        = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
  year         = {2026},
  version      = {1.1.0},
  howpublished = {\url{https://warin.ca/pystatlas/}},
  note         = {Source code: https://github.com/warint/pystatlas}
}

学術論文の表のその他のグラフ