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


R broom augment.ivreg 使用来自 ivreg 对象的信息增强数据


Augment 接受模型对象和数据集,并添加有关数据集中每个观察值的信息。最常见的是,这包括 .fitted 列中的预测值、.resid 列中的残差以及 .se.fit 列中拟合值的标准误差。新列始终以 . 前缀开头,以避免覆盖原始数据集中的列。

用户可以通过 data 参数或 newdata 参数传递数据以进行增强。如果用户将数据传递给 data 参数,则它必须正是用于拟合模型对象的数据。将数据集传递给 newdata 以扩充模型拟合期间未使用的数据。这仍然要求至少存在用于拟合模型的所有预测变量列。如果用于拟合模型的原始结果变量未包含在 newdata 中,则输出中不会包含 .resid 列。

根据是否给出 datanewdata,增强的行为通常会有所不同。这是因为通常存在与训练观察(例如影响或相关)测量相关的信息,而这些信息对于新观察没有有意义的定义。

为了方便起见,许多增强方法提供默认的 data 参数,以便 augment(fit) 将返回增强的训练数据。在这些情况下,augment 尝试根据模型对象重建原始数据,并取得了不同程度的成功。

增强数据集始终以 tibble::tibble 形式返回,其行数与传递的数据集相同。这意味着传递的数据必须可强制转换为 tibble。如果预测变量将模型作为协变量矩阵的一部分输入,例如当模型公式使用 splines::ns()stats::poly()survival::Surv() 时,它会表示为矩阵列。

我们正在定义适合各种 na.action 参数的模型的行为,但目前不保证数据丢失时的行为。

用法

# S3 method for ivreg
augment(x, data = model.frame(x), newdata = NULL, ...)

参数

x

通过调用 AER::ivreg() 创建的 ivreg 对象。

data

base::data.frametibble::tibble() 包含用于生成对象 x 的原始数据。默认为stats::model.frame(x),以便augment(my_fit) 返回增强的原始数据。不要将新数据传递给 data 参数。增强将报告传递给 data 参数的数据的影响和烹饪距离等信息。这些度量仅针对原始训练数据定义。

newdata

base::data.frame()tibble::tibble() 包含用于创建 x 的所有原始预测变量。默认为 NULL ,表示没有任何内容传递给 newdata 。如果指定了newdata,则data 参数将被忽略。

...

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

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

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

细节

此整理器当前仅支持 AER 包输出的 ivreg 类对象。 ivreg 包还输出类 ivreg 的对象,并将在以后的版本中得到支持。

也可以看看

augment() , AER::ivreg()

其他 ivreg 整理器:glance.ivreg()tidy.ivreg()

带有列的 tibble::tibble()

.fitted

拟合值或预测值。

.resid

观察值和拟合值之间的差异。

例子


# load libraries for models and data
library(AER)
#> Loading required package: car
#> Loading required package: carData
#> 
#> Attaching package: ‘car’
#> The following object is masked from ‘package:purrr’:
#> 
#>     some
#> The following object is masked from ‘package:dplyr’:
#> 
#>     recode

# load data
data("CigarettesSW", package = "AER")

# fit model
ivr <- ivreg(
  log(packs) ~ income | population,
  data = CigarettesSW,
  subset = year == "1995"
)

# summarize model fit with tidiers
tidy(ivr)
#> # A tibble: 2 × 5
#>   term         estimate std.error statistic  p.value
#>   <chr>           <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)  4.61e+ 0  4.45e- 2    104.   3.74e-56
#> 2 income      -5.71e-10  2.33e-10     -2.44 1.84e- 2
tidy(ivr, conf.int = TRUE)
#> # A tibble: 2 × 7
#>   term         estimate std.error statistic  p.value conf.low conf.high
#>   <chr>           <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
#> 1 (Intercept)  4.61e+ 0  4.45e- 2    104.   3.74e-56  4.52e+0  4.70e+ 0
#> 2 income      -5.71e-10  2.33e-10     -2.44 1.84e- 2 -1.03e-9 -1.13e-10
tidy(ivr, conf.int = TRUE, instruments = TRUE)
#> # A tibble: 1 × 5
#>   term   num.df den.df statistic  p.value
#>   <chr>   <dbl>  <dbl>     <dbl>    <dbl>
#> 1 income      1     46     3329. 1.46e-44

