Example of Time Series Prediction using Neural Networks in R

后端 未结 2 1744
攒了一身酷
攒了一身酷 2020-12-23 18:35

Anyone\'s got a quick short educational example how to use Neural Networks (nnet in R) for the purpose of prediction? Here is an example, in R

2条回答
  •  佛祖请我去吃肉
    2020-12-23 18:45

    The solution proposed by @agstudy is useful, but in-sample fits are not a reliable guide to out-of-sample forecasting accuracy. The gold standard in forecasting accuracy measurement is to use a holdout sample. Remove the last 5 or 10 or 20 observations (depending to the length of the time series) from the training sample, fit your models to the rest of the data, use the fitted models to forecast the holdout sample and simply compare accuracies on the holdout, using Mean Absolute Deviations (MAD) or weighted Mean Absolute Percentage Errors (wMAPEs). So to do this you can change the code above in this way:

    require(quantmod) 
    require(nnet)
    require(caret)
    t = seq(0,20,length=200)
    y = 1 + 3*cos(4*t+2) +.2*t^2 + rnorm(200)
    dat <- data.frame( y, x1=Lag(y,1), x2=Lag(y,2))
    names(dat) <- c('y','x1','x2')
    train_set <- dat[c(3:185),]
    test_set <- dat[c(186:200),]
    #Fit model
    model <- train(y ~ x1+x2 , 
                   train_set, 
                   method='nnet', 
                   linout=TRUE, 
                   trace = FALSE)
    ps <- predict(model, test_set)
    
    #Examine results
    
    plot(T,Y,type="l",col = 2)
    lines(T[c(186:200)],ps, col=3)
    legend(5, 70, c("y", "pred"), cex=1.5, fill=2:3)
    

    This last two lines output the wMAPE of the forecasts from the model

    sum(abs(ps-test_set["y"]))/sum(test_set)
    

提交回复
热议问题