The logical index at position 1 contains a true value outside the array boundary (Error tsVAR/estimate (line 826))

4 views (last 30 days)
The logical index at position 1 contains a true value outside the array boundary
位置 1 处的逻辑索引包含一个在数组边界之外的 true 值。
Error tsVAR/estimate (line 826)
出错 tsVAR/estimate (第 826 行)
Ei = E(s == iii,:);
出错 TVAR_v1 (第 92 行)
EstMdl = estimate(tMdl,tt,Ye(:,1:6), Y0 = Y00(:,1:6),...
p = 4;
%%
% 加载数据
data = readtable(file_var);
year_quarter = data{:,1};
% 分解为年份和季度部分
year = floor(year_quarter);
quarter = round((year_quarter - year) * 4 + 1);
% 将季度转换为对应的月份
month = (quarter - 1) * 3 + 1;
% 创建一个 datetime 对象
dt = datetime(year, month, 1);
data{:,'time'} = dt;
data = table2timetable(data);
Y = data(:, vars);
%%
% 滤波用于判断经济周期
% one-sided HP filter
raw_data = Y; % 需滤波数据
lambda = 1600;
[Yt,Yc] = hpfilter(raw_data,lambda,FilterType="one-sided");
% 差分方法 去除趋势
Yd = diff(Y);
% 添加状态
Yd{:,"state"} = [NaN; Yc{1:end-1,"lrgdp"}];
%%
% 划分预样本和估计样本
maxp = 4;
T = height(Yd);
eT = T - maxp;
idxpre = 1:maxp;
idxest = (maxp+1):T;
Y0 = Yd(idxpre,:);
Ye = Yd(idxest,:);
%% 绘图与检验
pic = Yd(:,1:6);
tiledlayout(2,3)
for j = 1:width(pic)
nexttile
plot(pic.time,pic{:,j})
title(pic.Properties.VariableNames(j))
end
% ADF检验
% True 则可以拒绝原假设
[h, pValue] = adftest(Ye);
%% 构建模型
% 基础VAR构建
numseries = numel(vars);
Mdls = varm(Constant=NaN(numseries,1),Lags=[1 2 3 4], ...
SeriesNames=vars);
% threhold
tt = threshold(0,Type = "discrete");
tt.StateNames = ["Recession" "Expansion"];
% 构建tsVAR
Mdls2 = [Mdls Mdls];
tMdl = tsVAR(tt,Mdls2);
% 转换数据类型
Y00 = table2array(Y0);
Ye = table2array(Ye);
%%
EstMdl = estimate(tMdl,tt,Ye(:,1:6), Y0 = Y00(:,1:6),...
Z=Ye(:,1),Type="exogenous",IterationPlot=false);

Accepted Answer

MULI
MULI on 20 Feb 2025
Edited: MULI on 20 Feb 2025
Hi @pula,
  • The issue arises when your code is trying to access elements outside the array limits in "tsVAR/estimate.m (line 826)". This is likely due to "s == iii", where 's' or 'iii' has unexpected values.
  • Try checking "unique(s)" to see what is inside and make sure "tt.StateNames" matches the expected states. Also, if 'Ye' has missing values, you can remove them as shown below:
Ye = table2array(Ye);
Ye(any(isnan(Ye), 2), :) = []; % Remove NaN rows
  • If the issue continues, you can try adding "disp(s)" and "disp(iii)" before that line to check for any invalid values.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!