forecasting method in matlab using VAR model

조회 수: 81 (최근 30일)
mallela ankamma rao
mallela ankamma rao 2024년 11월 19일 16:09
댓글: Pavl M. 2024년 11월 26일 16:25
Good evening sir
I request you please resolve the error from my code. I attached DATA also sir .
CODE:
% Step 1: Load data
data = xlsread('DATA.xlsx');
time = data(1:400, 1); % Time column (1st column)
inf_data = data(1:400, 2); % Infected data column (2nd column)
% Step 2: Prepare data for VAR
% Since we are using univariate data, we treat it as a single-variable time series
% Create lagged data (you can modify the lag length)
lags = 2; % Number of lags
dataForVAR = [inf_data(1:end-lags), inf_data(lags+1:end)];
% Step 3: Fit the VAR model
model = varm(2, 1); % 2 lags, 1 variable
fitModel = estimate(model, dataForVAR); % Fit the model
% Step 4: Forecasting using the VAR model
numForecasts = 10; % Number of steps to forecast
% Ensure 'Y0' is a numeric matrix, not a vector
presampleData = dataForVAR(end-lags+1:end, :); % The last lags data as presample
YForecast = forecast(fitModel, numForecasts, 'Y0', presampleData);
% Rescale the forecasted data (if needed)
forecastData = YForecast(:, 1); % First column is the forecast for the 'inf_data' series
% Step 5: Plot the results
figure;
hold on;
plot(time, inf_data, 'b-', 'LineWidth', 1.5, 'DisplayName', 'Actual Data'); % Actual data
forecastTime = time(end) + (1:numForecasts); % Forecast time points
plot(forecastTime, forecastData, 'r-', 'LineWidth', 1.5, 'DisplayName', 'Forecasted Data'); % Forecasted data
xlabel('Time', 'FontSize', 14);
ylabel('Infected Cases', 'FontSize', 14);
title('Forecasting with VAR Model', 'FontSize', 16);
legend('Location', 'Best');
grid on;
hold off;
ERROR:
Error using varm/forecast (line 195)
The value of 'Y0' is invalid. Expected presample responses to be one of these types:
double
Error in plots (line 20)
YForecast = forecast(fitModel, numForecasts, 'Y0', presampleData);

답변 (3개)

