This version: 2024-12-15

1 はじめに

  • Rパッケージmodelsummaryは、データとモデルの要約を簡単に作成するためのパッケージである。
  • このパッケージを使うと、推定結果や記述統計を表形式で表示することができる。
  • LaTeX形式で出力することも可能である。
  • kableExtraパッケージと組み合わせることで、表の見栄えをよくすることができる。
install.packages("modelsummary")
install.packages("kableExtra")
  • バージョンによって、できること・できないことが異なるので、注意が必要である。
  • 特定のバージョンをインストールしたいときは、以下のようなコードを用いる。
# 古いパッケージのインストールのため
install.packages("remotes")

# modelsummaryパッケージをインストールする。
remotes::install_version(package = "modelsummary", version = "1.4.3", 
                         dependencies = FALSE,
                         repos = "http://cran.us.r-project.org")

# LaTexへの表の出力のために、kableExtraパッケージをインストールする。
#install.packages("kableExtra")
remotes::install_version(package = "kableExtra", version = "1.4.0", 
                         dependencies = FALSE,
                         repos = "http://cran.us.r-project.org")

2 パッケージ読み込み

library(modelsummary)

3 データ

library(wooldridge)
data(wage1)

4 回帰分析

lm1 <- lm(lwage ~ educ , 
          data = wage1)
lm2 <- lm(lwage ~ educ + exper , 
          data = wage1)
lm3 <- lm(lwage ~ educ + exper + tenure, 
          data = wage1)

5 推定結果の表示のための準備

library(kableExtra)
library(modelsummary)

# longnote
longnote <- "This table shows the OLS estimates of the effect of education, experience, and tenure on log wages. The dependent variable is the natural logarithm of hourly wages. The sample consists of 526 observations. The standard errors are robust and are reported in parentheses. "


# rename
coef_rename = 
  c("educ" = "Education", 
    "exper" = "Experience",
    "tenure" = "Tenure")

models <- list("Model A" = lm1, 
               "Model B" = lm2, 
               "Model C" = lm3)

library(tibble)

rows <- tribble(~term,  ~"(1)", ~"(2)", ~"(3)",
'Firm Fixed-Effects', 'No', 'No', 'No', 
'Year Fixed-Effects', 'No', 'No', 'No'
)
attr(rows, 'position') <- c(7, 8)

6 HTML出力

modelsummary::msummary(models, 
  coef_omit = ".*Intercept*", 
  coef_rename = coef_rename,
  #gof_omit    = "Adj|Within|IC|RMSE", 
  gof_map = c("nobs", "r.squared"),
  stars = c('*' = .05, '**' = .01, '***' = .001),
  add_rows = rows,
  title       = "OLS Estimates of Log Wages",
  notes       = ""
  ) %>% 
kableExtra::add_header_above(c(" " = 1, "(1)" = 1,  "(2)" = 1, "(3)" = 1)) %>% 
kableExtra::add_header_above(c(" " = 1, "Model A & B" = 2,  "Model C" = 1)) %>%
kableExtra::footnote(general = longnote, threeparttable = TRUE) 
OLS Estimates of Log Wages
Model A & B
Model C
(1)
(2)
(3)
Model A Model B Model C
Education 0.083*** 0.098*** 0.092***
(0.008) (0.008) (0.007)
Experience 0.010*** 0.004*
(0.002) (0.002)
Tenure 0.022***
(0.003)
Firm Fixed-Effects No No No
Year Fixed-Effects No No No
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
* p < 0.05, ** p < 0.01, *** p < 0.001
Note:
This table shows the OLS estimates of the effect of education, experience, and tenure on log wages. The dependent variable is the natural logarithm of hourly wages. The sample consists of 526 observations. The standard errors are robust and are reported in parentheses.

7 LaTexファイルの出力 with plain

  • デフォルトでは、LaTexパッケージのsiunitxを使って数値を整形する。
  • 以下のコードを使うと、siunitxを使わずに、plainを使うことができる。
# デフォルトで使われているLaTexの`siunitx`を使わないようにするために、以下のオプションを設定しておく。
options("modelsummary_format_numeric_latex" = "plain")

# ドルマーク($$)で数値を囲みたい場合は、以下のオプションを設定しておく。
#options(modelsummary_format_numeric_latex = "mathmode")
  • 以下のコードを使うと、LaTexファイルを出力することができる。
  • LaTeXのPremableには、以下のコードを追加しておく。
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{threeparttable}
\usepackage{makecell}
modelsummary::msummary(models, 
  coef_omit = ".*Intercept*", 
  coef_rename = coef_rename,
  #gof_omit    = "Adj|Within|IC|RMSE", 
  gof_map = c("nobs", "r.squared"),
  stars = c('*' = .05, '**' = .01, '***' = .001),
  add_rows = rows,
  title       = "OLS Estimates of Log Wages",
  notes       = "",
  output = "latex"
  )  %>%
kableExtra::add_header_above(c(" " = 1, "(1)" = 1,  "(2)" = 1, "(3)" = 1)) %>% 
kableExtra::add_header_above(c(" " = 1, "Model A & B" = 2,  "Model C" = 1)) %>%
kableExtra::footnote(general = longnote, threeparttable = TRUE)  %>%
kableExtra::save_kable("Table1_plain.tex")
  • LaTexファイル”Table1_plain.tex”の中身は以下のようになる。
