## -----------------------------------------------------------------------------
#| message: false
#| code-summary: "Load Survival Package"
#| code-fold: true
# automatically install package if it doesn't exist on your machine, 
# then loads the library
if(!require(survival)){
  install.packages("survivial")
  library(survival)
} else {
    library(survival)
  }


## -----------------------------------------------------------------------------
anemia <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/anemia/anemia.txt')

S <- with(anemia, Surv(Time,Status!=0)) # get response
fit <- survfit(S~1) 

## -----------------------------------------------------------------------------
#| include: false

library(flextable)
library(ggplot2)


## ----echo=FALSE---------------------------------------------------------------
table1<- cbind(fit$time[1:5], fit$n.risk[1:5], fit$n.event[1:5], round((fit$n.risk[1:5]-fit$n.event[1:5])/fit$n.risk[1:5],4), round(fit$surv[1:5],4))

colnames(table1)<- c("time", "n(t)", "d(t)", "[n(t)-d(t)]/n(t)", "cumproduct")

flextable(as.data.frame(table1)) |> autofit()


## ----out.width='.6\\linewidth'------------------------------------------------
plot(fit, ylab = "Probability", xlab = "Time")


## ----out.width='.6\\linewidth'------------------------------------------------
fit2 <- with(anemia, survfit(S ~ Trt))

plot(fit2, ylab = "Overall Survival", 
     xlab = "Time", 
     col = c("red","blue"))
legend("bottomleft", c("MTX","MTX + CSP"), 
       text.col = c("red","blue"), bty = "n")



## -----------------------------------------------------------------------------
fit2 <- with(anemia, Surv(Time, Status != 0) ~ Trt)
survdiff(fit2)

