Augment 接受模型對象和數據集,並添加有關數據集中每個觀察值的信息。最常見的是,這包括 .fitted
列中的預測值、.resid
列中的殘差以及 .se.fit
列中擬合值的標準誤差。新列始終以 .
前綴開頭,以避免覆蓋原始數據集中的列。
用戶可以通過 data
參數或 newdata
參數傳遞數據以進行增強。如果用戶將數據傳遞給 data
參數,則它必須正是用於擬合模型對象的數據。將數據集傳遞給 newdata
以擴充模型擬合期間未使用的數據。這仍然要求至少存在用於擬合模型的所有預測變量列。如果用於擬合模型的原始結果變量未包含在 newdata
中,則輸出中不會包含 .resid
列。
根據是否給出 data
或 newdata
,增強的行為通常會有所不同。這是因為通常存在與訓練觀察(例如影響或相關)測量相關的信息,而這些信息對於新觀察沒有有意義的定義。
為了方便起見,許多增強方法提供默認的 data
參數,以便 augment(fit)
將返回增強的訓練數據。在這些情況下,augment 嘗試根據模型對象重建原始數據,並取得了不同程度的成功。
增強數據集始終以 tibble::tibble 形式返回,其行數與傳遞的數據集相同。這意味著傳遞的數據必須可強製轉換為 tibble。如果預測變量將模型作為協變量矩陣的一部分輸入,例如當模型公式使用 splines::ns()
、 stats::poly()
或 survival::Surv()
時,它會表示為矩陣列。
我們正在定義適合各種 na.action
參數的模型的行為,但目前不保證數據丟失時的行為。
參數
- x
-
通過調用
AER::ivreg()
創建的ivreg
對象。 - data
-
base::data.frame 或
tibble::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
進行。這裏有兩個異常:
也可以看看
其他 ivreg 整理器:glance.ivreg()
、tidy.ivreg()
例子
# 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 broom augment.betamfx 使用來自 betamfx 對象的信息增強數據
- R broom augment.robustbase.glmrob 使用來自 glmrob 對象的信息增強數據
- R broom augment.rlm 使用來自 rlm 對象的信息增強數據
- R broom augment.htest 使用來自(n)個 htest 對象的信息來增強數據
- R broom augment.clm 使用來自 clm 對象的信息增強數據
- R broom augment.speedlm 使用來自 speedlm 對象的信息增強數據
- R broom augment.felm 使用來自 (n) 個 felm 對象的信息來增強數據
- R broom augment.smooth.spline 整理一個(n)smooth.spline對象
- R broom augment.drc 使用來自 a(n) drc 對象的信息增強數據
- R broom augment.decomposed.ts 使用來自 decomposed.ts 對象的信息增強數據
- R broom augment.poLCA 使用來自 poLCA 對象的信息增強數據
- R broom augment.lm 使用來自 (n) lm 對象的信息增強數據
- R broom augment.rqs 使用來自 (n) 個 rqs 對象的信息來增強數據
- R broom augment.polr 使用來自 (n) 個 polr 對象的信息增強數據
- R broom augment.plm 使用來自 plm 對象的信息增強數據
- R broom augment.nls 使用來自 nls 對象的信息增強數據
- R broom augment.gam 使用來自 gam 對象的信息增強數據
- R broom augment.fixest 使用來自(n)個最固定對象的信息來增強數據
- R broom augment.survreg 使用來自 survreg 對象的信息增強數據
- R broom augment.rq 使用來自 a(n) rq 對象的信息增強數據
- R broom augment.Mclust 使用來自 Mclust 對象的信息增強數據
- R broom augment.nlrq 整理 a(n) nlrq 對象
- R broom augment.robustbase.lmrob 使用來自 lmrob 對象的信息增強數據
- R broom augment.lmRob 使用來自 lmRob 對象的信息增強數據
- R broom augment.mlogit 使用來自 mlogit 對象的信息增強數據
注:本文由純淨天空篩選整理自等大神的英文原創作品 Augment data with information from a(n) ivreg object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。