R语言中的前向逐步回归是怎样的

发布时间:2021-11-22 09:25:48 作者:柒染
来源:亿速云 阅读:270

这篇文章给大家介绍R语言中的前向逐步回归是怎样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

 建模过程中,选择合适的特征集合,可以帮助控制模型复杂度,防止过拟合等问题。为了选取最佳的特征集合,可以遍历所有的列组合,找出效果最佳的集合,但这样需要大量的计算。本文介绍的前向逐步回归法是针对最小二乘法的修改。相对于要将所有组合情况遍历一遍,前向逐步回归可以大大节省计算量,选择最优的特征集合,从而解决过拟合问题。

  • 前向逐步回归

前向逐步回归的过程是:遍历属性的一列子集,选择使模型效果最好的那一列属性。接着寻找与其组合效果最好的第二列属性,而不是遍历所有的两列子集。以此类推,每次遍历时,子集都包含上一次遍历得到的最优子集。这样,每次遍历都会选择一个新的属性添加到特征集合中,直至特征集合中特征个数不能再增加。

  • 实例代码

1、数据导入并分组。导入数据,将数据集抽取70%作为训练集,剩下30%作为测试集。特征与标签分开存放。

target.url <- "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"

data <- read.csv(target.url,header = T,sep=";")


#divide data into training and test sets

index <- which((1:nrow(data))%%3==0)

train <- data[-index,]

test <- data[index,]


#arrange date into list and label sets

trainlist <- train[,1:11]

testlist <- test[,1:11]

trainlabel <- train[,12]

testlabel <- test[,12]

2、前向逐步回归构建输出特征集合。通过for循环,从属性的一个子集开始进行遍历。第一次遍历时,该子集为空。每一个属性被加入子集后,通过线性回归来拟合模型,并计算在测试集上的误差,每次遍历选择得到误差最小的一列加入输出特征集合中。最终得到输出特征集合的关联索引和属性名称。

#build list of attributes one-at-a-time, starting with empty

attributeList<-as.numeric()

index<-1:ncol(trainlist)

indexSet<-as.numeric()

oosError<-as.numeric()


for(i in index){

  #attributes not in list already

  attTry<-setdiff(index,attributeList)

  #try each attribute not in set to see which one gives least oos error

  errorList<-as.numeric()

  attTemp<-as.numeric()

  for(ii in attTry){

    attTemp<-append(attTemp,attributeList)

    attTemp<-append(attTemp,ii)

    xTrainTemp<-as.data.frame(trainlist[,attTemp])

    xTestTemp<-as.data.frame(testlist[,attTemp])

    names(xTrainTemp)<-names(trainlist[attTemp])

    names(xTestTemp)<-names(testlist[attTemp])

    lm.mod <- lm(trainlabel~.,data=xTrainTemp)

    rmsError<-rmse(testlabel,predict(lm.mod,(xTestTemp)))

    errorList<-append(errorList,rmsError)

    attTemp<-as.numeric()

  }

  iBest<-which.min(errorList)

  attributeList<-append(attributeList,attTry[iBest])

  oosError<-append(oosError,errorList[iBest])

}

cat("Best attribute indices: ", attributeList, "\n","Best attribute names: \n",names(trainlist[attributeList]))

索引与名称如下:

Best attribute indices:  11 2 10 7 6 9 1 8 4 3 5 

 Best attribute names: 

 alcohol volatile.acidity sulphates total.sulfur.dioxide free.sulfur.dioxide pH fixed.acidity density residual.sugar citric.acid chlorides

属性名列表的顺序也是属性的     重要性排序,了解属性重要性,可以增加模型的解释性。

3、模型效果评估。分别画出RMSE与属性个数之间的关系,前向逐步预测算法对数据预测对错误直方图,和真实标签与预测标签散点图。

plot(oosError,type = "l",xlab = "Number of Attributes",ylab = "ERMS",main = "error versus number of attributes")


finaltrain<-trainlist[,attributeList[1:which.min(oosError)]]

finaltest<-testlist[,attributeList[1:which.min(oosError)]]

lm.finalmol<-lm(trainlabel~.,data = finaltrain)

finalpre<-predict(lm.finalmol,finaltest)

errorVector<-testlabel-finalpre

hist(errorVector)


plot(predict(lm.finalmol,finaltest),testlabel,xlab = "Predicted Taste Score",ylab = "Actual Taste Score")

R语言中的前向逐步回归是怎样的

R语言中的前向逐步回归是怎样的

R语言中的前向逐步回归是怎样的

从图上可以看出,使用前9个属性,误差值一直在降低,加入第十个属性后,误差值开始增加。因此,我们选取输出特征集合的前9项,作为最终的最优特征集合。从散点图上看,得分在5、6时,预测情况非常好,因为区域的颜色深度可以反映点的堆积程度,一般情况下,机器学习算法对边缘数据预测效果不好。由于真正的标签是整数,所以散点图呈水平状分布。后两张图,均可通过分析图像形态,指出模型性能提升途径。

关于R语言中的前向逐步回归是怎样的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 什么是r语言
  2. FEC前向纠错算法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言

上一篇:如何理解r语言中的缺失值处理

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》