Good day I would like to get a clear code of how to use NARX
Good day,
I would like to get a clear code of how to use NARX tool in Matlab to make prediction.
for example, If I have a pressure vector of 93 data points, and I trained the network for this data vs time for the first 70 data, how can I predict the remaining 23 data using the NARX I built? Also, is the prediction I made for future?
see my code below and let me know if this is the correct way to do it
clear;
load Pressure
X=time(1:70);
T=Pressure(1:70);
X_2=time(71:end);
T_2=Pressure(71:end);
X_2 = tonndata(X_2,false,false);
T_2 = tonndata(T_2,false,false);
% Solve an Autoregression Problem with External Input with a NARX Neural Network
% Script generated by Neural Time Series app
% Created 15-Feb-2017 12:55:53
%
% This script assumes these variables are defined:
%
% X - input time series.
% T - feedback time series.
X = tonndata(X,false,false);
T = tonndata(T,false,false);
% Choose a Training Function
% For a list of all training functions type: help nntrain
% \'trainlm\' is usually fastest.
% \'trainbr\' takes longer but may be better for challenging problems.
% \'trainscg\' uses less memory. Suitable in low memory situations.
trainFcn = \'trainlm\'; % Levenberg-Marquardt backpropagation.
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,\'open\',trainFcn);
% 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,X,{},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, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
nets = removedelay(net);
[xs_f,xis_f,ais_f,ts_f] = preparets(nets,X_2,{},T_2);
ys_f = nets(xs_f,xis_f,ais_f);
plot(time,Pressure);
hold on
plot(time(71:end-1),cell2mat(ys_f),\'g\')
Solution
[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, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
nets = removedelay(net);
[xs_f,xis_f,ais_f,ts_f] = preparets(nets,X_2,{},T_2);
ys_f = nets(xs_f,xis_f,ais_f);
plot(time,Pressure);
hold on
plot(time(71:end-1),cell2mat(ys_f),\'g\')



