R/plot.parfm.R

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
################################################################################
#  Plot of HRs' with confidence intervals                                      #
################################################################################
#                                                                              #
#  Plots the hazard ratios' estimated values and confidence intervals for      #
#  parametric frailty models                                                   #
#                                                                              #
#  Its parameters are                                                          #
#   - x        : the frailty model, object of class 'predict.parfm'            #
#   - xlim     : xlim (see pars())                                             #
#   - main     : main (see pars())                                             #
#   - coef     : the name of the covariates whose coefficients must be plotted #
#   - signcol  : Should non-significant coefficients be plotted in grey?       #
#   - cex      : cex (see pars())                                              #
#                                                                              #
#   Date: February 21, 2012                                                    #
#   Last modification on: October 17, 2012                                     #
################################################################################

plot.parfm <- function(x, 
                       xlim=NULL,
                       main=NULL,
                       coef=NULL,
                       signcol=TRUE,
                       cex=1,
                       ...){
  if (is.null(coef)) {
    n <- sum(!is.na(x[, "p-val"]))
    coef <- rownames(x)[nrow(x) + 1 - n:1]
  } else
    n <- length(coef)
  coef <- coef[n:1]
  
  intervals <- cbind(exp(x[coef, "ESTIMATE"]),
                     ci.parfm(x)[coef, , drop=FALSE])
  
  if (is.null(xlim)) {
    range <- c(0, 
               max(max(intervals[is.finite(intervals)], na.rm=TRUE) * 1.2, 2))
    if (!is.finite(range[2])) 
      range <- c(0, 100)
  } else {
    range <- xlim
  }
  
  if (is.null(main)) {
    frailty <- attr(x, "frailty")
    dist <- attr(x, "dist")
    main <- paste(c(none="Cox ", 
                    paste(c("Gamma", "Inverse Gaussian", "Positive stable",
                            "Lognormal"),
                  "frailty "))[which(c("none", "gamma", "ingau", "possta",
                                       "lognormal") == frailty)], 
                  "model\nwith ", 
                  dist, " baseline", sep="", collapse="")
  }
  
  if (signcol) {
    color <- apply(intervals[, c("low", "up"), drop=FALSE], 1, 
                   function(int) {
                     if (is.na(int[1]) | is.na(int[2]) | (int[1]<1 & int[2]>1))  
                       "grey" else "black"
                   })
  } else 
    color <- "black"
  
  par(mar=c(5, 5, 4, 2) + .1)
  
  plot(as.vector(intervals), rep(1:n, 3),
       xlim=range,
       ylim=c(.2, n + .8),
       bty="]",
       xlab="HR", 
       ylab="", 
       yaxt="n",
       main=main,
       pch= c(rep(20, n), rep(91, n), rep(93, n)),
       cex=cex,
       col=color)
  
  abline(h=1:n, v=0:1, 
         lty=c(rep(3, n), 1, 2), 
         col=c(rep("grey", n), rep("black", 2)))
  
  intervals[!is.finite(intervals[,3]),3] <- 
    intervals[!is.finite(intervals[,3]),1] + 10^10
  
  segments(intervals[, 2], 1:n, intervals[, 3], 1:n, col=color)
  
  mtext(substr(dimnames(intervals)[[1]], 1, 10),
        side=2,
        at=1:n,
        las=1,
        cex=.7,
        col=color)	
}

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.