-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrf-regression-R.qmd
72 lines (50 loc) · 1.58 KB
/
rf-regression-R.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: "Random Forest Regression with R"
format: html
typora-copy-images-to: images
---
```{r}
# Load required libraries
library(randomForest)
library(ggplot2)
# Set seed for reproducibility
set.seed(123)
data <- mtcars
```
# Split data into training and testing sets
```{r}
train_indices <- sample(1:nrow(data), 0.7 * nrow(data))
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]
```
# Train Random Forest model and make predictions
```{r}
rf_model <- randomForest(mpg ~ ., data = train_data, ntree = 500)
predictions <- predict(rf_model, newdata = test_data)
```
# Calculate RMSE
```{r}
rmse <- sqrt(mean((test_data$mpg - predictions)^2))
cat("Root Mean Square Error:", rmse, "\n")
```
# Plot actual vs predicted values
```{r}
ggplot(data.frame(actual = test_data$mpg, predicted = predictions), aes(x = actual, y = predicted)) +
geom_point(alpha = 0.5) +
geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") +
labs(x = "Actual Values", y = "Predicted Values", title = "Random Forest Regression: Actual vs Predicted")
```
# Print feature importance
```{r}
importance <- importance(rf_model)
print(importance)
```
# Plot feature importance
```{r}
importance_df <- data.frame(feature = rownames(importance), importance = importance[, 1])
ggplot(importance_df, aes(x = reorder(feature, importance), y = importance)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(x = "Features", y = "Importance", title = "Random Forest: Feature Importance")
```
alternative at https://hackernoon.com/random-forest-regression-in-r-code-and-interpretation