Você está na página 1de 13

EXERCÍCIOS DE ANÁLISE DE SOBREVIVÊNCIA

1.
a)
SCRIPT DE ENTRADA NO R
library(survival)
tempos=c(7, 34, 42, 63, 64, 74, 83, 84, 91, 108, 112, 129, 133, 133, 139, 140,140, 146, 149,154, 157, 160, 160,
165, 173, 176, 185, 218, 225, 241, 248, 273,277, 279, 297, 319, 405, 417, 420, 440, 523, 523, 583, 594, 1101,
1116, 1146, 1226, 1349, 1412, 1417)
cens=c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 0, 0, 0, 1)
cbind(tempos, cens)
Surv(tempos, cens)
ekm=survfit(Surv(tempos, cens)~1, conf.type="plain")
print(survfit(Surv(tempos, cens)~1, conf.type="plain") , print.rmean=T)
summary(ekm)
plot(ekm, mark.time = T, main = "Tempos de pacientes submetidos á radioterapia",
xlab = " Tempo", lwd=2, ylim=c(0,1), ylab = "Estimativa de S(t)", lty = c(1, 2, 2), col = c("black", "blue", "blue"))
legend(0, 0.1, c("Kaplan-Meier", "IC(95%)"), lwd=2, lty = c(1, 2, 2), bty="n",
col = c("black", "blue", "blue"))
b)
> print(survfit(Surv(tempos, cens)~1, conf.type="plain") , print.rmean=T)
Call: survfit(formula = Surv(tempos, cens) ~ 1, conf.type = "plain")

n events *rmean *se(rmean) median 0.95LCL 0.95UCL


51 42 422 67 218 154 297
* restricted mean with upper limit = 1417

O tempo médio de vida dos pacientes é de 422 dias e o tempo mediano de vida dos pacientes é
de 218 dias.

c)
SCRIPT DE ENTRADA NO R
o=order(tempos)
t=tempos[o]
cens=cens[o]
n=length(t)
r=sum(cens)
j=1
TF=numeric()
MON=numeric()
I=numeric()
TTT=numeric()
Fi=numeric()
F=numeric()
S=numeric()
TF[j]=0
MON[j]=0
F[j]=0
S[j]=1
TTT[j]=0
i=1
while(i<(n+1)){
if(cens[i]==1){
j=j+1
TF[j]=t[i]
NI=n-i+1
I=((n+1)-MON[j-1])/(1+NI)
MON[j]=MON[j-1]+I
F[j]=MON[j]/n
S[j]=1-F[j]}
i=i+1}
TF[r+2]=t[n]
F[r+2]=1
TTT[1]=0
for(j in 2:(r+2)){
TTT[j]=TTT[j-1]+n*S[j-1]*(TF[j]-TF[j-1])}
for(j in 1:(r+2)){
Fi[j]=TTT[j]/TTT[r+2]}
plot(F,Fi,xlim=c(0,1),ylim=c(0,1),ylab='TTT com censuras',xlab='F(t)',
lwd=2,pch=19,cex.axis=1.4,cex.lab=1.4)
lines(F,Fi,type='l',lwd=1)
x=c(0,1)
y=c(0,1)
lines(x,y,lwd=1)

A função de risco tem a forma unimodal.


d)
SCRIPT DE ENTRADA NO R
library(flexsurv)
ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')
ajust1
ajust2<-flexsurvreg(Surv(tempos,cens)~1, dist='lognorm')
ajust2

> ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')


> ajust1
Call:
flexsurvreg(formula = Surv(tempos, cens) ~ 1, dist = "weibull")
Estimates:
est L95% U95% se
shape 0.930 0.738 1.171 0.109
scale 426.868 307.073 593.398 71.739
N = 51, Events: 42, Censored: 9
Total time at risk: 18250
Log-likelihood = -296.9191, df = 2
AIC = 597.8381

> ajust2<-flexsurvreg(Surv(tempos,cens)~1, dist='lognorm')


> ajust2
Call:
flexsurvreg(formula = Surv(tempos, cens) ~ 1, dist = "lognorm")
Estimates:
est L95% U95% se
meanlog 5.521 5.188 5.855 0.170
sdlog 1.177 0.945 1.466 0.132
N = 51, Events: 42, Censored: 9
Total time at risk: 18250
Log-likelihood = -293.2365, df = 2
AIC = 590.4729
e)
SCRIPT DE ENTRADA NO R
#### ESTIMATIVA DAS FUNÇÕES DE SOBREVIVÊNCIA
ekm<-survfit(Surv(tempos,cens)~1)
time<-ekm$time
st<-ekm$surv # ESTIMATIVA EMPÍRICA PO KM
ste<- exp(-0.0490*time) # EXPONENCIAL
stw<- exp(-(time/21.339)^1.543) # WEIBULL
stln<- pnorm((-log(time)+ 2.717)/0.765) # LOG-NORMAL
cbind(time,st,ste,stw,stln)

