R語言 決策樹 Bagging 隨機(jī)森林 Random Forest 隨機(jī)森林變

一、回歸

二、對回歸準(zhǔn)確性進(jìn)行檢驗(yàn)


代碼:

#加載包
library (tidyverse)#數(shù)據(jù)預(yù)處理
library(tidytext)#分組排序
library(glmnet)# Lasso & Ridgelibrary ( rpart)#決策樹
library(ipred)# Bagging
library( randomForest)#隨機(jī)森林

#、加載數(shù)據(jù)
data( 'mtcars ' )

代碼:

#數(shù)據(jù)預(yù)處理
data = mtcars %>%
mutate(vs = factor(vs , levels = c(0,1), labels = c( 'V-shaped
, 'straight" )) ,
am = factor(am, levels = c(0,1), labels = c ( ' automatic ', 'manual ' )))

x = model.matrix(mpg~. ,data = data)[ ,-1]#啞元變量轉(zhuǎn)換

y = data$mpg
data = data.frame (mpg = y , x)


#數(shù)據(jù)集劃分
set .seed(1)
train_id = sample(1:nrow(data),0.7*nrow(data))train = data[train_id , ]
test = data[-train_id, ]


#訓(xùn)練模型set.seed(1)
linear_reg = lm(mpg~. ,data = train)
stepwise = step(linear_reg,direction = 'both ' ,trace = 0)cv .ridge = cv.glmnet(x = as.matrix (train[ ,-1]),
y = train$mpg,
family = 'gaussian ' ,alpha = 0,
nfolds = 5)
cv.lasso = cv.glmnet(x = as.matrix (train[ ,-1]),
y = train$mpg,
family = 'gaussian ' ,alpha = 1,
nfolds = 5)
tree = rpart(mpg~. ,data = train)
bag_tree = bagging(mpg~. , data = train)
rf = randomForest(mpg~. , data = train , importance = T)

6隨機(jī)森林變量重要性
#變量重要性
varImpPlot(rf ,main = 'Variable Importance in Random Forest' )



。。。。。。。。。。。。。。。。。。。。。。。




最后:





