Get startedGet started for free

Visualizing Weibull models

1. Visualising Weibull models

In this video, we will discuss the steps we need to take in order to visualize a Weibull model with covariates. It is important to be able to visualize your results in order to communicate them. In the last video and in the exercise we already prepared for this task.

2. Figure

In the last video, we saw this visualization. We see 6 survival curves for different tumor sizes and different treatments. We will now discuss how to create this figure.

3. Steps to produce visualization

There are 5 steps we need to take: Step 1 is obviously to compute the model including the covariates - in this case, the tumor size and the hormonal therapy. Step 2 is to decide on the covariate combinations for which to compute the survival curves. In this figure, we have an estimated survival curve for an imaginary patient receiving the therapy and tumor size 20, one for a patient receiving therapy and tumor size 25, and so on. Step 3 is to compute the survival curves for these imaginary patients. In step 4, we need to reformat the survival curves so that they can be used for plotting. We need a data frame where each line is one time-point for one patient. When we have that we can finally plot our results using the survminer package. Let's go through these steps 1 by 1.

4. Step 1 and 2

You already know how to specify a Weibull model. So the first step is already clear. What I call "imaginary patients" essentially means you have to decide for which covariate combinations you want to compute the survival curves. In this case, we want all combinations of hormonal therapy and the 25, 50 and 75% quantile of tumor size. The data frame with this information can be easily generated using the expand-dot-grid function.

5. Step 3

We can now compute the survival curve just as we are used to. The only difference is that we enter the data frame we just generated in the newdata argument in the predict function. For each row in the newdata we get a row in the result. For each quantile, meaning each element of surv, we get a column. This is why we have 6 rows and 99 columns. In this format we cannot use the survival curves for plotting, so we need to reshape it.

6. Step 4

Before we use the melt function from the reshape package, we add the information of the "imaginary patients" by column-binding it to the survival functions. Now we need the function melt from the reshape2 package. This function transforms data from wide to long format. We need to specify the data to be melted, the variables, which should stay the same (in this case its the variables defining the patient horTh and tsize), the variable name from the column names, which we want to call surv_id since it indicates which value of surv is specified, and finally the variable name of the value, which in our case is the time. The data frame needs to have a column called surv, which we can generate using our surv object and the variable surv_id which was created in the melting process. Finally, we need to add some variables to the data frame which are required by the plotting function, but not necessary for us, which is why we give them the value NA.

7. Step 4

The result is a data.frame with these 9 variables shown here. We can now pass this on to the plotting function.

8. If this was too fast...

If the melting step was too fast because you have never worked with the function before I recommend looking at a melt function tutorial or the melt function documentation (using ?melt) before you move on to the exercises.

9. Step 5

Now with the prepared data.frame we can easily use the ggsurvplot_df function. We need to set surv-dot-geom to geom_line because the model outputs a smooth survival curve (for step functions such as in the Kaplan-Meier estimate we used geom_step, which is the default setting). This time, we need to specify the linetype and color according to the covariates in our model. The legend-dot-title needs to be set to NULL in order to get this beautiful plot we have been working so hard to get.

10. You can do it too! All you need is practice.

These were the steps that need to be taken in order to plot the results of a Weibull model. In the exercises, we will practice what you have learned.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.