Skip to content

augment.clm() does not respect type.predict argument #617

@IndrajeetPatil

Description

@IndrajeetPatil

A couple of issues here-

# setup
set.seed(123)
library(tidyverse, warn.conflicts = FALSE)
#> Registered S3 method overwritten by 'rvest':
#>   method            from
#>   read_xml.response xml2
library(ordinal)
#> 
#> Attaching package: 'ordinal'
#> The following object is masked from 'package:dplyr':
#> 
#>     slice
library(MASS)
#> 
#> Attaching package: 'MASS'
#> The following object is masked from 'package:dplyr':
#> 
#>     select
  1. Irrespective of what type.predict is specified, the augment method always returns predicted class probabilties.
# model
clm_mod <- clm(rating ~ temp * contact, data = wine)

# predicting class labels with `stats` (works as expected)
stats::predict(clm_mod, newdata = wine, type = "class") %>%
  tibble::as_tibble(.)
#> # A tibble: 72 x 1
#>    fit  
#>    <fct>
#>  1 2    
#>  2 2    
#>  3 3    
#>  4 3    
#>  5 3    
#>  6 3    
#>  7 4    
#>  8 4    
#>  9 2    
#> 10 2    
#> # ... with 62 more rows

stats::predict(clm_mod, newdata = wine, type = "prob") %>%
  tibble::as_tibble(.)
#> # A tibble: 72 x 1
#>       fit
#>     <dbl>
#>  1 0.562 
#>  2 0.209 
#>  3 0.435 
#>  4 0.0894
#>  5 0.190 
#>  6 0.190 
#>  7 0.286 
#>  8 0.286 
#>  9 0.196 
#> 10 0.562 
#> # ... with 62 more rows

# predicting class labels with `broom`
broom::augment(clm_mod, newdata = wine, type.predict = "class")
#> # A tibble: 72 x 8
#>    response rating temp  contact bottle judge .fitted .se.fit
#>       <dbl> <ord>  <fct> <fct>   <fct>  <fct>   <dbl>   <dbl>
#>  1       36 2      cold  no      1      1      0.562   0.0885
#>  2       48 3      cold  no      2      1      0.209   0.0788
#>  3       47 3      cold  yes     3      1      0.435   0.0837
#>  4       67 4      cold  yes     4      1      0.0894  0.0436
#>  5       77 4      warm  no      5      1      0.190   0.0711
#>  6       60 4      warm  no      6      1      0.190   0.0711
#>  7       83 5      warm  yes     7      1      0.286   0.0993
#>  8       90 5      warm  yes     8      1      0.286   0.0993
#>  9       17 1      cold  no      1      2      0.196   0.0860
#> 10       22 2      cold  no      2      2      0.562   0.0885
#> # ... with 62 more rows

broom::augment(clm_mod, newdata = wine, type.predict = "prob")
#> # A tibble: 72 x 8
#>    response rating temp  contact bottle judge .fitted .se.fit
#>       <dbl> <ord>  <fct> <fct>   <fct>  <fct>   <dbl>   <dbl>
#>  1       36 2      cold  no      1      1      0.562   0.0885
#>  2       48 3      cold  no      2      1      0.209   0.0788
#>  3       47 3      cold  yes     3      1      0.435   0.0837
#>  4       67 4      cold  yes     4      1      0.0894  0.0436
#>  5       77 4      warm  no      5      1      0.190   0.0711
#>  6       60 4      warm  no      6      1      0.190   0.0711
#>  7       83 5      warm  yes     7      1      0.286   0.0993
#>  8       90 5      warm  yes     8      1      0.286   0.0993
#>  9       17 1      cold  no      1      2      0.196   0.0860
#> 10       22 2      cold  no      2      2      0.562   0.0885
#> # ... with 62 more rows
  1. In addition to 1, the results for polr objects are different from the source method even when type of prediction is probs.
# model
polr_mod <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing)

# predicting class labels with `stats` (works as expected)
stats::predict(polr_mod, newdata = housing, type = "probs") %>%
  tibble::as_tibble(.)
#> # A tibble: 72 x 3
#>      Low Medium  High
#>    <dbl>  <dbl> <dbl>
#>  1 0.378  0.288 0.334
#>  2 0.378  0.288 0.334
#>  3 0.378  0.288 0.334
#>  4 0.257  0.274 0.469
#>  5 0.257  0.274 0.469
#>  6 0.257  0.274 0.469
#>  7 0.144  0.211 0.645
#>  8 0.144  0.211 0.645
#>  9 0.144  0.211 0.645
#> 10 0.519  0.261 0.220
#> # ... with 62 more rows

stats::predict(polr_mod, newdata = housing, type = "class") %>%
  tibble::as_tibble(.)
#> Warning: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `enframe(name = NULL)` instead.
#> This warning is displayed once per session.
#> # A tibble: 72 x 1
#>    value
#>    <fct>
#>  1 Low  
#>  2 Low  
#>  3 Low  
#>  4 High 
#>  5 High 
#>  6 High 
#>  7 High 
#>  8 High 
#>  9 High 
#> 10 Low  
#> # ... with 62 more rows

# predicting class labels with `broom`
broom::augment(polr_mod, type.predict = "probs")
#> # A tibble: 72 x 6
#>    Sat    Infl   Type      Cont  X.weights. .fitted
#>    <ord>  <fct>  <fct>     <fct>      <int>   <dbl>
#>  1 Low    Low    Tower     Low           21       1
#>  2 Medium Low    Tower     Low           21       1
#>  3 High   Low    Tower     Low           28       1
#>  4 Low    Medium Tower     Low           34       3
#>  5 Medium Medium Tower     Low           22       3
#>  6 High   Medium Tower     Low           36       3
#>  7 Low    High   Tower     Low           10       3
#>  8 Medium High   Tower     Low           11       3
#>  9 High   High   Tower     Low           36       3
#> 10 Low    Low    Apartment Low           61       1
#> # ... with 62 more rows

broom::augment(polr_mod, type.predict = "class")
#> # A tibble: 72 x 6
#>    Sat    Infl   Type      Cont  X.weights. .fitted
#>    <ord>  <fct>  <fct>     <fct>      <int>   <dbl>
#>  1 Low    Low    Tower     Low           21       1
#>  2 Medium Low    Tower     Low           21       1
#>  3 High   Low    Tower     Low           28       1
#>  4 Low    Medium Tower     Low           34       3
#>  5 Medium Medium Tower     Low           22       3
#>  6 High   Medium Tower     Low           36       3
#>  7 Low    High   Tower     Low           10       3
#>  8 Medium High   Tower     Low           11       3
#>  9 High   High   Tower     Low           36       3
#> 10 Low    Low    Apartment Low           61       1
#> # ... with 62 more rows

Created on 2019-03-03 by the reprex package (v0.2.1.9000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    beginner-friendlybugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions