# Read in Data
library(survival)
anemia <- read.delim("http://myweb.uiowa.edu/pbreheny/data/anemia.txt")
S <- with(anemia, Surv(Time,Status!=0))
fit <- survfit(S~1)


# Kaplan-Meier calculations
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")

table1


# Plot Survival Curve
plot(fit, ylab = "Probability", xlab = "Time")


# Separate curves for each treatment group
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")
