随机森林回归模型

1. 分为测试集和验证集 

library(randomForest)
data("mtcars")
data=mtcars
set.seed(123)
train <- sample(nrow(data), nrow(data)*0.7)
data_train <- data[train, ]
data_test <- data[-train, ]

2. 初步建立随机森林模型

set.seed(123)
data_train.forest <- randomForest(mpg~., data = data_train, importance = TRUE)
data_train.forest

Call:
 randomForest(formula = mpg ~ ., data = data_train, importance = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 4.936502
                    % Var explained: 84.44

结果中,% Var explained体现了预测变量对响应变量有关方差的整体解释率。

查看该模型的预测性能,可以看到具有较高的精度。

#使用训练集,查看预测精度
data_predict <- predict

你可能感兴趣的:(机器学习,随机森林,回归,机器学习)