augment(ivr)
#> # A tibble: 48 × 6
#>    .rownames `log(packs)`    income population .fitted  .resid
#>    <chr>            <dbl>     <dbl>      <dbl>   <dbl>   <dbl>
#>  1 49                4.62  83903280    4262731    4.56  0.0522
#>  2 50                4.71  45995496    2480121    4.59  0.124 
#>  3 51                4.28  88870496    4306908    4.56 -0.285 
#>  4 52                4.04 771470144   31493524    4.17 -0.131 
#>  5 53                4.41  92946544    3738061    4.56 -0.145 
#>  6 54                4.38 104315120    3265293    4.55 -0.177 
#>  7 55                4.82  18237436     718265    4.60  0.223 
#>  8 56                4.53 333525344   14185403    4.42  0.112 
#>  9 57                4.58 159800448    7188538    4.52  0.0591
#> 10 58                4.53  60170928    2840860    4.58 -0.0512
#> # ℹ 38 more rows
augment(ivr, data = CigarettesSW)
#> # A tibble: 96 × 11
#>    state year    cpi population packs    income   tax price  taxs .fitted
#>    <fct> <fct> <dbl>      <dbl> <dbl>     <dbl> <dbl> <dbl> <dbl>   <dbl>
#>  1 AL    1985   1.08    3973000  116.  46014968  32.5 102.   33.3    4.56
#>  2 AR    1985   1.08    2327000  129.  26210736  37   101.   37      4.59
#>  3 AZ    1985   1.08    3184000  105.  43956936  31   109.   36.2    4.56
#>  4 CA    1985   1.08   26444000  100. 447102816  26   108.   32.1    4.17
#>  5 CO    1985   1.08    3209000  113.  49466672  31    94.3  31      4.56
#>  6 CT    1985   1.08    3201000  109.  60063368  42   128.   51.5    4.55
#>  7 DE    1985   1.08     618000  144.   9927301  30   102.   30      4.60
#>  8 FL    1985   1.08   11352000  122. 166919248  37   115.   42.5    4.42
#>  9 GA    1985   1.08    5963000  127.  78364336  28    97.0  28.8    4.52
#> 10 IA    1985   1.08    2830000  114.  37902896  34   102.   37.9    4.58
#> # ℹ 86 more rows
#> # ℹ 1 more variable: .resid <dbl>
augment(ivr, newdata = CigarettesSW)
#> # A tibble: 96 × 10
#>    state year    cpi population packs    income   tax price  taxs .fitted
#>    <fct> <fct> <dbl>      <dbl> <dbl>     <dbl> <dbl> <dbl> <dbl>   <dbl>
#>  1 AL    1985   1.08    3973000  116.  46014968  32.5 102.   33.3    4.59
#>  2 AR    1985   1.08    2327000  129.  26210736  37   101.   37      4.60
#>  3 AZ    1985   1.08    3184000  105.  43956936  31   109.   36.2    4.59
#>  4 CA    1985   1.08   26444000  100. 447102816  26   108.   32.1    4.36
#>  5 CO    1985   1.08    3209000  113.  49466672  31    94.3  31      4.58
#>  6 CT    1985   1.08    3201000  109.  60063368  42   128.   51.5    4.58
#>  7 DE    1985   1.08     618000  144.   9927301  30   102.   30      4.61
#>  8 FL    1985   1.08   11352000  122. 166919248  37   115.   42.5    4.52
#>  9 GA    1985   1.08    5963000  127.  78364336  28    97.0  28.8    4.57
#> 10 IA    1985   1.08    2830000  114.  37902896  34   102.   37.9    4.59
#> # ℹ 86 more rows

glance(ivr)
#> # A tibble: 1 × 8
#>   r.squared adj.r.squared sigma statistic p.value    df df.residual  nobs
#>       <dbl>         <dbl> <dbl>     <dbl>   <dbl> <int>       <int> <int>
#> 1     0.131         0.112 0.229      5.98  0.0184     2          46    48
源代码:R/aer-tidiers.R

相关用法


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