资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

创新互联公司是专业的嘉定网站建设公司,嘉定接单;提供成都网站制作、做网站、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行嘉定网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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

  • 前向逐步回归

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

  • 实例代码

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


当前题目:R语言中的前向逐步回归是怎样的
URL网址:http://cdkjz.cn/article/iechpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220