How to calculate error in trend?

I have estimated slope from least square method. Now I need to calculate error of this slope at 95% confidence interval. kindly suggest me a MATLAB code in this regard. I'll be thankful remaining you.

1 Comment

Brendan Hamm
Brendan Hamm on 12 May 2015
Edited: Brendan Hamm on 12 May 2015
Are you using the fitlm function in the Statistics and Machine Learning Toolbox or polyfit/ polyval? Please provide an example of what you have done, so that it is easier to help you.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 12 May 2015
If you used polyfit, I strongly recommend polyparci!
If you used another method, you need to specify the function or other code you used. It is not possible to answer your Question otherwise.

4 Comments

I am writing here a code.
if true
y = [1:120]';
x = time_series';
time = y'; % representing each month of the data
omega = (2*pi)/12;
S = [(time*0+1)',(sin(time.*omega))', (cos(time.*omega))',(sin(time.*2*omega))',(cos(time.*2*omega))']; %, time'
beta = (S'*S)\(S'*(x));
E_model = S*beta;
amp1 = sqrt((beta(2)).^2 + (beta(3)).^2); % Amplitude of first order harmonic
amp2 = sqrt((beta(4)).^2 + (beta(5)).^2); % Amplitude of second order harmonic
%amp3 = sqrt((beta(6)).^2 + (beta(7)).^2); % Amplitude of third order harmonic
E_res = x - E_model;
E_rms = sqrt(nanmean(E_res.^2));
%disp('Fit of Annual and Semi-Annual Give a trend'); beta(6)
% Robustfit %
[brob_E,stats] = robustfit(time,x);%brob_discharge(2) is the slope
[regls_E, bint_E,r, rint, stats_E]=regress(x, [ones(length(time),1) time'], 0.05);
I don’t have much experience with robustfit since I haven’t used it in a while. I would imagine the ‘p’ value returned in the ‘E_stats’ structure (in your code) would provide the confidence intervals. Confidence intervals are all you routinely need, since you want to know if the parameters are significantly different from zero.
In your regress call, the ‘bint_E’ variable it returns (in your code) would contain the same information.
The results from robustfit and regress will be different, because they use different algorithms and actually do different regressions.
Yes you are right 'regls_E' returns slope value of time series and 'stats_E' returns p-value of this slope value. I need to calculate error value of this slope value. How would I coding in order to get error value ?
Unfortunately, regress is not going to give you the covariance matrix, and you need it to compute the standard errors. I would use fitlm instead, since it will give you the output you want.
If you must use regress, you can calculate the standard errors as:
x = randi(10, 10, 1); % Create Data
y = randi(100, 10, 1); % Create Data
X = [ones(10,1) x]; % Design Matrix
[b,bint,r,rint,stats] = regress(y,X); % Estimate Parameters
rvar = r'*r/(size(X,1)-length(b)); % Residual Variance
covb = rvar./(X'*X); % Covariance Matrix
SE = sqrt(diag(covb)); % Standard Errors
That would be the same for the ‘covb’ field of the ‘stats’ structure returned by robustfit.

Sign in to comment.

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 12 May 2015

Commented:

on 13 May 2015

Community Treasure Hunt

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

Start Hunting!