Clear Filters
Clear Filters

Line of Best Fit for a time series plot

12 views (last 30 days)
Ethan
Ethan on 29 Nov 2023
Answered: Peter Perkins on 30 Nov 2023
I would like to know how I can use the polyfit function to fit a line to my time series plot.
ts_MinJan = timeseries(Jan_Min,1:41);
ts_MinJan.Name = 'Janurary Min';
ts_MinJan.TimeInfo.Units = 'Year';
subplot(3,4,1), plot(ts_MinJan)
ts_MinFeb = timeseries(Feb_Min,1:41);
ts_MinFeb.Name = 'February Min';
ts_MinFeb.TimeInfo.Units = 'Year';
subplot(3,4,2), plot(ts_MinFeb)
ts_MinMar = timeseries(Mar_Min,1:41);
ts_MinMar.Name = 'March Min';
ts_MinMar.TimeInfo.Units = 'Year';
subplot(3,4,3), plot(ts_MinMar)
ts_MinApr = timeseries(Apr_Min,1:41);
ts_MinApr.Name = 'April Min';
ts_MinApr.TimeInfo.Units = 'Year';
subplot(3,4,4), plot(ts_MinApr)
ts_MinMay = timeseries(May_Min,1:41);
ts_MinMay.Name = 'May Min';
ts_MinMay.TimeInfo.Units = 'Year';
subplot(3,4,5), plot(ts_MinMay)
ts_MinJun = timeseries(Jun_Min,1:41);
ts_MinJun.Name = 'June Min';
ts_MinJun.TimeInfo.Units = 'Year';
subplot(3,4,6), plot(ts_MinJun)
ts_MinJul = timeseries(Jul_Min,1:41);
ts_MinJul.Name = 'July Min';
ts_MinJul.TimeInfo.Units = 'Year';
subplot(3,4,7), plot(ts_MinJul)
ts_MinAug = timeseries(Aug_Min,1:41);
ts_MinAug.Name = 'August Min';
ts_MinAug.TimeInfo.Units = 'Year';
subplot(3,4,8), plot(ts_MinAug)
ts_MinSep = timeseries(Sep_Min,1:41);
ts_MinSep.Name = 'September Min';
ts_MinSep.TimeInfo.Units = 'Year';
subplot(3,4,9), plot(ts_MinSep)
ts_MinOct = timeseries(Oct_Min,1:41);
ts_MinOct.Name = 'October Min';
ts_MinOct.TimeInfo.Units = 'Year';
subplot(3,4,10), plot(ts_MinOct)
ts_MinNov = timeseries(Nov_Min,1:41);
ts_MinNov.Name = 'November Min';
ts_MinNov.TimeInfo.Units = 'Year';
subplot(3,4,11), plot(ts_MinNov)
ts_MinDec = timeseries(Dec_Min,1:41);
ts_MinDec.Name = 'December Min';
ts_MinDec.TimeInfo.Units = 'Year';
subplot(3,4,12), plot(ts_MinDec)

Answers (2)

Mathieu NOE
Mathieu NOE on 30 Nov 2023
hello
I am not working that much with timeseries , but IMHO, you could do the polyfit first on the raw data , otherwise you can still access the data from a timeseries to do the fit then you have to create a new timeserie for the fitted curve to overlay on the plot
demo :
n = 10;
t = (0:n-1)';
x = 1+t/n + 0.1*randn(n,1);
ts1 = timeseries(x,t);
ts1.Name = 'Daily Count';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '01-Jan-2011'; % Set start date.
ts1.TimeInfo.Format = 'mmm dd, yy'; % Set format for display on x-axis.
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1)
% polyfit
% [p,S,mu] = polyfit(t,x,1); % polyfit on raw data
[p,S,mu] = polyfit(t,ts1.Data,1); % polyfit on ts1 data
[xfit,delta] = polyval(p,t,S,mu);
ts2 = timeseries(xfit,t);
ts2.TimeInfo.Units = 'days';
ts2.TimeInfo.StartDate = '01-Jan-2011'; % Set start date.
ts2.TimeInfo.Format = 'mmm dd, yy'; % Set format for display on x-axis.
ts2.Time = ts2.Time - ts2.Time(1); % Express time relative to the start date.
% Plot fit line:
hold on
plot(ts2)

Peter Perkins
Peter Perkins on 30 Nov 2023
There are a bunch of things you might mean by this, but here's one thing you might do.
% set up random data
Time = datetime(1983:2023,1,1,Format="uuuu");
tt = array2timetable(rand(41,12),RowTimes=Time,VariableNames=["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"]);
% fit a quadratic to each month's data, plot along with the raw data
t = 1:length(Time);
tt2 = varfun(@(x) polyval(polyfit(t,x,2),t)',tt);
tt2.Properties.VariableNames = tt.Properties.VariableNames;
stackedplot(tt,tt2)

Categories

Find more on Weather and Atmospheric Science in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!