Skip to content
Back to skills

Rpart

ASecurity

R rpart package for decision trees. Use for recursive partitioning classification and regression trees.

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

Works with

  • terminal

Security analysis

A100/100

Scanned June 4, 2026

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

Installs into .claude/skills of the current project.

Are you the author of Rpart?

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

Security grade badge for Rpart
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-rpart/badge)](https://www.skillsdirectory.com/skills/leolin990405-rpart)

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: rpart
description: R rpart package for decision trees. Use for recursive partitioning classification and regression trees.
---

# rpart

Recursive partitioning for decision trees.

## Classification Tree

```r
library(rpart)

# Train
tree <- rpart(Species ~ ., data = iris, method = "class")

# Predict class
pred <- predict(tree, newdata = test_df, type = "class")

# Predict probabilities
pred_prob <- predict(tree, newdata = test_df, type = "prob")
```

## Regression Tree

```r
# Train
tree <- rpart(mpg ~ ., data = mtcars, method = "anova")

# Predict
pred <- predict(tree, newdata = test_df)
```

## Control Parameters

```r
tree <- rpart(
  target ~ .,
  data = train_df,
  method = "class",           # class, anova, poisson, exp
  control = rpart.control(
    minsplit = 20,            # Min obs for split attempt
    minbucket = 7,            # Min obs in terminal node
    cp = 0.01,                # Complexity parameter
    maxdepth = 30,            # Max tree depth
    xval = 10,                # Cross-validation folds
    maxcompete = 4,           # Competing splits to display
    maxsurrogate = 5,         # Surrogate splits
    usesurrogate = 2          # How to use surrogates
  )
)
```

## Visualization

```r
# Base plot
plot(tree)
text(tree, use.n = TRUE)

# Better plot with rpart.plot
library(rpart.plot)
rpart.plot(tree)
rpart.plot(tree, extra = 104)  # Show probabilities

# Prp function
prp(tree, faclen = 0, extra = 1, roundint = FALSE)
```

## Pruning

```r
# View complexity table
printcp(tree)
plotcp(tree)

# Get optimal cp
opt_cp <- tree$cptable[which.min(tree$cptable[, "xerror"]), "CP"]

# Prune tree
pruned_tree <- prune(tree, cp = opt_cp)

# 1-SE rule
cp_1se <- tree$cptable[which.min(tree$cptable[, "xerror"]) + 1, "CP"]
pruned_tree <- prune(tree, cp = cp_1se)
```

## Variable Importance

```r
# Get importance
tree$variable.importance

# Normalized importance
importance <- tree$variable.importance / sum(tree$variable.importance)

# Plot
barplot(tree$variable.importance, las = 2)
```

## Model Summary

```r
# Summary
summary(tree)

# Print tree rules
print(tree)

# Tree structure
tree$frame
```

## Cross-Validation

```r
# Built-in CV (default xval = 10)
tree <- rpart(target ~ ., data = df)

# CV error
tree$cptable[, "xerror"]

# CV standard error
tree$cptable[, "xstd"]
```

## Handling Missing Values

```r
# Surrogate splits (default)
tree <- rpart(target ~ ., data = df)

# Predictions with missing values work automatically
pred <- predict(tree, newdata = test_df_with_na)
```

## Cost-Sensitive Learning

```r
# Loss matrix for classification
loss_matrix <- matrix(c(0, 1, 5, 0), nrow = 2)

tree <- rpart(
  target ~ .,
  data = df,
  method = "class",
  parms = list(loss = loss_matrix)
)

# Prior probabilities
tree <- rpart(
  target ~ .,
  data = df,
  method = "class",
  parms = list(prior = c(0.3, 0.7))
)
```

## Extract Rules

```r
# As text
rpart.rules(tree)

# Path to terminal nodes
path.rpart(tree, nodes = c(2, 3))
```

## Poisson Regression Tree

```r
# For count data
tree <- rpart(count ~ ., data = df, method = "poisson")
```

## Survival Tree

```r
# For survival data
library(survival)
tree <- rpart(Surv(time, status) ~ ., data = df, method = "exp")
```

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…

Newsletter

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