Skip to content
Back to skills

R Ml Trees

ASecurity

R tree-based models. Use for random forests, decision trees, and ensemble methods with ranger, randomForest, rpart.

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 4, 2026
data

Security analysis

A100/100

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

Scanned June 4, 2026

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

Installs into .claude/skills of the current project.

Are you the author of R Ml Trees?

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

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

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-trees
description: R tree-based models. Use for random forests, decision trees, and ensemble methods with ranger, randomForest, rpart.
---

# R Tree-Based Models

Decision trees and random forests.

## ranger (Fast Random Forest)

```r
library(ranger)

# Classification
model <- ranger(
  target ~ .,
  data = train,
  num.trees = 500,
  mtry = sqrt(ncol(train) - 1),
  importance = "impurity",
  probability = TRUE  # For probabilities
)

# Regression
model <- ranger(
  target ~ .,
  data = train,
  num.trees = 500
)

# Predictions
pred <- predict(model, test)
pred$predictions

# Variable importance
importance(model)
sort(importance(model), decreasing = TRUE)

# OOB error
model$prediction.error
```

## randomForest

```r
library(randomForest)

# Train
model <- randomForest(
  target ~ .,
  data = train,
  ntree = 500,
  mtry = 3,
  importance = TRUE
)

# Predictions
pred <- predict(model, test)
pred_prob <- predict(model, test, type = "prob")

# Variable importance
importance(model)
varImpPlot(model)

# Partial dependence
partialPlot(model, train, x.var = "feature")

# OOB error
model$err.rate[nrow(model$err.rate), ]
```

## rpart (Decision Tree)

```r
library(rpart)
library(rpart.plot)

# Train
model <- rpart(
  target ~ .,
  data = train,
  method = "class",  # or "anova" for regression
  control = rpart.control(
    minsplit = 20,
    cp = 0.01,
    maxdepth = 10
  )
)

# Plot tree
rpart.plot(model, extra = 104)

# Predictions
pred <- predict(model, test, type = "class")
pred_prob <- predict(model, test, type = "prob")

# Pruning
printcp(model)
plotcp(model)
model_pruned <- prune(model, cp = 0.02)

# Variable importance
model$variable.importance
```

## party/partykit

```r
library(partykit)

# Conditional inference tree
model <- ctree(target ~ ., data = train)
plot(model)

# Predictions
pred <- predict(model, test)

# Random forest with conditional trees
library(party)
model <- cforest(target ~ ., data = train)
```

## Ensemble with tidymodels

```r
library(tidymodels)

# Random forest
rf_spec <- rand_forest(
  mtry = tune(),
  trees = 500,
  min_n = tune()
) %>%
  set_engine("ranger", importance = "impurity") %>%
  set_mode("classification")

# Decision tree
tree_spec <- decision_tree(
  cost_complexity = tune(),
  tree_depth = tune(),
  min_n = tune()
) %>%
  set_engine("rpart") %>%
  set_mode("classification")

# Tune
tune_results <- tune_grid(
  workflow() %>% add_model(rf_spec) %>% add_formula(target ~ .),
  resamples = vfold_cv(train, v = 5),
  grid = 20
)
```

## Comparison

| Package | Speed | Features | Use Case |
|---------|-------|----------|----------|
| ranger | Fastest | Survival, probability | Large data |
| randomForest | Medium | Classic, stable | General |
| rpart | Fast | Interpretable | Single tree |
| party | Slow | Statistical tests | Research |

Files in this skill

  • SKILL.md2.8 KB
  • ranger/SKILL.md3 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.

Comments

Loading comments…