简体   繁体   中英

How to find x value for given y value in R plot

I stumbled upon this question and wanted to do something similar on my plot (for exponential and weibull function).

I want to extract the x value for a specific y value. for me, the Y value would be 0.5.

here is my reproducible example:

library(survival)
attach(stanford2) 
km = survfit(Surv(time, status)~ 1)

wykładniczy <- survreg(Surv(time, status) ~ 1, dist = 'exponential')
lambda_wyk <- exp(-wykładniczy$coef)
weibulla <- survreg(Surv(time, status) ~ 1, dist = 'weibull')
lambda_wei <- exp(-weibulla$icoef[1])
kappa_wei <- 1/weibulla$icoef[2]


plot(km, col = 'black', lwd = 2, conf.int = FALSE) 


x = seq(0, max(time), length = 10000) 
lines(x, exp(-lambda_wyk*x), col = 'red', lwd = 2)
lines(x, exp(-(lambda_wei*x)^kappa_wei), col = 'blue', lwd = 2)
grid()
legend('topright', c('Kaplan-Meier', 'Wykladniczy', 
                     'Weibull'), col = c('black', 
                                         'red', 'blue'), lwd = 2)

I'm using the stanford2 data from the survival package.

I tried to do this like this.

x[which(exp(-(lambda_wei*x)^kappa_wei) == 0.5)])

But I'm missing something.

Explanation what should I do would be great.

I'm not sure if this is exactly what you are looking for, but maybe it helps you find the final solution:

y = exp(-lambda_wyk*x)
df = data.frame(x, y)
# I convert the column to character to avoid the issue explained in the link below
df$x = lapply(df$x, as.character)
df$y = lapply(df$y, as.character)
# Find x value for y = 0.0188018801880188
df[df$x == '0.0188018801880188',]

Output:

                   x                 y
2 0.0188018801880188 0.999815684829423

Why are these numbers not equal?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM