mindhabits
mindhabits

Reputation: 421

Mutating Values within a List

I have the following example code

library(survival)
fit2 <- coxph( Surv(stop, event) ~ size, data = bladder )
# single curve
ggadjustedcurves(fit2, data = bladder)

fit2 <- coxph( Surv(stop, event) ~ size + strata(rx), data = bladder )
# average in groups
ggadjustedcurves(fit2, data = bladder, method = "average", variable = "rx")

a<-ggadjustedcurves(fit2, data = bladder, method = "average", variable = "rx")

This is just a technical plot from a statistical model that plots a "survival curve". You can plot it and see for yourself. I am trying to change the values of the "curves" themselves however. In the object a there contains a list called "surv" (under both data and plot_env). This is where the y-coordinates are coming from (from either one, I am not sure).

I would like to instead perform a simple transformation (1-surv). That is, the beginning value of "1" would be changed to 0 and so forth. This will achieve the effect of having the survival lines go "up" instead of "down". Just an interpretation thing.

How can I achieve this effect? Essentially, what I want to do is go into the list and do "1-surv". There are 76 elements in the list so 38 y-coordinates for variable 1 and 38 for variable 2. I have a weak grasp of programming but I'm sure what I'm asking is easily doable.

Again, to describe my problem, I would like to change the y-coordinates (presumably contained under data or plot_env under the list surv) to mutate them.

Thanks for all your help!

EDIT

I attempted the following:

b<-a$data$surv
a$data$surv<- 1-b
b

This gives me the intended solution. is there a more elegant solution to this? perhaps using dplyr etc.? I realize this is a simple task but would like any pointers on where to look.

Upvotes: 1

Views: 153

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76621

I don't know if this is more elegant but it is more tidy.

library(survival)
library(survminer)

complement <- function(g){
  g$data$surv <- 1 - g$data$surv
  g
}

fit2 <- coxph( Surv(stop, event) ~ size + strata(rx), data = bladder )
# average in groups
a <- ggadjustedcurves(fit2, data = bladder, method = "average", variable = "rx")

complement(a)

Note that the object a was not changed, to do so the function's result will have to be assigned back to it. Or keep the object as it was output by ggadjustedcurves and assign to another object.

b <- complement(a)

Upvotes: 1

Related Questions