当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R broom tidy.margins 整理 a(n) 边距对象


Tidy 总结了有关模型组件的信息。模型组件可能是回归中的单个项、单个假设、聚类或类。 tidy 所认为的模型组件的确切含义因模型而异,但通常是不言而喻的。如果模型具有多种不同类型的组件,您将需要指定要返回哪些组件。

用法

# S3 method for margins
tidy(x, conf.int = FALSE, conf.level = 0.95, ...)

参数

x

margins::margins() 返回的 margins 对象。

conf.int

逻辑指示是否在整理的输出中包含置信区间。默认为 FALSE

conf.level

用于置信区间的置信水平(如果 conf.int = TRUE )。必须严格大于 0 且小于 1。默认为 0.95,对应于 95% 的置信区间。

...

附加参数。不曾用过。仅需要匹配通用签名。注意:拼写错误的参数将被吸收到 ... 中,并被忽略。如果拼写错误的参数有默认值,则将使用默认值。例如,如果您传递 conf.lvel = 0.9 ,所有计算将使用 conf.level = 0.95 进行。这里有两个异常:

  • tidy() 方法在提供 exponentiate 参数时会发出警告(如果该参数将被忽略)。

  • augment() 方法在提供 newdata 参数时会发出警告(如果该参数将被忽略)。

细节

margins 包提供了一种获取各种(非线性)模型(例如 logit 或具有多路交互项的模型)的系数边际效应的方法。请注意,glance.margins() 方法需要再次重新运行底层模型,这可能需要一些时间。同样,目前不支持augment.margins()方法,但用户可以简单地运行底层模型来获取相同的信息。

也可以看看

带有列的 tibble::tibble()

conf.high

估计置信区间的上限。

conf.low

估计置信区间的下限。

estimate

回归项的估计值。

p.value

与观察到的统计量相关的两侧 p 值。

statistic

在回归项非零的假设中使用的 T-statistic 的值。

std.error

回归项的标准误差。

term

回归项的名称。

例子


# load libraries for models and data
library(margins)

# example 1: logit model
mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial)

# get tidied "naive" model coefficients
tidy(mod_log)
#> # A tibble: 4 × 5
#>   term        estimate std.error statistic p.value
#>   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept)  19.7       8.12       2.43   0.0152
#> 2 cyl           0.488     1.07       0.455  0.649 
#> 3 hp            0.0326    0.0189     1.73   0.0840
#> 4 wt           -9.15      4.15      -2.20   0.0276

# convert to marginal effects with margins()
marg_log <- margins(mod_log)

# get tidied marginal effects
tidy(marg_log)
#> # A tibble: 3 × 5
#>   term  estimate std.error statistic  p.value
#>   <chr>    <dbl>     <dbl>     <dbl>    <dbl>
#> 1 cyl    0.0215   0.0470       0.457 0.648   
#> 2 hp     0.00143  0.000618     2.32  0.0204  
#> 3 wt    -0.403    0.115       -3.49  0.000487
tidy(marg_log, conf.int = TRUE)
#> # A tibble: 3 × 7
#>   term  estimate std.error statistic  p.value  conf.low conf.high
#>   <chr>    <dbl>     <dbl>     <dbl>    <dbl>     <dbl>     <dbl>
#> 1 cyl    0.0215   0.0470       0.457 0.648    -0.0706     0.114  
#> 2 hp     0.00143  0.000618     2.32  0.0204    0.000222   0.00265
#> 3 wt    -0.403    0.115       -3.49  0.000487 -0.629     -0.176  

# requires running the underlying model again. quick for this example
glance(marg_log)
#> # A tibble: 1 × 8
#>   null.deviance df.null logLik   AIC   BIC deviance df.residual  nobs
#>           <dbl>   <int>  <dbl> <dbl> <dbl>    <dbl>       <int> <int>
#> 1          43.2      31  -4.92  17.8  23.7     9.84          28    32

# augmenting `margins` outputs isn't supported, but
# you can get the same info by running on the underlying model
augment(mod_log)
#> # A tibble: 32 × 11
#>    .rownames    am   cyl    hp    wt .fitted  .resid   .hat .sigma .cooksd
#>    <chr>     <dbl> <dbl> <dbl> <dbl>   <dbl>   <dbl>  <dbl>  <dbl>   <dbl>
#>  1 Mazda RX4     1     6   110  2.62  2.24    0.449  0.278   0.595 1.42e-2
#>  2 Mazda RX…     1     6   110  2.88 -0.0912  1.22   0.352   0.529 2.30e-1
#>  3 Datsun 7…     1     4    93  2.32  3.46    0.249  0.0960  0.602 9.26e-4
#>  4 Hornet 4…     0     6   110  3.22 -3.20   -0.282  0.0945  0.601 1.17e-3
#>  5 Hornet S…     0     8   175  3.44 -2.17   -0.466  0.220   0.595 1.03e-2
#>  6 Valiant       0     6   105  3.46 -5.61   -0.0856 0.0221  0.604 2.12e-5
#>  7 Duster 3…     0     8   245  3.57 -1.07   -0.766  0.337   0.576 6.55e-2
#>  8 Merc 240D     0     4    62  3.19 -5.51   -0.0897 0.0376  0.603 4.10e-5
#>  9 Merc 230      0     4    95  3.15 -4.07   -0.184  0.122   0.603 6.76e-4
#> 10 Merc 280      0     6   123  3.44 -4.84   -0.126  0.0375  0.603 8.02e-5
#> # ℹ 22 more rows
#> # ℹ 1 more variable: .std.resid <dbl>

