rms icon indicating copy to clipboard operation
rms copied to clipboard

strata.coefs option for printing lrm doesn't work

Open romantseg opened this issue 9 years ago • 4 comments

I was trying to implement support for rendering objects from rms in markdown using pander and while printing lrm in different ways, I realized that setting strata.coefs causes and error. Taking example from ?lrm

> n <- 1000    # define sample size
> set.seed(17) # so can reproduce the results
> age            <- rnorm(n, 50, 10)
> blood.pressure <- rnorm(n, 120, 15)
> cholesterol    <- rnorm(n, 200, 25)
> sex            <- factor(sample(c('female','male'), n,TRUE))
> label(age)            <- 'Age'      # label is in Hmisc
> label(cholesterol)    <- 'Total Cholesterol'
> label(blood.pressure) <- 'Systolic Blood Pressure'
> label(sex)            <- 'Sex'
> units(cholesterol)    <- 'mg/dl'   # uses units.default in Hmisc
> units(blood.pressure) <- 'mmHg'
> f <- lrm(ch ~ age)
> print(f, strata.coefs = TRUE)
Error in print.lrm(f, strata.coefs = TRUE) : 
  attempt to apply non-function

Looking at code of rms:::print.lrm, it seems that there is an error here:

    if (strata.coefs) {
        cof <- c(cof, x$strata.coef)
        vv <- c(vv, x$Varcov(x, which = "strata.var.diag"))
        if (length(pm)) 
            penalty.scale <- c(penalty.scale, rep(NA, x$nstrata - 
                                                      1))
    }

lrm objects don't have Varcov field and moreover, since lrm object is not an S4 object, I don't think field can be a function.

romantseg avatar Jul 30 '15 23:07 romantseg

I'm wondering about this whole approach. The software underlying pander and pandoc are quite limited in their table formatting and you are having to modify a lot of code from the rms package that will break if I modify rms. If you are able to install the TeX4ht software package on your system you can use rms package latex methods to create advanced LaTeX code then convert it to self-contained html. I am working with the creator of knitr to get this implemented with the Hmisc html method. Then no new programming would be required to use html(latex(fit object)) in knitr with Rmarkdown in RStudio.

But thanks for pointing out the error. I have removed x$ in front of Varcov for the next release.

harrelfe avatar Jul 31 '15 12:07 harrelfe

Thank you very much!

About the approach: although pandoc and markdown are indeed quite limited in what the syntax offers for tables, but this limited syntax supports seamless conversion not only to LaTeX or HTML, but to docx, odt and a bunch of other document formats as well -- which is a huge advantage in some use cases.

Also, if working with Pandoc.brew, you do not have to think about if calling xtable, latex, html, stargazer, broom, kable, print or any other function is required -- all R objects are automatically transformed to the appropriate markdown format, so that the author can concentrate on writing the article and R code, not trying to figure out how to represent an R object in LaTeX, HTML etc.

Disclaimer: I am the original author of pander, so thus my opinion is somewhat skewed :)

daroczig avatar Jul 31 '15 16:07 daroczig

Just checked aganist latest commit and still getting an error

> print(f, strata.coefs = T)
Error in print.lrm(f, strata.coefs = T) : 
  could not find function "Varcov"

romantseg avatar Jul 31 '15 23:07 romantseg

Sorry about that. That should have been vcov. I've posted a fix although the use of strata is still not fully implemented for lrm.

harrelfe avatar Aug 01 '15 12:08 harrelfe