1 パッケージ読み込み

library(gt)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

2 データ

library(wooldridge)
data(wage1)

3 回帰分析

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

models <- list(lm1, lm2, lm3)

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

tab1 <- modelsummary::msummary(models, 
  coef_omit = ".*Intercept*", 
  coef_rename = coef_rename,
  gof_omit    = "Adj|Within|IC|RMSE", 
  stars       = TRUE,
  output = "gt")
tab1
(1) (2) (3)
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

4 タイトルを追加

tab1 <- 
  gt::tab_header(tab1,
    title = "OLS Results",
    subtitle = "Wage Equation",
  )
tab1
OLS Results
Wage Equation
(1) (2) (3)
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

5 脚注を追加

tab1 <- 
  gt::tab_footnote(tab1,
    footnote = "Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey."
  )
tab1
OLS Results
Wage Equation
(1) (2) (3)
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.

6 グループ化

tab1 <- 
  gt::tab_spanner(tab1, 
                  "Model 1 & 2", 
                  c(2, 3)
  )
tab1
OLS Results
Wage Equation
Model 1 & 2
(3)
(1) (2)
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.

7 桁数の変更

tab1 <- 
  gt::fmt_number(tab1, 
                 columns = 2:4, 
                 decimals = 6
                 )
tab1
OLS Results
Wage Equation
Model 1 & 2
(3)
(1) (2)
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.

8 列名の変更

tab1 <- 
  gt::cols_label(tab1, 
                 "(1)" =  "Model 1", 
                 "(2)" =  "Model 2", 
                 "(3)" =  "Model 3"
                 )
tab1
OLS Results
Wage Equation
Model 1 & 2
Model 3
Model 1 Model 2
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.

9 デザインの変更

  • gtExtrasパッケージを使って、表のデザインを変更
library(gtExtras)
tab1 <- gtExtras::gt_theme_espn(tab1)
tab1
OLS Results
Wage Equation
Model 1 & 2
Model 3
Model 1 Model 2
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.
tab1 <- gtExtras::gt_theme_nytimes(tab1)
tab1
OLS Results
Wage Equation
Model 1 & 2
Model 3
Model 1 Model 2
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.
tab1 <- gtExtras::gt_theme_dot_matrix(tab1)
tab1
OLS Results
Wage Equation
Model 1 & 2
Model 3
Model 1 Model 2
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)
Num.Obs. 526 526 526
R2 0.186 0.249 0.316
Log.Lik. -359.378 -338.009 -313.548
F 119.582 86.862 80.391
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.

10 推定結果をLaTeX形式で出力

gt::gtsave(tab1, "tab1.tex")

tab1.tex <- readLines("tab1.tex")
tab1.tex
##  [1] "\\begin{table}[!t]"                                                                 
##  [2] "\\caption*{"                                                                        
##  [3] "{\\large OLS Results} \\\\ "                                                        
##  [4] "{\\small Wage Equation}"                                                            
##  [5] "} "                                                                                 
##  [6] "\\fontsize{12.0pt}{14.4pt}\\selectfont"                                             
##  [7] "\\begin{tabular*}{\\linewidth}{@{\\extracolsep{\\fill}}lccc}"                       
##  [8] "\\toprule"                                                                          
##  [9] " & \\multicolumn{2}{c}{Model 1 \\& 2} &  \\\\ "                                     
## [10] "\\cmidrule(lr){2-3}"                                                                
## [11] "{\\TEXTCOLOR[HTML]{A9A9A9}{ }} & Model 1 & Model 2 & Model 3 \\\\ "                 
## [12] "\\midrule\\addlinespace[2.5pt]"                                                     
## [13] "{Education} & {0.083***} & {0.098***} & {0.092***} \\\\ "                           
## [14] "{} & {(0.008)} & {(0.008)} & {(0.007)} \\\\ "                                       
## [15] "{Experience} & {} & {0.010***} & {0.004*} \\\\ "                                    
## [16] "{} & {} & {(0.002)} & {(0.002)} \\\\ "                                              
## [17] "{Tenure} & {} & {} & {0.022***} \\\\ "                                              
## [18] "{} & {} & {} & {(0.003)} \\\\ "                                                     
## [19] "{Num.Obs.} & {526} & {526} & {526} \\\\ "                                           
## [20] "{R2} & {0.186} & {0.249} & {0.316} \\\\ "                                           
## [21] "{Log.Lik.} & {-359.378} & {-338.009} & {-313.548} \\\\ "                            
## [22] "{F} & {119.582} & {86.862} & {80.391} \\\\ "                                        
## [23] "\\bottomrule"                                                                       
## [24] "\\end{tabular*}"                                                                    
## [25] "\\begin{minipage}{\\linewidth}"                                                     
## [26] "Notes: Std. errors are clustered at the ABC level. Data is from the XYZ survey.\\\\"
## [27] "+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001\\\\"                              
## [28] "\\end{minipage}"                                                                    
## [29] "\\end{table}"                                                                       
## [30] ""

LaTexでは、以下のパッケージを読み込むことで、表が出力される。

  \usepackage{booktabs, caption, longtable, colortbl, array}