Pavl M.
Pavl M. 2024년 11월 19일 17:15
편집: Pavl M. 2024년 11월 20일 17:49
clc
clear all
close all
%% Step 1: Load data
data = xlsread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1809848/DATA.xlsx');
time = double(data(1:400,1)); % Time column (1st column)
inf_data = double(data(1:400,2)); % Infected data column (2nd column)
d2 = double(data(1:400,3));
%% Step 2: Prepare data for VAR
% Since we are using univariate data, we treat it as a single-variable time series
% Create lagged data (you can modify the lag length)
lags = 2; % Number of lags
dataForVAR = [inf_data(1:end-lags);inf_data(lags+1:end)];
%% Step 3: Fit the VAR model
model = varm(1,lags); % 2 lags, 1 sequence of variables
fitModel = estimate(model,dataForVAR); % Fit the model
%Identified model whose output is to be forecasted, specified as one of the following:
%Linear model — idpoly, idproc, idss, idtf, or idgrey
%Nonlinear model — idnlgrey, idnlhw, or idnlarx
%If a model is unavailable, estimate sys from PastData using commands
% such as ar, arx, armax, nlarx, and ssest.
%% Step 4: Forecasting using the VAR model
summarize(fitModel)
numForecasts = 10; % Number of steps to forecast
presampleData = dataForVAR(end-lags+1:end,:); % The last lags data as presample
%opt = forecastOptions('InitialCondition','e'); % Add more options
%opt.InputOffset = 0;
YForecast = forecast(fitModel,presampleData,numForecasts);
%or try using the estimated model and in-sample data as presample observations.
YForecast2 = forecast(fitModel,dataForVAR,numForecasts);
% Rescale the forecasted data (if needed)
forecastData = YForecast(:, 1); % First column is the forecast for the 'inf_data' series
forecastData2 = YForecast2(:,1);% Step 5: Plot the results
forecastTime = time(end)+(1:numForecasts); % Forecast time points
%% Visualize anticipative foreseeing:
figure;
hold on;
plot(time,inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastTime,forecastData,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on presampled input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
figure
plot(time,inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastTime,forecastData2,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on original input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
  댓글 수: 2
mallela ankamma rao
mallela ankamma rao 2024년 11월 20일 2:33
Thank you for your response Pavl M sir. while i run the code i got the another error. i request please this problem sir
CODE:
% Clear workspace and setup
clear;
clc;
close all;
% % Step 1: Load data
data = xlsread('DATA.xlsx');
time = double(data(1:400, 1)); % Time column (1st column)
inf_data = double(data(1:400, 2)); % Infected data column (2nd column)
%% Step 2: Prepare data for VAR
% Since we are using univariate data, we treat it as a single-variable time series
% Create lagged data (you can modify the lag length)
lags = 2; % Number of lags
dataForVAR = [inf_data(1:end-lags),inf_data(lags+1:end)];
%% Step 3: Fit the VAR model
model = varm(1,2); % 2 lags, 1 variable
fitModel = estimate(model,dataForVAR); % Fit the model
%Identified model whose output is to be forecasted, specified as one of the following:
%Linear model — idpoly, idproc, idss, idtf, or idgrey
%Nonlinear model — idnlgrey, idnlhw, or idnlarx
%If a model is unavailable, estimate sys from PastData using commands
% such as ar, arx, armax, nlarx, and ssest.
%% Step 4: Forecasting using the VAR model
summarize(fitModel)
numForecasts = 10; % Number of steps to forecast
presampleData = dataForVAR(end-lags+1:end,:); % The last lags data as presample
%opt = forecastOptions('InitialCondition','e'); % Add more options
%opt.InputOffset = 0;
YForecast = forecast(fitModel,presampleData,numForecasts);
%or try using the estimated model and in-sample data as presample observations.
YForecast2 = forecast(fitModel,dataForVAR,numForecasts);
% Rescale the forecasted data (if needed)
forecastData = YForecast(:, 1); % First column is the forecast for the 'inf_data' series
forecastData2 = YForecast2(:,1);% Step 5: Plot the results
forecastTime = time(end)+(1:numForecasts); % Forecast time points
%% Visualize anticipative foreseeing:
figure;
hold on;
plot(time,inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastTime,forecastData,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on presampled input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
figure
plot(time,inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastTime,forecastData2,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on original input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
ERROR:
Error using varm/estimate (line 155)
The value of 'Y' is invalid. Expected Responses to be an array with number of columns equal to 1.
Error in plots (line 18)
fitModel = estimate(model,dataForVAR); % Fit the model
Pavl M.
Pavl M. 2024년 11월 20일 6:38
편집: Pavl M. 2024년 11월 20일 7:23
OK
Yes, of course. It is what m for to contribute simple solutions to complex conundrums.
Today Fixed to (';' instead of your ',' for making data as a row, varm parameters swap to meet the standard how the function signature is defiend,forceast(...) params swap to meet the standard of how the function signature is defined in TCE):
...
dataForVAR = [inf_data(1:end-lags);inf_data(lags+1:end)];
%% Step 3: Fit the VAR model
model = varm(1,2); % 2 lags, 1 sequence of variables
fitModel = estimate(model,dataForVAR); % Fit the model
%Identified model whose output is to be forecasted, specified as one of the following:
%Linear model — idpoly, idproc, idss, idtf, or idgrey
%Nonlinear model — idnlgrey, idnlhw, or idnlarx
%If a model is unavailable, estimate sys from PastData using commands
% such as ar, arx, armax, nlarx, and ssest.
%% Step 4: Forecasting using the VAR model
summarize(fitModel)
numForecasts = 10; % Number of steps to forecast
presampleData = dataForVAR(end-lags+1:end,:); % The last lags data as presample
%opt = forecastOptions('InitialCondition','e'); % Add more options
%opt.InputOffset = 0;
YForecast = forecast(fitModel,presampleData,numForecasts);
...
*****
What do you get?
Next conundrum predicted:
What if you run predict to evaluate / backtest how predicted result matches the observed response of an estimated model:
[yp,x0,Predictor]= predict(fitModel,[crossvalidationdatum],numForecasts)
crossvalidationdatum = presampleData or dataForVAR or?
to check whether yp equals to YForecast to estimate your forecast accuracy?
What if the numForecasts growing / shrinking?
What if in forecast(...) you increase the presampleData from just 2 rows in 1 column orginally towards more rows and columns to get more evolutional funcional dependency (distribution) resemblings (subleties) of the original process/plant/environment model that produced such datum distribution?
*****
What if you exchange your varm model to
ar,(too simple model, losses in peculiarities)
arx, (too simple model, losses in peculiarities)
armax (not advisable to use, better use VARIMAX),
nlarx,
ssest (better also try this).
?
Then you can construct more sophisticated:
Mdl = msVAR(mc,mdl) %creates a Markov-switching dynamic regression model Mdl from:
dataForVAR1 = [inf_data(1:end-lags);inf_data(lags+1:end)];
dataForVAR2 = [d2(1:end-lags);d2(lags+1:end)];
lags = 4;
dataForVAR3 = [inf_data(1:end-lags);inf_data(lags+1:end)];
dataForVAR4 = [d2(1:end-lags);d2(lags+1:end)];
model1 = varm(1,2,'Covariance',cov1); % 2 lags, 1 sequence of variables
model2 = varm(1,4,'Covariance',cov2); % 4 lags
fitModel1 = estimate(model1,dataForVAR1); % Fit the model
fitModel2 = estimate(model1,dataForVAR2); % Fit the model
fitModel3 = estimate(model2,dataForVAR3); % Fit the model
fitModel4 = estimate(model2,dataForVAR4); % Fit the model
%Model with states:
mc = dtmc(dataForVARArray,'StateNames',["State1" "State2" "State3" "State4"])
mdl = [fitModel1; fitModel2;fitModel3;fitModel4];
Use msVAR to create a Markov-switching dynamic regression model from the switching mechanism mc and the state-specific submodels mdl.
Mdl = msVAR(mc,mdl) %Markov switching model with states
Or simply use sset instead of msVAR. Interesting research gap question is identified here can sset outperform msVAR in stated model accuracy ?
sys = ssest(datum,4); %if for 4 stated model estimation

댓글을 달려면 로그인하십시오.


mallela ankamma rao
mallela ankamma rao 2024년 11월 20일 7:37
편집: mallela ankamma rao 2024년 11월 20일 7:38
Thank you for your respense sir. After modification, I run the code. Still I got the same error sir. I request you sir if possible please run the code and remove the error sir. Since two days, I tried sir but i did not get graph sir. This graph is necesssary for research article sir.
ERROR:
Error using varm/estimate (line 155)
The value of 'Y' is invalid. Expected Responses to be an array with number of columns equal to 1.
Error in plots (line 144)
fitModel = estimate(model, dataForVAR'); % Fit the model (data needs to be transposed)
CODE:
home
clear all
close all
%% Step 1: Load data
data = xlsread('DATA.xlsx');
time = double(data(1:400,1)); % Time column (1st column)
inf_data = double(data(1:400,2)); % Infected data column (2nd column)
d2 = double(data(1:400,3)); % Additional data if necessary
%% Step 2: Prepare data for VAR
lags = 2; % Number of lags
dataForVAR = [inf_data(1:end-lags); inf_data(lags+1:end)];
%% Step 3: Fit the VAR model
model = varm(1, lags); % 1 sequence of variables, 2 lags
fitModel = estimate(model, dataForVAR'); % Fit the model (data needs to be transposed)
%% Step 4: Forecasting using the VAR model
summarize(fitModel)
numForecasts = 100; % Number of steps to forecast
% Prepare the presample data (last lags data)
presampleData = dataForVAR(end-lags+1:end, :); % Matrix format for VAR forecast
% Forecast for the next 10 time steps
YForecast = forecast(fitModel, numForecasts, presampleData);
% Rescale the forecasted data (if needed)
forecastData = YForecast(:, 1); % First column is the forecast for the 'inf_data' series
%% Step 5: Plot the results
forecastTime = time(end) + (1:numForecasts); % Forecast time points
% Create a new figure for plotting
figure;
hold on;
plot(time, inf_data, 'b-', 'LineWidth', 1.5, 'DisplayName', 'Actual Data'); % Actual data
plot(forecastTime, forecastData, 'r-', 'LineWidth', 1.5, 'DisplayName', 'Forecasted Data'); % Forecasted data
xlabel('Time', 'FontSize', 14);
ylabel('Infected Cases', 'FontSize', 14);
title('Forecasting with VAR Model', 'FontSize', 16);
legend('Location', 'Best');
grid on;
hold off;
  댓글 수: 2
Pavl M.
Pavl M. 2024년 11월 20일 7:54
편집: Pavl M. 2024년 11월 20일 8:12
Why, for what You invoked
estimate(model, dataForVAR'); with dataForVAR transposed (' sign)?
while at the time of your latest comment I've already updated(corrected) the my answer:
estimate(model, dataForVAR);
after I had constructed the correct column vector:
dataForVAR = [inf_data(1:end-lags);inf_data(lags+1:end)];
which is a 1 column vector and estimate "eats" 1 column vector.
With no transposed version, run it what you get?
I think you used un-updated versions.
I corrected it initially:
That is your dataForVAR in green.
Are you sure about it. It contains 1 peak, gaussian-like distribution and seasonable(periodic) components.
What is your time scale/units? (start date, end date?)
I think the data to modify to
Human operator can analyse and predict, the best forecast by natural intelligence on this data is that it will further grow up(uptrend) till some second following peak then will go downtrend.
The dataForVAR so far is more like for evaluation of how varm(...) estimated the underlying model and for forecast m on it ongoing construction of the data...
Let me kow which more help is required? It should be OK now and in the future.
Please run it and let me know what you get, there are a few very valued R&D workstreams to proceed and continue furthermore.
mallela ankamma rao
mallela ankamma rao 2024년 11월 20일 8:08
편집: Walter Roberson 2024년 11월 22일 22:21
Thank you for your response sir. Yes sir the graph is very good. I want that same graph sir. But when irun the code i got the error sir. My matlab version is R2021b.
CODE:
home
clear all
close all
%% Step 1: Load data
data = xlsread('DATA.xlsx');
Dlength = 400;
time = double(data(1:Dlength,1)); % Time column (1st column)
inf_data = double(data(1:Dlength,2)); % Infected data column (2nd column)
d2 = double(data(1:Dlength,3));
%% Step 2: Prepare data for VAR
% Since we are using univariate data, we treat it as a single-variable time series
% Create lagged data (you can modify the lag length)
lags = 2; % Number of lags
dataForVAR = [inf_data(1:end-lags);inf_data(lags+1:end)];
%% Step 3: Fit the VAR model
model = varm(1,lags); % 2 lags, 1 sequence of variables
fitModel = estimate(model,dataForVAR); % Fit the model
%Identified model whose output is to be forecasted, specified as one of the following:
%Linear model — idpoly, idproc, idss, idtf, or idgrey
%Nonlinear model — idnlgrey, idnlhw, or idnlarx
%If a model is unavailable, estimate sys from PastData using commands
% such as nlarx, and ssest.
%% Step 4: Forecasting using the VAR model
summarize(fitModel)
numForecasts = 10; % Number of steps to forecast
presampleData = dataForVAR(end-lags+1:end,:); % The last lags data as presample
dataForVAR2 = inf_data(1:end);
%opt = forecastOptions('InitialCondition','e'); % Add more options
%opt.InputOffset = 0;
YForecast = forecast(fitModel,presampleData,numForecasts);
%or try using the estimated model and in-sample data as presample observations.
YForecast2 = forecast(fitModel,dataForVAR,numForecasts);
YForecast3 = forecast(fitModel,dataForVAR2,numForecasts);
% Rescale the forecasted data (if needed)
forecastData = YForecast(:, 1); % First column is the forecast for the 'inf_data' series
forecastData2 = YForecast2(:,1);
forecastData3 = YForecast(:,2);
forecastData4 = YForecast2(:,2);
forecastData5 = YForecast3(:,1);
forecastTime = time(end)+(1:numForecasts); % Forecast time points
% Step 5: Plot the results
%% Visualize anticipative foreseeing:
figure;
hold on;
plot(time,inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastTime,forecastData,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on presampled input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
figure
plot(inf_data,'b-','LineWidth',1.5,'DisplayName','Actual Data'); % Actual data
plot(forecastData5,'r-','LineWidth',1.5,'DisplayName','Forecasted Data'); % Forecasted data
xlabel('Time','FontSize',14);
ylabel('Infected Cases','FontSize',14);
title('Forecasting with VAR Model on original input','FontSize',16);
legend('Location','Best');
grid on;
hold off;
ERROR:
Error using varm/forecast (line 195)
The value of 'numperiods' is invalid. Expected forecast horizon to be a scalar.
Error in plots (line 125)
YForecast = forecast(fitModel,presampleData,numForecasts);

댓글을 달려면 로그인하십시오.


Pavl M.
Pavl M. 2024년 11월 20일 8:54
편집: Pavl M. 2024년 11월 20일 9:05
It's still OK, quite smooth and not problematic to solve complete and resolve in full.
There are 2 different forecast functions with the same name:
and
where Mdl is the system and Y0 is your presample.
One in Econometrics toolbox and 1 in system identification toolbox. In econometrics they assume by default ARIMA and GARCH composite model.
System identification is better, since it covers more real world cases(more general). While forecast in Econometrics mainly fitted for ARIMAX model. That 's why even before I putted in my answer the hint to try other estimators of model, in addition to varm.
I know why your system invoked the forecast from Econometrics. That is mechanism of function dispatch in TCE, identified it due to the input Mdl of type varm.
I assumed the first one as most reasonable per se.
It's still ok, the solution is just swap the tail 2 function arguments places.
Before R2023 version they may be calling the Econometrics 1 by default
Check and let me know.
Which toolboxes do you have which you will be needing?
Swap presampleData and numForecasts places in it.
What on TCE R2024b?
If on R2021b value of 'numperiods' Expected forecast horizon to be a scalar.
(line 121)
YForecast = forecast(fitModel,presampleData,numForecasts);
Try to swap parameters
numForecasts is the predictionHorizon, which is scalar
numForecasts = 10;
I think I esablished robust trust. Let's build solid understanding. Will you be needing it in R and Python as well?
Turst me.
I'll be happy to help, to debug your code.
I think now you have succeeded to launch it and get intial results.
What do you get now?
  댓글 수: 8
mallela ankamma rao
mallela ankamma rao 2024년 11월 22일 14:03
편집: mallela ankamma rao 2024년 11월 22일 14:08
Good evening, Pavl M Sir,
I hope this message finds you well. I truly appreciate your response, and I wholeheartedly accept your answer. I also attempted to cast a vote to acknowledge your assistance, but I could not locate the voting option.
Recently, I updated my MATLAB version from R2021b to R2024b. Upon running the DatumModeling2.m script, I noticed that the graphs I generated differ from those presented in your work.
In your graphs, the observed data and simulation data (forecasting data) are seamlessly displayed from day 1 to day 400. However, in my graph, the simulation data (forecasting data) starts appearing only from day 400 to day 500.
If possible, I kindly request your guidance or an updated version of the code to ensure that the forecasting data aligns correctly and is displayed consistently from day 1 to day 400, as shown in your original graphs.
Thank you for your time and support, sir.
Pavl M.
Pavl M. 2024년 11월 26일 16:25
OK, of course I can do it, kindly contact me more explain which specific particular gist, algorithm to rethink,
then which implementations to resolve. My DatumModeling2.m script works fine.
Alpha) Why you have not contacted me before nor at Skype nor by email so long time as I requested to contact me via Skype, email, phone, only via secure, safe, protected by robust encryption channels (Kindly gather investment and I will improve my link upon necessary financial aid is provided to me, it is not early to ask for money, I have been doing many significant Ph.D. and M.Sc. works before indeed, not to forget, RAM and ROM). Don't forget no free lunch theorem. Investment will cement focus. It is also like how reinforcement learning agens as I do do in rewards driven way.If I do not receive I switch to other tasks with hope to get paid doing.
Which built-in function you need to use? In this specific post, the solution is in which forecastData forecastData2 or forecastData5 you observed skew in display (from day 400 to day 500)?Which morphism to build for you in case built-in TCE NCE MPPL functions to fit to your more specific requirements. Sure, I saw and interested in many of your high level postdoc works with multivariate long differential equations, modeling and shaping nature, optimal control domain.
Contact me more where I told you Sir/Madam.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Vector Autoregression Models에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by