[R] survplot invert number at risk labels

Marco Barbàra jabbba at gmail.com
Mon Jul 7 18:12:47 CEST 2014

Hi all,

and thank you for your interested answers. The problem could be
already solved. It is present on my old R 2.15.1
(x86_64-pc-linux-gnu), but after reproducing the error I got the
expected result on a R 3.0.2 through a web interface. However, since I
spent a considerable amount of time to understand what was happening,
I nevertheless decided to submit to your attention what i found.

The following code reproduces the problem on my system (and probably
on every Debian 7 "Wheezy" system with no cran repositories added)

Hazards <- rep(c(1/3, 1/10), each=50)
FupTimes <- rexp(100, rate=Hazards)
CensTimes <- rexp(100, rate=Hazards) # just for fun, we don't need
censoring here
Status <- ifelse(FupTimes <= CensTimes, 1, 0)
FupTimes <- ifelse(Status==1, FupTimes, CensTimes)
SurvObj <- Surv(FupTimes, Status)

# let's manually order the levels:
Covariate <- factor(rep(c("A","B"), each=50), labels=c("B", "A"))
SurvfitObj <- survfit(SurvObj ~ Covariate)
survplot(SurvfitObj, conf="none", n.risk=TRUE, levels.only=TRUE, time.inc=1)

(Resulting plot has nrisk labels inverted)

The error was much simpler than I initially thought, and it is
actually unrelated to survfit. I think that
survplot relies (on my R) on the factor's levels being alphabetically
ordered with the underlying numerical code appropriately arranged
(which I believe is the default behavior of factor() ). There is no
problem if the covariate is a character vector, since in that case the
survfit$strata names have the expected order.

Incidentally, debugging this little problem, I also found that if the
internal nrisk vector has length 1 the survplot function gives error
(line 244 in my version), and this is why I put time.inc=1 in the
previous call (This other issue is still present in 3.0.2)

I'm going to upgrade my R and check that this little error is actually gone.

Thank you

2014-07-03 0:17 GMT+02:00 Frank Harrell <f.harrell at vanderbilt.edu>:
> Please try hard to create a simple reproducible example, then I'll work
> on it.
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list