當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R broom augment.polr 使用來自 (n) 個 polr 對象的信息增強數據

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 polr
augment(
  x,
  data = model.frame(x),
  newdata = NULL,
  type.predict = c("class"),
  ...
)

參數

x

MASS::polr() 返回的 polr 對象。

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 參數將被忽略。

type.predict

要計算哪種類型的預測,傳遞給 MASS:::predict.polr() 。目前僅支持"class"

...

附加參數。不曾用過。僅需要匹配通用簽名。注意:拚寫錯誤的參數將被吸收到 ... 中,並被忽略。如果拚寫錯誤的參數有默認值,則將使用默認值。例如,如果您傳遞 conf.lvel = 0.9 ,所有計算將使用 conf.level = 0.95 進行。這裏有兩個異常:

  • tidy() 方法在提供 exponentiate 參數時會發出警告(如果該參數將被忽略)。

  • augment() 方法在提供 newdata 參數時會發出警告(如果該參數將被忽略)。

也可以看看

例子


# load libraries for models and data
library(MASS)

# fit model
fit <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing)

# summarize model fit with tidiers
tidy(fit, exponentiate = TRUE, conf.int = TRUE)
#> 
#> Re-fitting to get Hessian
#> # A tibble: 8 × 7
#>   term          estimate std.error statistic conf.low conf.high coef.type 
#>   <chr>            <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>     
#> 1 InflMedium       1.76     0.105       5.41    1.44      2.16  coefficie…
#> 2 InflHigh         3.63     0.127      10.1     2.83      4.66  coefficie…
#> 3 TypeApartment    0.564    0.119      -4.80    0.446     0.712 coefficie…
#> 4 TypeAtrium       0.693    0.155      -2.36    0.511     0.940 coefficie…
#> 5 TypeTerrace      0.336    0.151      -7.20    0.249     0.451 coefficie…
#> 6 ContHigh         1.43     0.0955      3.77    1.19      1.73  coefficie…
#> 7 Low|Medium       0.609    0.125      -3.97   NA        NA     scale     
#> 8 Medium|High      2.00     0.125       5.50   NA        NA     scale     

glance(fit)
#> # A tibble: 1 × 7
#>     edf logLik   AIC   BIC deviance df.residual  nobs
#>   <int>  <dbl> <dbl> <dbl>    <dbl>       <int> <int>
#> 1     8 -1740. 3495. 3539.    3479.        1673  1681
augment(fit, type.predict = "class")
#> # A tibble: 72 × 6
#>    Sat    Infl   Type      Cont  `(weights)` .fitted
#>    <ord>  <fct>  <fct>     <fct>       <int> <fct>  
#>  1 Low    Low    Tower     Low            21 Low    
#>  2 Medium Low    Tower     Low            21 Low    
#>  3 High   Low    Tower     Low            28 Low    
#>  4 Low    Medium Tower     Low            34 High   
#>  5 Medium Medium Tower     Low            22 High   
#>  6 High   Medium Tower     Low            36 High   
#>  7 Low    High   Tower     Low            10 High   
#>  8 Medium High   Tower     Low            11 High   
#>  9 High   High   Tower     Low            36 High   
#> 10 Low    Low    Apartment Low            61 Low    
#> # ℹ 62 more rows

fit2 <- polr(factor(gear) ~ am + mpg + qsec, data = mtcars)

tidy(fit, p.values = TRUE)
#> 
#> Re-fitting to get Hessian
#> p-values can presently only be returned for models that contain
#>               no categorical variables with more than two levels
#> # A tibble: 8 × 6
#>   term          estimate std.error statistic p.value coef.type  
#>   <chr>            <dbl>     <dbl>     <dbl> <lgl>   <chr>      
#> 1 InflMedium       0.566    0.105       5.41 NA      coefficient
#> 2 InflHigh         1.29     0.127      10.1  NA      coefficient
#> 3 TypeApartment   -0.572    0.119      -4.80 NA      coefficient
#> 4 TypeAtrium      -0.366    0.155      -2.36 NA      coefficient
#> 5 TypeTerrace     -1.09     0.151      -7.20 NA      coefficient
#> 6 ContHigh         0.360    0.0955      3.77 NA      coefficient
#> 7 Low|Medium      -0.496    0.125      -3.97 NA      scale      
#> 8 Medium|High      0.691    0.125       5.50 NA      scale      

相關用法


注:本文由純淨天空篩選整理自大神的英文原創作品 Augment data with information from a(n) polr object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。