tidymodels / broom

Convert statistical analysis objects from R into tidy format
https://broom.tidymodels.org
Other
1.46k stars 304 forks source link

tidier does not work properly for `psm` objects #747

Closed IndrajeetPatil closed 3 years ago

IndrajeetPatil commented 5 years ago

# setup
set.seed(123)
library(rms)
#> Loading required package: Hmisc
#> Loading required package: lattice
#> Loading required package: survival
#> Loading required package: Formula
#> Loading required package: ggplot2
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
#> Loading required package: SparseM
#> 
#> Attaching package: 'SparseM'
#> The following object is masked from 'package:base':
#> 
#>     backsolve

# data
n <- 400
set.seed(1)
age <- rnorm(n, 50, 12)
sex <- factor(sample(c("Female", "Male"), n, TRUE))
h <- .02 * exp(.06 * (age - 50) + .8 * (sex == "Female"))
d.time <- -log(runif(n)) / h
cens <- 15 * runif(n)
death <- ifelse(d.time <= cens, 1, 0)
d.time <- pmin(d.time, cens)
dat <-
  data.frame(d.time, death, sex, age, stringsAsFactors = FALSE)

# model
m1 <-
  psm(Surv(d.time, death) ~ sex * pol(age, 2),
      dist = "lognormal",
      data = dat
  )

# class
class(m1)
#> [1] "psm"     "rms"     "survreg"

# details
m1
#> Parametric Survival Model: Log Normal Distribution
#>  
#>  psm(formula = Surv(d.time, death) ~ sex * pol(age, 2), data = dat, 
#>      dist = "lognormal")
#>  
#>                     Model Likelihood     Discrimination    
#>                        Ratio Test           Indexes        
#>  Obs        400    LR chi2      37.75    R2       0.107    
#>  Events      83    d.f.             5    Dxy      0.362    
#>  sigma 1.635998    Pr(> chi2) <0.0001    g        0.894    
#>                                          gr       2.446    
#>  
#>                   Coef    S.E.   Wald Z Pr(>|Z|)
#>  (Intercept)       6.0037 2.4924  2.41  0.0160  
#>  sex=Male          5.6166 4.9552  1.13  0.2570  
#>  age              -0.0659 0.0933 -0.71  0.4800  
#>  age^2             0.0001 0.0009  0.12  0.9056  
#>  sex=Male * age   -0.1970 0.1842 -1.07  0.2850  
#>  sex=Male * age^2  0.0019 0.0017  1.15  0.2499  
#>  Log(scale)        0.4923 0.0839  5.87  <0.0001 
#> 

# tidier?
broom::tidy(m1)
#> Error in summary.rms(x): adjustment values not defined here or with datadist for sex age

# glance
broom::glance(m1)
#> # A tibble: 1 x 9
#>    iter    df statistic logLik   AIC   BIC df.residual  nobs     p.value
#>   <int> <int>     <dbl>  <dbl> <dbl> <dbl>       <int> <dbl>       <dbl>
#> 1     5     7      37.8  -347.  709.  737.         393   400 0.000000423