###MÉTODOS GRÁFICOS
## 1 - MÉTODO GRÁFICO 1
par(mfrow=c(1,3))
plot(st, ste,pch=16,ylim=range(c(0.0,1)),xlim=range(c(0,1)),xlab="S(t): Kaplan-Meier",ylab="S(t): exponencial")
lines(c(0,1), c(0,1), type="l", lty=1)
plot(st, stw, pch=16, ylim=range(c(0.0,1)), xlim=range(c(0,1)), xlab = "S(t): Kaplan-Meier", ylab="S(t): Weibull")
lines(c(0,1), c(0,1), type="l", lty=1)
plot(st,stln,pch=16,ylim=range(c(0.0,1)),xlim=range(c(0,1)),xlab="S(t): Kaplan-Meier", ylab="S(t): log-normal")
lines(c(0,1), c(0,1), type="l", lty=1)
## 2 - MÉTODO GRÁFICO 2
par(mfrow=c(1,3))
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,ste), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Exponencial"), bty="n", cex=1.0)
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,stw), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Weibull"), bty="n", cex=1.0)
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,stln), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Log-normal"), bty="n", cex=1.0)

## 3 - MÉTODO DA LINEARIZAÇÃO
par(mfrow=c(1,3))
ekm<-survfit(Surv(tempos,cens)~1)
time<-ekm$time
st<-ekm$surv
plot(time, -log(st), pch=16, xlab="tempos", ylab="-log(S(t))")
lines(c(0,1), c(0,1), type="l", lty=1)
plot(log(time), log(-log(st)), pch=16, xlab="log(tempos)", ylab="log(-log(S(t)))")
lines(c(0,1), c(0,1), type="l", lty=1)
invst<-qnorm(st)
plot(log(time), invst, pch=16, xlab="log(tempos)", ylab=expression(Phi^-1*(S(t))))
MÉTODO GRÁFICO 1

MÉTODO GRÁFICO 2
MÉTODO DA LINEARIZAÇÃO
f)
SCRIPT DE ENTRADA NO R
ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')
ajust1
LogGG=ajust1$loglik
LogGG
LogW=ajust1$loglik
LogW
TRVW=2*(LogGG-LogW)
TRVW
pvalue.v2 <- 1-pchisq(TRVW, 1)
pvalue.v2
logL=c(LogW)
logL
TRV=c(TRVW)
TRV
PVALOR=c(pvalue.v2)
PVALOR
tab=cbind(logL, TRV, PVALOR)
rownames(tab) <- c("WEIBULL")
tab
round(tab, 3)
> ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')
> ajust1
Call:
flexsurvreg(formula = Surv(tempos, cens) ~ 1, dist = "weibull")
Estimates:
est L95% U95% se
shape 0.930 0.738 1.171 0.109
scale 426.868 307.073 593.398 71.739
N = 51, Events: 42, Censored: 9
Total time at risk: 18250
Log-likelihood = -296.9191, df = 2
AIC = 597.8381
>
> LogGG=ajust1$loglik
> LogGG
[1] -296.9191
> LogW=ajust1$loglik
> LogW
[1] -296.9191
> TRVW=2*(LogGG-LogW)
> TRVW
[1] 0
> pvalue.v2 <- 1-pchisq(TRVW, 1)
> pvalue.v2
[1] 1
O modelo considerado para o ajuste foi o Weibull, porém os modelos não diferenciam muito
devido ao tamanho pequeno da amostra.
SCRIPT COMPLETO DE ENTRADA NO R
###KAPLAN MEIER

library(survival)

tempos=c(7, 34, 42, 63, 64, 74, 83, 84, 91, 108, 112, 129, 133, 133, 139, 140,140, 146, 149, 154, 157, 160, 160,
165, 173, 176, 185, 218, 225, 241, 248, 273,277, 279, 297, 319, 405, 417, 420, 440, 523, 523, 583, 594, 1101,
1116, 1146, 1226, 1349, 1412, 1417)
cens=c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 0, 0, 0, 1)
cbind(tempos, cens)
Surv(tempos, cens)
ekm=survfit(Surv(tempos, cens)~1, conf.type="plain")
print(survfit(Surv(tempos, cens)~1, conf.type="plain") , print.rmean=T)
summary(ekm)
plot(ekm, mark.time = T, main = "Tempos de pacientes submetidos á radioterapia",
xlab = " Tempo", lwd=2, ylim=c(0,1), ylab = "Estimativa de S(t)", lty = c(1, 2, 2), col = c("black", "blue", "blue"))
legend(0, 0.1, c("Kaplan-Meier", "IC(95%)"), lwd=2, lty = c(1, 2, 2), bty="n",
col = c("black", "blue", "blue"))