cat(readLines("Table1_plain.tex"), sep = "\n")
## \begin{table}
## \centering
## \caption{\label{tab:TableOLS2_plain}OLS Estimates of Log Wages}
## \centering
## \begin{threeparttable}
## \begin{tabular}[t]{lccc}
## \toprule
## \multicolumn{1}{c}{ } & \multicolumn{2}{c}{Model A \& B} & \multicolumn{1}{c}{Model C} \\
## \cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-4}
## \multicolumn{1}{c}{ } & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\
## \cmidrule(l{3pt}r{3pt}){2-2} \cmidrule(l{3pt}r{3pt}){3-3} \cmidrule(l{3pt}r{3pt}){4-4}
##   & Model A & Model B & Model C\\
## \midrule
## Education & 0.083*** & 0.098*** & 0.092***\\
##  & (0.008) & (0.008) & (0.007)\\
## Experience &  & 0.010*** & 0.004*\\
##  &  & (0.002) & (0.002)\\
## Tenure &  &  & 0.022***\\
##  &  &  & (0.003)\\
## \midrule
## Firm Fixed-Effects & No & No & No\\
## Year Fixed-Effects & No & No & No\\
## Num.Obs. & 526 & 526 & 526\\
## R2 & 0.186 & 0.249 & 0.316\\
## \bottomrule
## \multicolumn{4}{l}{\rule{0pt}{1em}* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001}\\
## \multicolumn{4}{l}{\rule{0pt}{1em}}\\
## \end{tabular}
## \begin{tablenotes}
## \item \textit{Note: } 
## \item This table shows the OLS estimates of the effect of education, experience, and tenure on log wages. The dependent variable is the natural logarithm of hourly wages. The sample consists of 526 observations. The standard errors are robust and are reported in parentheses. 
## \end{tablenotes}
## \end{threeparttable}
## \end{table}

8 LaTexファイルの出力 with siunitx

  • デフォルトでは、LaTexパッケージのsiunitxを使って数値を整形する。
  • LaTeXのPremableには、以下のコードを追加しておく。
\usepackage{booktabs}
\usepackage{siunitx}
\newcolumntype{d}{S[
    input-open-uncertainty=,
    input-close-uncertainty=,
    parse-numbers = false,
    table-align-text-pre=false,
    table-align-text-post=false
 ]}
library(modelsummary)
options("modelsummary_format_numeric_latex" = "siunitx")

modelsummary::msummary(models, 
  coef_omit = ".*Intercept*", 
  coef_rename = coef_rename,
  #gof_omit    = "Adj|Within|IC|RMSE", 
  gof_map = c("nobs", "r.squared"),
  stars = c('*' = .05, '**' = .01, '***' = .001),
  add_rows = rows,
  title       = "OLS Estimates of Log Wages",
  notes       = "",
  output = "latex"
  )  %>%
kableExtra::add_header_above(c(" " = 1, "(1)" = 1,  "(2)" = 1, "(3)" = 1)) %>% 
kableExtra::add_header_above(c(" " = 1, "Model A & B" = 2,  "Model C" = 1)) %>%
kableExtra::footnote(general = longnote, threeparttable = TRUE)  %>%
kableExtra::save_kable("Table2_siunitx.tex")
## Warning: To compile a LaTeX document with this table, the following commands must be placed in the document preamble:
## 
## \usepackage{booktabs}
## \usepackage{siunitx}
## \newcolumntype{d}{S[
##     input-open-uncertainty=,
##     input-close-uncertainty=,
##     parse-numbers = false,
##     table-align-text-pre=false,
##     table-align-text-post=false
##  ]}
## 
## To disable `siunitx` and prevent `modelsummary` from wrapping numeric entries in `\num{}`, call:
## 
## options("modelsummary_format_numeric_latex" = "plain")
##  This warning appears once per session.
  • LaTexファイル”Table2_siunitx.tex”の中身は以下のようになる。
  • 数値が\num{}で囲まれていることに注意。
cat(readLines("Table2_siunitx.tex"), sep = "\n")
## \begin{table}
## \centering
## \caption{\label{tab:Table_siunitx}OLS Estimates of Log Wages}
## \centering
## \begin{threeparttable}
## \begin{tabular}[t]{lccc}
## \toprule
## \multicolumn{1}{c}{ } & \multicolumn{2}{c}{Model A \& B} & \multicolumn{1}{c}{Model C} \\
## \cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-4}
## \multicolumn{1}{c}{ } & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\
## \cmidrule(l{3pt}r{3pt}){2-2} \cmidrule(l{3pt}r{3pt}){3-3} \cmidrule(l{3pt}r{3pt}){4-4}
##   & Model A & Model B & Model C\\
## \midrule
## Education & \num{0.083}*** & \num{0.098}*** & \num{0.092}***\\
##  & (\num{0.008}) & (\num{0.008}) & (\num{0.007})\\
## Experience &  & \num{0.010}*** & \num{0.004}*\\
##  &  & (\num{0.002}) & (\num{0.002})\\
## Tenure &  &  & \num{0.022}***\\
##  &  &  & (\num{0.003})\\
## \midrule
## Firm Fixed-Effects & No & No & No\\
## Year Fixed-Effects & No & No & No\\
## Num.Obs. & \num{526} & \num{526} & \num{526}\\
## R2 & \num{0.186} & \num{0.249} & \num{0.316}\\
## \bottomrule
## \multicolumn{4}{l}{\rule{0pt}{1em}* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001}\\
## \multicolumn{4}{l}{\rule{0pt}{1em}}\\
## \end{tabular}
## \begin{tablenotes}
## \item \textit{Note: } 
## \item This table shows the OLS estimates of the effect of education, experience, and tenure on log wages. The dependent variable is the natural logarithm of hourly wages. The sample consists of 526 observations. The standard errors are robust and are reported in parentheses. 
## \end{tablenotes}
## \end{threeparttable}
## \end{table}