Skip to content
Back to skills

R Ml Survival

ASecurity

R survival analysis. Use for Kaplan-Meier, Cox regression, survival curves with survival and survminer.

  • 5 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added June 4, 2026
data

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill r-ml-survival --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of R Ml Survival?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for R Ml Survival
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-r-ml-survival/badge)](https://www.skillsdirectory.com/skills/leolin990405-r-ml-survival)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: r-ml-survival
description: R survival analysis. Use for Kaplan-Meier, Cox regression, survival curves with survival and survminer.
---

# R Survival Analysis

Time-to-event analysis.

## survival Package

```r
library(survival)

# Create survival object
surv_obj <- Surv(time = df$time, event = df$status)

# Kaplan-Meier estimate
km_fit <- survfit(surv_obj ~ 1)
summary(km_fit)
plot(km_fit)

# KM by group
km_fit <- survfit(surv_obj ~ group, data = df)
plot(km_fit, col = 1:2)

# Median survival
km_fit

# Cox proportional hazards
cox_model <- coxph(Surv(time, status) ~ age + sex + treatment, data = df)
summary(cox_model)

# Hazard ratios
exp(coef(cox_model))
exp(confint(cox_model))

# Test proportional hazards assumption
cox.zph(cox_model)
plot(cox.zph(cox_model))

# Stratified Cox
cox_model <- coxph(Surv(time, status) ~ age + strata(sex), data = df)
```

## survminer (Visualization)

```r
library(survminer)

# Kaplan-Meier plot
km_fit <- survfit(Surv(time, status) ~ group, data = df)

ggsurvplot(km_fit,
  data = df,
  pval = TRUE,
  conf.int = TRUE,
  risk.table = TRUE,
  risk.table.col = "strata",
  ggtheme = theme_minimal(),
  palette = c("#E7B800", "#2E9FDF")
)

# Customization
ggsurvplot(km_fit,
  data = df,
  pval = TRUE,
  pval.method = TRUE,
  log.rank.weights = "1",
  surv.median.line = "hv",
  legend.title = "Group",
  legend.labs = c("Control", "Treatment"),
  xlab = "Time (months)",
  ylab = "Survival probability",
  break.time.by = 12,
  xlim = c(0, 60)
)

# Forest plot for Cox model
ggforest(cox_model, data = df)

# Cumulative hazard
ggsurvplot(km_fit, fun = "cumhaz")

# Event plot
ggsurvplot(km_fit, fun = "event")
```

## Parametric Models

```r
library(survival)

# Weibull
weibull_model <- survreg(Surv(time, status) ~ age + sex, data = df, dist = "weibull")
summary(weibull_model)

# Exponential
exp_model <- survreg(Surv(time, status) ~ age + sex, data = df, dist = "exponential")

# Log-normal
lognorm_model <- survreg(Surv(time, status) ~ age + sex, data = df, dist = "lognormal")

# Predictions
predict(weibull_model, type = "response")  # Expected time
predict(weibull_model, type = "quantile", p = 0.5)  # Median
```

## Competing Risks

```r
library(cmprsk)

# Cumulative incidence
ci <- cuminc(df$time, df$status, df$group)
plot(ci)

# Fine-Gray model
fg_model <- crr(df$time, df$status, df[, c("age", "sex")])
summary(fg_model)
```

## Random Survival Forests

```r
library(randomForestSRC)

# Fit
rsf <- rfsrc(Surv(time, status) ~ ., data = df, ntree = 500)

# Variable importance
vimp(rsf)
plot(vimp(rsf))

# Predictions
pred <- predict(rsf, newdata = test)
pred$survival  # Survival curves
```

## Log-Rank Test

```r
# Compare survival curves
survdiff(Surv(time, status) ~ group, data = df)

# Pairwise comparisons
pairwise_survdiff(Surv(time, status) ~ group, data = df)
```

Files in this skill

  • SKILL.md2.8 KB
  • survival/SKILL.md3.2 KB
  • survminer/SKILL.md3.7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in data →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.