BP神经网络预测(R语言)

说明:测试数据具有周期性特征(7天)

library(nnet)
source <- c(10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422,
6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355,10477,
10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767,12136,12812,
12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232,13261,13230,15535,
16837,19598,14823,11622,19391,18177,19994,14723,15694,13248,9543,12872,13101,15053,
12619,13749,10228,9725,14729,12518,14564,15085,14722,11999,9390,13481,14795,15845,
15271,14686,11054,10395,14775,14618,16029,15231,14246,12095,10473,15323,15381,14947)

for(i in 1:10){                    #分轮预测最后十个数
    dataLen <- length(source) - i  #该轮测试数据集 
    real <- source[dataLen + 1]    #实际值
    xNum = (dataLen) %/% 7         #组数
    yNum = 7                       #每组7个数
    data <- array(1 :(xNum * yNum), c(xNum, yNum))
    
    pre = dataLen
    for(x in 1 : xNum){          #数组赋值
        for(y in 1 : yNum){
            data[x, y] = source[pre]
            pre = pre - 1
           
        }
        if(pre < 7){
            break
        }
    }


    ascData <- array(1 : (xNum * yNum), c(xNum, yNum)) #数组逆序
    for(x in 1 : xNum){
        for(y in 1 : yNum){
            ascData[x, y] = data[xNum - x + 1, y]
        }
    }


    colnames(ascData) <- c("a","b","c","d","e","f","g") #每列列名
    trainData<-data.frame(scale(ascData[, c(1 : 7)]))      
   
    nn <- nnet(a~b+c+d+e+f+g, trainData[1:(xNum-1),], size=10, decay=0.01, maxit=1000, linout=F, trace=F)
    predict <- predict(nn, trainData[xNum,])
   
    predict = predict * sd(ascData[,1]) + mean(ascData[,1])
    percent <- (predict - real) * 100 / real
    res <- paste("预测值:",predict,"实际值:",real,"误差:",percent)
    print(res)
 }
 
 

第一次预测:

[1] "预测值: 14575.1350287356 实际值: 14947 误差: -2.48789035434786"
[1] "预测值: 14564.0845492553 实际值: 15381 误差: -5.31119856150275"
[1] "预测值: 9813.5726280941 实际值: 15323 误差: -35.9552788090185"
[1] "预测值: 11368.0194667784 实际值: 10473 误差: 8.54597027383219"
[1] "预测值: 15418.2163474166 实际值: 12095 误差: 27.4759516115473"
[1] "预测值: 15123.5388034679 实际值: 14246 误差: 6.15989613553241"
[1] "预测值: 16059.771373612 实际值: 15231 误差: 5.44134576595118"
[1] "预测值: 14369.1727184095 实际值: 16029 误差: -10.3551517973077"
[1] "预测值: 14772.465408013 实际值: 14618 误差: 1.05667949112772"
[1] "预测值: 9043.33120180563 实际值: 14775 误差: -38.7930206307571"

第二次预测:

[1] "预测值: 14642.415790076 实际值: 14947 误差: -2.03776149009185"
[1] "预测值: 14477.8210850479 实际值: 15381 误差: -5.87204287726449"
[1] "预测值: 9861.94237828068 实际值: 15323 误差: -35.6396111839674"
[1] "预测值: 11369.1672070958 实际值: 10473 误差: 8.55692931438747"
[1] "预测值: 15419.1446478315 实际值: 12095 误差: 27.4836266873212"
[1] "预测值: 15126.2916871806 实际值: 14246 误差: 6.17922004198079"
[1] "预测值: 16059.699750677 实际值: 15231 误差: 5.44087552148231"
[1] "预测值: 14369.1371979859 实际值: 16029 误差: -10.355373398304"
[1] "预测值: 14772.4626692104 实际值: 14618 误差: 1.05666075530422"
[1] "预测值: 9044.18840033801 实际值: 14775 误差: -38.7872189486429"

第三次预测:

[1] "预测值: 14575.1720964241 实际值: 14947 误差: -2.48764236017837"
[1] "预测值: 14490.6672087037 实际值: 15381 误差: -5.78852344643614"
[1] "预测值: 9805.73876255179 实际值: 15323 误差: -36.0064036901926"
[1] "预测值: 11368.0667648788 实际值: 10473 误差: 8.54642189323791"
[1] "预测值: 15422.2708856613 实际值: 12095 误差: 27.5094740443269"
[1] "预测值: 15123.5520814093 实际值: 14246 误差: 6.15998934023089"
[1] "预测值: 16059.8131034466 实际值: 15231 误差: 5.44161974556236"
[1] "预测值: 14360.0319673611 实际值: 16029 误差: -10.4121781311303"
[1] "预测值: 14771.4816606555 实际值: 14618 误差: 1.049949792417"
[1] "预测值: 9044.31285364907 实际值: 14775 误差: -38.7863766250486"




 

你可能感兴趣的:(BP神经网络预测(R语言))