Created on 2019-08-17 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R version 3.6.1 (2019-07-05) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz America/New_York #> date 2019-08-17 #> #> - Packages -------------------------------------------------------------- #> package * version date lib #> acepack 1.4.1 2016-10-29 [1] #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.1.4 2019-04-10 [1] #> base64enc 0.1-3 2015-07-28 [1] #> broom 0.5.2.9002 2019-08-15 [1] #> callr 3.3.1 2019-07-18 [1] #> checkmate 1.9.4 2019-07-04 [1] #> cli 1.1.0 2019-03-19 [1] #> cluster 2.1.0 2019-06-19 [2] #> codetools 0.2-16 2018-12-24 [1] #> colorspace 1.4-1 2019-03-18 [1] #> crayon 1.3.4 2017-09-16 [1] #> data.table 1.12.2 2019-04-07 [1] #> desc 1.2.0 2019-04-03 [1] #> devtools 2.1.0 2019-07-06 [1] #> digest 0.6.20 2019-07-04 [1] #> dplyr 0.8.3 2019-07-04 [1] #> evaluate 0.14 2019-05-28 [1] #> fansi 0.4.0 2018-11-05 [1] #> foreign 0.8-71 2018-07-20 [2] #> Formula * 1.2-3 2018-05-03 [1] #> fs 1.3.1 2019-05-06 [1] #> generics 0.0.2 2019-03-05 [1] #> ggplot2 * 3.2.1 2019-08-10 [1] #> glue 1.3.1 2019-03-12 [1] #> gridExtra 2.3 2017-09-09 [1] #> gtable 0.3.0 2019-03-25 [1] #> highr 0.8 2019-03-20 [1] #> Hmisc * 4.2-0 2019-01-26 [1] #> htmlTable 1.13.1 2019-01-07 [1] #> htmltools 0.3.6 2017-04-28 [1] #> htmlwidgets 1.3 2018-09-30 [1] #> knitr 1.24 2019-08-08 [1] #> lattice * 0.20-38 2018-11-04 [2] #> latticeExtra 0.6-28 2016-02-09 [1] #> lazyeval 0.2.2 2019-03-15 [1] #> lifecycle 0.1.0 2019-08-01 [1] #> magrittr 1.5 2014-11-22 [1] #> MASS 7.3-51.4 2019-03-31 [1] #> Matrix 1.2-17 2019-03-22 [1] #> MatrixModels 0.4-1 2015-08-22 [1] #> memoise 1.1.0 2017-04-21 [1] #> multcomp 1.4-10 2019-03-05 [1] #> munsell 0.5.0 2018-06-12 [1] #> mvtnorm 1.0-11 2019-06-19 [1] #> nlme 3.1-140 2019-05-12 [2] #> nnet 7.3-12 2016-02-02 [2] #> pillar 1.4.2 2019-06-29 [1] #> pkgbuild 1.0.4 2019-08-05 [1] #> pkgconfig 2.0.2 2018-08-16 [1] #> pkgload 1.0.2 2018-10-29 [1] #> polspline 1.1.15 2019-06-13 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.4.1 2019-07-18 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr 0.3.2 2019-03-15 [1] #> quantreg 5.51 2019-08-07 [1] #> R6 2.4.0 2019-02-14 [1] #> RColorBrewer 1.1-2 2014-12-07 [1] #> Rcpp 1.0.2 2019-07-25 [1] #> remotes 2.1.0 2019-06-24 [1] #> rlang 0.4.0 2019-06-25 [1] #> rmarkdown 1.14.3 2019-08-02 [1] #> rms * 5.1-3.1 2019-04-22 [1] #> rpart 4.1-15 2019-04-12 [2] #> rprojroot 1.3-2 2018-01-03 [1] #> rstudioapi 0.10 2019-03-19 [1] #> sandwich 2.5-1 2019-04-06 [1] #> scales 1.0.0 2018-08-09 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> SparseM * 1.77 2017-04-23 [1] #> stringi 1.4.3 2019-03-12 [1] #> stringr 1.4.0 2019-02-10 [1] #> survival * 2.44-1.1 2019-04-01 [2] #> testthat 2.2.1 2019-07-25 [1] #> TH.data 1.0-10 2019-01-21 [1] #> tibble 2.1.3 2019-06-06 [1] #> tidyr 0.8.99.9000 2019-08-11 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> usethis 1.5.1.9000 2019-07-27 [1] #> utf8 1.1.4 2018-05-24 [1] #> vctrs 0.2.0 2019-07-05 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.8 2019-06-25 [1] #> yaml 2.2.0 2018-07-25 [1] #> zeallot 0.1.0 2018-01-28 [1] #> zoo 1.8-6 2019-05-28 [1] #> source #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.0) #> local #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.5.2) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.3) #> Github (r-lib/desc@c860e7b) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (brodieG/fansi@ab11e9c) #> CRAN (R 3.6.1) #> CRAN (R 3.5.0) #> CRAN (R 3.6.0) #> Github (r-lib/generics@c15ac43) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.1) #> CRAN (R 3.6.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.2) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (rstudio/rmarkdown@37bcb8f) #> CRAN (R 3.6.1) #> CRAN (R 3.6.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.1) #> CRAN (R 3.5.2) #> CRAN (R 3.6.0) #> Github (tidyverse/tidyr@908da2c) #> CRAN (R 3.5.1) #> Github (r-lib/usethis@b241420) #> CRAN (R 3.5.1) #> CRAN (R 3.6.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> CRAN (R 3.5.1) #> CRAN (R 3.5.1) #> CRAN (R 3.6.0) #> #> [1] C:/Users/inp099/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-3.6.1/library ```
alexpghayes commented 5 years ago

Might have a solution in #326. In the short term might be good to short circuit and give an informative error for rms objects with tidy.rms <- tidy.default. Long term solution is a tidy.psm() method. Fair warning to anybody taking that task on: working with rms objects can be frustrating.

github-actions[bot] commented 3 years ago

This issue has been automatically closed due to inactivity.

github-actions[bot] commented 3 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.