Error of Index exceeds matrix dimensions. Please help
1 view (last 30 days)
Show older comments
oluwatayo ogunmiloro
on 21 Apr 2020
Commented: oluwatayo ogunmiloro
on 22 Apr 2020
% 2016 2017 2018 2019
y = [3 2 6 6 % Jan
6 1 10 9 % Feb
2 5 4 0 % Mar
1 7 5 3 % Apr
0 1 0 8 % May
0 3 4 1 % Jun
0 5 7 1 % Jul
2 0 4 0 % Aug
3 2 1 6 % Sep
2 5 3 4 % Oct
1 1 1 4 % Nov
3 2 5 2 ]; % Dec
% Source:personal library
yr = repmat((2016:2019),12,1);
mo = repmat((1:12)',1,12);
time = datestr(datenum(yr(2016:2019),mo(1:12),1));
ts = timeseries(y(:),time,'name','Diabetes Diagnosis in Male Individuals');
ts.TimeInfo.Format = 'dd-mmm-yyyy';
tscol = tscollection(ts);
plot(ts)
%Examine Trend and Seasonality
%This series seems to have a strong seasonal component, with a trend that may be linear
%or quadratic. Furthermore, the magnitude of the seasonal variation increases as
%the general level increases. Perhaps a log transformation would make the seasonal
%variation be more constant. First we'll change the axis scale.
h_gca = gca;
h_gca.YScale = 'log';
%It appears that it would be easier to model the seasonal component on the log scale.
%We'll create a new time series with a log transformation.
tscol = addts(tscol,log(ts.data),'logdiabetes complications');
logts = tscol.logAirlinePassengers;
%Now let's plot the yearly averages, with monthly deviations superimposed.
%We want to see if the month-to-month variation within years appears constant.
%For these manipulations treating the data as a matrix in a month-by-year format,
%it's more convenient to operate on the original data matrix.
t = reshape(datenum(time),12,12);
logy = log(y);
ymean = repmat(mean(logy),12,1);
ydiff = logy - ymean;
x = yr + (mo-1)/12;
plot(x,ymean,'b-',x,ymean+ydiff,'r-')
title('Monthly variation within year')
xlabel('Year')
%Now let's reverse the years and months, and try to see if the year-to-year trend
%is constant for each month.
subplot(1,1,1);
X = [dummyvar(mo(:)) logts.time];
[b,bint,resid] = regress(logts.data,X);
tscol = addts(tscol,X*b,'Fit1')
%Time Series Collection Object: unnamed
%Time vector characteristics
% Start date 01-Jan-2016
% End date 01-Dec-2019
%Member Time Series Objects:
% Diabetes diagnosis
% logDiabetes diagnosis
% Fit1
plot(logts)
hold on
plot(tscol.Fit1,'Color','r')
hold off
legend('Data','Fit','location','NW')
%Based on this graph, the fit appears to be good. The differences between the actual
%data and the fitted values may well be small enough for our purposes.
%But let's try to investigate this some more. We would like the residuals to look
%independent. If there is autocorrelation (correlation between adjacent residuals),
%then there may be an opportunity to model that and make our fit better. Let's create
%a time series from the residuals and plot it
tscol = addts(tscol,resid,'Resid1');
plot(tscol.Resid1)
0 Comments
Accepted Answer
David Welling
on 21 Apr 2020
in line 17 ( time = datestr(datenum(yr(2016:2019),mo(1:12),1)); ), You call array yr(2016:2019). the size of the array is however, 12x4. the index of yr in this line is incorrect. it also has only one dimension, not two.
5 Comments
Walter Roberson
on 22 Apr 2020
You appear to be adding a timeseries named 'logdiabetes complications' but then trying to access a timeseries named 'logdiabetes'
More Answers (0)
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!