How to plot the NAR predicted values in matlab?
3 views (last 30 days)
Show older comments
Hi there. After several reading up, I realise for my project I will need to use NAR because I am using history values to predict. I have a monthly data previous months. & I need to predict for the next 12 months.
T = tonndata(Target_Set1,false,false);
% Create a Nonlinear Autoregressive Network
feedbackDelays = 1:2;
hiddenLayerSize = 2;
net = narnet(feedbackDelays,hiddenLayerSize);
% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[x,xi,ai,t] = preparets(net,{},{},T);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
[xc,xic,aic,tc] = preparets(netc,{},{},T);
yc = netc(xc,xic,aic);
perfc = perform(net,tc,yc)
% Step-Ahead Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1. The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
[xs,xis,ais,ts] = preparets(nets,{},{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(net,ts,ys)
How do I plot the predicted values after training and validation.From the examples I have seen for NARXNET normally they will use the code below.
plot([cell2mat(T),nan(1,N);
nan(1,length(T)),cell2mat(yPred);
nan(1,length(T)),cell2mat(targetSeriesVal)]')
legend('Original Targets','Network Predictions','Expected Outputs');
Thanks in advance.
0 Comments
Accepted Answer
Greg Heath
on 6 Jan 2014
Edited: Greg Heath
on 6 Jan 2014
1. Use the Target autocorrelation function to determine a good set of feedback delays. Search the NEWSGROUP and ANSWERS using
greg nncorr
2. Find a good value for H, the number of hidden nodes, by using a double loop over candidate values and initial weights. Search N & A:
greg narnet Ntrials
greg narxnet Ntrials
3. Plot y and yc. If perfc is much worse than performance, continue training with
[ netc trc yc ec xcf acf ] = train(netc, x, t, xi, ai);
4. Search N & A:
greg closeloop
Hope this helps.
Greg
4 Comments
Madhav Kathal
on 22 Feb 2016
hi greg! i have gone through some of your answers and you consistently say that we can find feedback delays by plotting the autocorrelation function. i am currently working on narnet and i am unable to find the optimum no. of delays i should use. How exactly do i find it? Steps or any links would help. i have gone through the NEWSGROUP and ANSWERS. Thanks !
Greg Heath
on 22 Feb 2016
In general, it is too hard to find the optimal set of hidden nodes and delays. I am just happy to find the minimum number of delays and hidden nodes that will model 95% of the average target variance.
I have many postings dealing with that search NEWSGROUP and ANSWERS using
narnet nncorr
Write back in a NEW POST if you have problems understanding or using it.
Hope this helps
Greg
More Answers (0)
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!