Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. data(ovarian)
  2. library(survival)
  3.  
  4. ## Fit cox model
  5. fit <- coxph( Surv(futime,fustat)~age + rx,data=ovarian)
  6.  
  7.  
  8. ### Calculate risks and associated confidence interval manually, using non centred baseline hazard and linear predictor
  9.  
  10. ## Extract design matrix
  11. test.model.matrix<-model.matrix(Surv(futime,fustat)~age + rx,data=ovarian)
  12. S<-test.model.matrix
  13. ## Remove intercept from design matrix
  14. S<-S[,-c(1)]
  15.  
  16.  
  17. ## Extract coefficients and covariance matrix
  18. B<-fit$coefficients
  19. C<-fit$var
  20.  
  21. ## Calculate linear predictor
  22. p<-(S%*%B)[,1]
  23. head(p)
  24.  
  25. ## Calculate standard error and CI
  26. std.er.p1<-(S%*%C)
  27.  
  28. std.er.p2<-std.er.p1*S
  29.  
  30. std.er.p2<-data.frame(std.er.p2)
  31. head(std.er.p2)
  32.  
  33. se<-sqrt(rowSums(std.er.p2))
  34.  
  35. ## Calculate the relevant t statistic, degrees of freedom = n-p = 26-2
  36. t<-qt(0.975,24)
  37.  
  38.  
  39. ## Create dataset with linear predictor, upper and lower limit
  40. lp<-data.frame(risk=p,low.lim=p-t*se,upp.lim=p+t*se)
  41.  
  42. ## Now get cumulative baseline hazard using basehaz function, not centred
  43. basehaz<-basehaz(fit,centered=FALSE)
  44.  
  45. ## Extract hazard at relevant time
  46. ## Must be a time at which an event happened, choose time=156 (3rd event)
  47. basehaz156<-basehaz[basehaz$time==156,]$hazard
  48.  
  49. ## Generate survival function
  50. surv<-exp(-basehaz156*exp(lp))
  51.  
  52. ## generate risks
  53. risks.uncent<-1-surv
  54.  
  55.  
  56. ### Now extract the linear predictor using the predict function (centred linear predictor)
  57. ### And use the centred baseline hazard
  58.  
  59. ## Do it using predict function
  60. fit.cent<-predict(fit,se.fit=TRUE,type="lp")
  61.  
  62. ## Extract linear predictor and standard error
  63. p.cent<-fit.pred[[1]]
  64. se.cent<-fit.pred[[2]]
  65.  
  66. ## Calculate the relevant t statistic
  67. t<-qt(0.975,24)
  68.  
  69.  
  70. # Create dataset with linear predictor, upper and lower limit
  71. lp.cent<-data.frame(risk=p.cent,low.lim=p.cent-t*se.cent,upp.lim=p.cent+t*se.cent)
  72.  
  73.  
  74. ### Now get cumulative baseline hazard, centred
  75. basehaz.cent<-basehaz(fit,centered=TRUE)
  76. head(basehaz.cent)
  77.  
  78.  
  79. ### Extract hazard at relevant time (10 years)
  80. basehaz156.cent<-basehaz.cent[basehaz.cent$time==156,]$hazard
  81.  
  82. ## Generate survival function
  83. surv.cent<-exp(-basehaz156.cent*exp(lp.cent))
  84.  
  85. ## generate risks
  86. risks.cent<-1-surv.cent
  87.  
  88.  
  89. ### Compare results
  90. head(risks.uncent)
  91. head(risks.cent)
  92.  
  93. > head(risks.uncent)
  94. risk low.lim upp.lim
  95. 1 0.51509705 5.322379e-04 1.0000000
  96. 2 0.63037120 5.975637e-04 1.0000000
  97. 3 0.26288533 3.883639e-04 1.0000000
  98. 4 0.01961519 4.553868e-05 0.9998191
  99. 5 0.02794971 1.615327e-04 0.9930874
  100. 6 0.06717218 2.255081e-04 1.0000000
  101.  
  102. > head(risks.cent)
  103. risk low.lim upp.lim
  104. 1 0.51509705 0.13947540 0.96942843
  105. 2 0.63037120 0.15703534 0.99696746
  106. 3 0.26288533 0.09773654 0.59527761
  107. 4 0.01961519 0.01014882 0.03774142
  108. 5 0.02794971 0.01121265 0.06878584
  109. 6 0.06717218 0.03569621 0.12455084
Add Comment
Please, Sign In to add comment