【R】jtools

1. はじめに

jtoolsパッケージは、回帰問題の結果の表示を手助けしてくれます。

2. インストール

CRANからインストールできます。

install.packages("jtools")

最新版は、Githubから。

devtools::install_github("jacob-long/jtools")

3. 使ってみる

summ()関数で、回帰のsummaryを見やすく表示してくれます。

library(jtools)
dat <- movies
fit <- glm(metascore ~ log(budget) + runtime + log(us_gross) + log(int_gross), data = movies)
summ(fit)
> summ(fit)
MODEL INFO:
Observations: 831 (10 missing obs. deleted)
Dependent Variable: metascore
Type: Linear regression 

MODEL FIT:
χ²(4) = 42611.56, p = 0.00
Pseudo-R² (Cragg-Uhler) = 0.18
Pseudo-R² (McFadden) = 0.02
AIC = 6904.41, BIC = 6932.75 

Standard errors: MLE
---------------------------------------------------
                        Est.   S.E.   t val.      p
-------------------- ------- ------ -------- ------
(Intercept)            85.14   8.48    10.04   0.00
log(budget)            -7.23   0.62   -11.75   0.00
runtime                13.87   1.64     8.45   0.00
log(us_gross)          -0.16   1.32    -0.12   0.91
log(int_gross)          4.31   1.32     3.28   0.00
---------------------------------------------------

Estimated dispersion parameter = 235.63 

variance inflation factors (VIFs)も表示できます。

summ(fit, scale = TRUE, vifs = TRUE, part.corr = TRUE, confint = TRUE, pvals = FALSE)
> summ(fit, scale = TRUE, vifs = TRUE, part.corr = TRUE, confint = TRUE, pvals = FALSE)
MODEL INFO:
Observations: 831 (10 missing obs. deleted)
Dependent Variable: metascore
Type: Linear regression 

MODEL FIT:
χ²(4) = 42611.56, p = 0.00
Pseudo-R² (Cragg-Uhler) = 0.18
Pseudo-R² (McFadden) = 0.02
AIC = 6904.41, BIC = 6932.75 

Standard errors: MLE
---------------------------------------------------------------
                          Est.    2.5%   97.5%   t val.     VIF
---------------------- ------- ------- ------- -------- -------
(Intercept)              63.01   61.97   64.05   118.33        
log(budget)              -8.47   -9.88   -7.06   -11.75    1.83
runtime                   4.88    3.75    6.01     8.45    1.18
log(us_gross)            -0.21   -3.71    3.28    -0.12   11.20
log(int_gross)            6.11    2.45    9.76     3.28   12.25
---------------------------------------------------------------

Estimated dispersion parameter = 235.63 

Continuous predictors are mean-centered and scaled by 1 s.d.

複数の回帰モデルを表示するときも、このパッケージは非常に有用です。

library(ggstance)
library(sandwich)
fit <- lm(metascore ~ log(budget), data = movies)
fit_b <- lm(metascore ~ log(budget) + log(us_gross), data = movies)
fit_c <- lm(metascore ~ log(budget) + log(us_gross) + runtime, data = movies)
coef_names <- c("Budget" = "log(budget)", "US Gross" = "log(us_gross)",
                "Runtime (Hours)" = "runtime", "Constant" = "(Intercept)")

coef_names <- coef_names[1:3] # Dropping intercept for plots
plot_summs(fit, fit_b, fit_c, robust = "HC3", coefs = coef_names)
plot_summs(fit, fit_b, fit_c, robust = "HC3", coefs = coef_names, plot.distributions = TRUE)

4. さいごに

このパッケージはTwitterで知りましたが、なかなか使えそうです。

Add a Comment

メールアドレスが公開されることはありません。