###TTT PLOT
o=order(tempos)
t=tempos[o]
cens=cens[o]
n=length(t)
r=sum(cens)
j=1
TF=numeric()
MON=numeric()
I=numeric()
TTT=numeric()
Fi=numeric()
F=numeric()
S=numeric()
TF[j]=0
MON[j]=0
F[j]=0
S[j]=1
TTT[j]=0
i=1
while(i<(n+1)){
if(cens[i]==1){
j=j+1
TF[j]=t[i]
NI=n-i+1
I=((n+1)-MON[j-1])/(1+NI)
MON[j]=MON[j-1]+I
F[j]=MON[j]/n
S[j]=1-F[j]}
i=i+1}
TF[r+2]=t[n]
F[r+2]=1
TTT[1]=0
for(j in 2:(r+2)){
TTT[j]=TTT[j-1]+n*S[j-1]*(TF[j]-TF[j-1])}
for(j in 1:(r+2)){
Fi[j]=TTT[j]/TTT[r+2]}
plot(F,Fi,xlim=c(0,1),ylim=c(0,1),ylab='TTT com censuras',xlab='F(t)',
lwd=2,pch=19,cex.axis=1.4,cex.lab=1.4)
lines(F,Fi,type='l',lwd=1)
x=c(0,1)
y=c(0,1)
lines(x,y,lwd=1)

####AJUSTE PARA DOIS MODELOS

library(flexsurv)
ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')
ajust1
ajust2<-flexsurvreg(Surv(tempos,cens)~1, dist='lognorm')
ajust2

#### ESTIMATIVA DAS FUNÇÕES DE SOBREVIVÊNCIA


ekm<-survfit(Surv(tempos,cens)~1)
time<-ekm$time
st<-ekm$surv # ESTIMATIVA EMPÍRICA PO KM
ste<- exp(-0.0490*time) # EXPONENCIAL
stw<- exp(-(time/21.339)^1.543) # WEIBULL
stln<- pnorm((-log(time)+ 2.717)/0.765) # LOG-NORMAL
cbind(time,st,ste,stw,stln)
###MÉTODOS GRÁFICOS

## 1 - MÉTODO GRÁFICO 1
par(mfrow=c(1,3))
plot(st, ste, pch=16, ylim=range(c(0.0,1)), xlim=range(c(0,1)),
xlab = "S(t): Kaplan-Meier", ylab="S(t): exponencial")
lines(c(0,1), c(0,1), type="l", lty=1)
plot(st, stw, pch=16, ylim=range(c(0.0,1)), xlim=range(c(0,1)),
xlab = "S(t): Kaplan-Meier", ylab="S(t): Weibull")
lines(c(0,1), c(0,1), type="l", lty=1)
plot(st, stln, pch=16, ylim=range(c(0.0,1)), xlim=range(c(0,1)),
xlab = "S(t): Kaplan-Meier", ylab="S(t): log-normal")
lines(c(0,1), c(0,1), type="l", lty=1)

## 2 - MÉTODO GRÁFICO 2

par(mfrow=c(1,3))
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,ste), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Exponencial"), bty="n", cex=1.0)
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,stw), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Weibull"), bty="n", cex=1.0)
plot(ekm, conf.int=F, xlab="Tempos", ylab="S(t)")
lines(c(0,time), c(1,stln), lty=2, col=c(2))
legend(10, 1, lty=c(1,2), c("Kaplan-Meier", "Log-normal"), bty="n", cex=1.0)

## MÉTODO DA LINEARIZAÇÃO
par(mfrow=c(1,3))
ekm<-survfit(Surv(tempos,cens)~1)
time<-ekm$time
st<-ekm$surv

plot(time, -log(st), pch=16, xlab="tempos", ylab="-log(S(t))")


lines(c(0,1), c(0,1), type="l", lty=1)
plot(log(time), log(-log(st)), pch=16, xlab="log(tempos)", ylab="log(-log(S(t)))")
lines(c(0,1), c(0,1), type="l", lty=1)

invst<-qnorm(st)
plot(log(time), invst, pch=16, xlab="log(tempos)", ylab=expression(Phi^-1*(S(t))))
## TRV
ajust1<-flexsurvreg(Surv(tempos,cens)~1, dist='weibull')
ajust1

LogGG=ajust1$loglik
LogGG
LogW=ajust1$loglik
LogW
TRVW=2*(LogGG-LogW)
TRVW
pvalue.v2 <- 1-pchisq(TRVW, 1)
pvalue.v2

logL=c(LogW)
logL
TRV=c(TRVW)
TRV
PVALOR=c(pvalue.v2)
PVALOR
tab=cbind(logL, TRV, PVALOR)

rownames(tab) <- c("WEIBULL")


tab
round(tab, 3)

Você também pode gostar