Skip to contents

調査実習用の回帰分析結果を作成する関数です。

Usage

jisshu_reg(object)

# S3 method for class 'glm'
jisshu_reg(object)

# S3 method for class 'lm'
jisshu_reg(object)

# S3 method for class 'multinom'
jisshu_reg(object)

Arguments

object

an object for which a summary is desired. For example, a model object, such as those returned by lm, glm, and multinom.

Details

lmglmnnet::multinomの結果から、執筆要項に沿ったエクセルの表が作成できます。

エクセルへの保存はexampleを参照してください。

Examples

sample_data <- tibble::tibble(
  x1 = rnorm(100, mean = 0, sd = 1),
  x2 = rnorm(100, mean = 0, sd = 1),
  y = 0.2 + 0.3*x1 + 0.5*x2 + rnorm(100, mean = 0, sd = 0.1),
  y_bin = rbinom(100, 1, plogis(y)),
  y_multinom = cut(
    y,
    breaks = quantile(y, probs = c(0, 0.25, 0.75, 1)),
    labels = c('Q1', 'Q2', 'Q3'),
    include.lowest = TRUE
  )
)

sample_data
#> # A tibble: 100 × 5
#>          x1     x2       y y_bin y_multinom
#>       <dbl>  <dbl>   <dbl> <int> <fct>     
#>  1 -1.40    -0.387 -0.457      0 Q1        
#>  2  0.255   -0.785  0.0199     0 Q2        
#>  3 -2.44    -1.06  -1.07       0 Q1        
#>  4 -0.00557 -0.796 -0.227      1 Q1        
#>  5  0.622   -1.76  -0.736      1 Q1        
#>  6  1.15    -0.691  0.206      0 Q2        
#>  7 -1.82    -0.559 -0.736      1 Q1        
#>  8 -0.247   -0.537 -0.206      0 Q2        
#>  9 -0.244    0.227  0.0339     0 Q2        
#> 10 -0.283    0.978  0.869      0 Q3        
#> # ℹ 90 more rows

# Create a regression table
# Linear regression
model_lm <- lm(y ~ x1 + x2, data = sample_data)
result_lm <- jisshu_reg(model_lm)
#> # A tibble: 6 × 4
#>   独立変数               偏回帰係数 標準誤差 p.value
#>   <chr>                  <chr>      <chr>    <chr>  
#> 1 (定数)               0.193      0.010    ***    
#> 2 x1                     0.311      0.010    ***    
#> 3 x2                     0.525      0.010    ***    
#> 4 自由度調整済み決定係数 0.973      NA       NA     
#> 5 F値                    1792.107   ***      NA     
#> 6 N                      100        NA       NA     

# Logistic regression
model_glm <- glm(y_bin ~ x1 + x2, data = sample_data, family = binomial(link = 'logit'))
result_glm <- jisshu_reg(model_glm)
#> # A tibble: 6 × 4
#>   独立変数           偏回帰係数 標準誤差 p.value
#>   <chr>              <chr>      <chr>    <chr>  
#> 1 (定数)           0.161      "0.206"  ""     
#> 2 x1                 0.333      "0.202"  "+"    
#> 3 x2                 0.199      "0.204"  ""     
#> 4 Nagelkerke決定係数 0.049       NA       NA    
#> 5 モデルχ2乗値       3.696      ""        NA    
#> 6 N                  100         NA       NA    

# Multinomial logistic regression
model_multinom <-
  nnet::multinom(y_multinom ~ x1 + x2, data = sample_data, model = TRUE, trace = FALSE)
result_multinom <- jisshu_reg(model_multinom)
#> # A tibble: 9 × 5
#>   y_multinom 独立変数           偏回帰係数 標準誤差 p.value
#>   <chr>      <chr>              <chr>      <chr>    <chr>  
#> 1 Q2         (定数)           27.037     17.688   ""     
#> 2 Q2         x1                 24.225     15.582   ""     
#> 3 Q2         x2                 36.973     23.761   ""     
#> 4 Q3         (定数)           15.108     18.234   ""     
#> 5 Q3         x1                 30.633     15.756   "+"    
#> 6 Q3         x2                 49.717     24.253   "*"    
#> 7 NA         Nagelkerke決定係数 0.969      NA        NA    
#> 8 NA         モデルχ2乗値       188.366    ***       NA    
#> 9 NA         N                  100        NA        NA    


# Glance the regression table
if (FALSE) { # \dontrun{
  result_lm$open()
  result_glm$open()
  result_multinom$open()
} # }

# Save the regression table as an excel file
if (FALSE) { # \dontrun{
  result_lm$save('hoge_lm.xlsx')
  result_glm$save('hoge_glm.xlsx')
  result_multinom$save('hoge_multinom.xlsx')
} # }