# example 2: threeway interaction terms
mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars)

# get tidied "naive" model coefficients
tidy(mod_ie)
#> # A tibble: 8 × 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept) 108.      23.3          4.62 0.000109
#> 2 wt          -24.8      8.47        -2.92 0.00744 
#> 3 cyl         -10.8      4.34        -2.49 0.0201  
#> 4 disp         -0.593    0.213       -2.79 0.0102  
#> 5 wt:cyl        2.91     1.42         2.05 0.0514  
#> 6 wt:disp       0.184    0.0685       2.69 0.0127  
#> 7 cyl:disp      0.0752   0.0268       2.81 0.00979 
#> 8 wt:cyl:disp  -0.0233   0.00861     -2.71 0.0123  

# convert to marginal effects with margins()
marg_ie0 <- margins(mod_ie)
# get tidied marginal effects
tidy(marg_ie0)
#> # A tibble: 3 × 5
#>   term  estimate std.error statistic p.value
#>   <chr>    <dbl>     <dbl>     <dbl>   <dbl>
#> 1 cyl    -3.85      1.46       -2.65 0.00812
#> 2 disp   -0.0295    0.0174     -1.70 0.0900 
#> 3 wt     -2.01      1.17       -1.72 0.0860 
glance(marg_ie0)
#> # A tibble: 1 × 12
#>   r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC
#>       <dbl>         <dbl> <dbl>     <dbl>    <dbl> <dbl>  <dbl> <dbl>
#> 1     0.896         0.865  2.21      29.4 2.75e-10     7  -66.2  150.
#> # ℹ 4 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>,
#> #   nobs <int>

# marginal effects evaluated at specific values of a variable (here: cyl)
marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8)))

# summarize model fit with tidiers
tidy(marg_ie1)
#> # A tibble: 9 × 7
#>   term  at.variable at.value  estimate std.error statistic p.value
#>   <chr> <chr>          <dbl>     <dbl>     <dbl>     <dbl>   <dbl>
#> 1 cyl   cyl                4 -3.85        1.46     -2.65   0.00808
#> 2 cyl   cyl                6 -3.85        1.46     -2.65   0.00814
#> 3 cyl   cyl                8 -3.85        1.46     -2.65   0.00812
#> 4 disp  cyl                4  0.000978    0.0314    0.0312 0.975  
#> 5 disp  cyl                6  0.00134     0.0182    0.0737 0.941  
#> 6 disp  cyl                8  0.00170     0.0120    0.141  0.888  
#> 7 wt    cyl                4  7.91        5.06      1.56   0.118  
#> 8 wt    cyl                6  2.96        2.52      1.18   0.239  
#> 9 wt    cyl                8 -1.98        2.40     -0.825  0.409  

# marginal effects of one interaction variable (here: wt), modulated at
# specific values of the two other interaction variables (here: cyl and drat)
marg_ie2 <- margins(mod_ie,
                    variables = "wt",
                    at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4)))

# summarize model fit with tidiers
tidy(marg_ie2)
#> # A tibble: 18 × 7
#>    term  at.variable at.value estimate std.error statistic p.value
#>    <chr> <chr>          <dbl>    <dbl>     <dbl>     <dbl>   <dbl>
#>  1 wt    cyl              4       7.91      5.06     1.56    0.118
#>  2 wt    drat             3       7.91      5.06     1.56    0.118
#>  3 wt    cyl              4       7.91      5.06     1.56    0.118
#>  4 wt    drat             3.5     7.91      5.06     1.56    0.118
#>  5 wt    cyl              4       7.91      5.06     1.56    0.118
#>  6 wt    drat             4       7.91      5.06     1.56    0.118
#>  7 wt    cyl              6       2.96      2.52     1.18    0.239
#>  8 wt    drat             3       2.96      2.52     1.18    0.239
#>  9 wt    cyl              6       2.96      2.52     1.18    0.239
#> 10 wt    drat             3.5     2.96      2.52     1.18    0.239
#> 11 wt    cyl              6       2.96      2.52     1.18    0.239
#> 12 wt    drat             4       2.96      2.52     1.18    0.239
#> 13 wt    cyl              8      -1.98      2.40    -0.825   0.409
#> 14 wt    drat             3      -1.98      2.40    -0.825   0.409
#> 15 wt    cyl              8      -1.98      2.40    -0.825   0.409
#> 16 wt    drat             3.5    -1.98      2.40    -0.825   0.409
#> 17 wt    cyl              8      -1.98      2.40    -0.825   0.409
#> 18 wt    drat             4      -1.98      2.40    -0.825   0.409

相关用法


注:本文由纯净天空筛选整理自大神的英文原创作品 Tidy a(n) margins object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。