# Update to Graphing Non-Proportional Hazards in R

**Christopher Gandrud (간드루드 크리스토파)**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Update 1 February 2013:** I’ve moved all of the functionality described in this post into an R package called simtvc. Have a look. It is much easier to use.

This is a quick update for a previous post on Graphing Non-Proportional Hazards in R.

In the previous post I showed how to simulate and graph 1,000 non-proportional hazard ratios at roughly every point in time across an observation period. In the previous example I kept in simulation outliers. Some people have suggested dropping the top and bottom 2.5 percent of simulated values (i.e. keeping the middle 95 percent).

Luckily this can be accomplished with Hadley Wickham‘s plyr package and three lines of code. The trick is to use plyr’s `ddply`

command to subset the data frame at each point in `Time`

where we simulated values. In the previous example the simulated values were in a variable called `HRqmv`

. In each subset we use the `quantile`

command from `base`

R to create logical variables indicating if a simulation of `HRqmv`

is greater than the 0.975 or less than the 0.025 quantile. Then we simply `subset`

the data frame.

Here’s how you do it: after all of the values have been simulated and the data set ordered and right before graphing the results add this code:

# Indicate bottom 2.5% of observations TVSimPerc <- ddply(TVSim, .(Time), transform, Lower = HRqmv < quantile(HRqmv, c(0.025))) # Indicate top 2.5% of observations TVSimPerc <- ddply(TVSimPerc, .(Time), transform, Upper = HRqmv > quantile(HRqmv, c(0.975))) # Drop simulations outside of the middle 95% TVSimPerc <- subset(TVSimPerc, Lower == FALSE & Upper == FALSE)

Adding this code to the previous example creates this graph:

Here is the full code to replicate the example:

**leave a comment**for the author, please follow the link and comment on their blog:

**Christopher Gandrud (간드루드 크리스토파)**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.