How to make a linear regression line?

Hi.
I have a data set with year and wind direction. I have used the plot command to make a figure but I also want to include a linear regression line. Is there an easy way to do this in matlab? Here is my data set:
1973 203
1974 124
1975 223
1976 177
1977 166
1978 104
1979 95
1980 94
1981 26
1982 135
1983 179
1984 210
1985 133
1986 153
1987 150
1988 189
1989 149
1990 184
1991 156
1992 133
1993 173
1994 211
1995 97
1996 92
1997 37
1998 200
1999 196
2000 195
2001 189
2002 155
2003 145
2004 149
2005 195
2006 168
2007 201
2008 173
2009 168
2010 192
2011 193
2012 216
2013 157
Helps is much appreciated!
Kristine

 Respuesta aceptada

Kristine
Kristine el 16 de Jun. de 2014

0 votos

I get:
Error executing Basic Fitting Output argument "pp" (and maybe others) not assigned during call to "/home/mano/kt31/Documents/MATLAB/R2014a/toolbox/matlab/graph2d/private/bfitcalcfit.m>fitcalcfit".

2 comentarios

Sean de Wolski
Sean de Wolski el 16 de Jun. de 2014
Editada: Sean de Wolski el 16 de Jun. de 2014
Hi Kristine,
I think you might have a corrupted path or installation (all of your errors thus far are wierd).
First try this to set everything straight:
restoredefaultpath
rehash toolboxcache
If it works, save this new state
savepath
Kristine
Kristine el 17 de Jun. de 2014
Thank you! I got it to work:)

Iniciar sesión para comentar.

Más respuestas (5)

Sean de Wolski
Sean de Wolski el 16 de Jun. de 2014

5 votos

If you go to "Tools" -> "Basic fitting" in the figure window you can add the line and the equation:
Star Strider
Star Strider el 16 de Jun. de 2014

1 voto

Did you take a close look at your data?
There are two significant shifts from the usual southerly-southwesterly direction to easterly-northeasterly between 1977-82 and again from 1994-8. A linear fit is not going to tell you anything significant about your data, and given the direction shifts, might be misleading.
I don’t know what I’d recommend you do in your data analysis because I don’t know what you want to demonstrate. But a linear fit definitely isn’t it.

4 comentarios

Kristine
Kristine el 17 de Jun. de 2014
I'm sorry, I did not define my x and y correctly. x is year, and y is trade wind count, so number of trade wind days in a year. I need a linear regression line to see if there is a slight decrease or increase in trade wind days.
I appreciate the clarification, but that doesn’t change my interpretation of your data. When I did a linear regression on your data (that I called YD) with polyfit and analysed the parameters with the FEX File polyparci,
[p,S,mu] = polyfit(YD(:,1),YD(:,2),1);
CI = polyparci(p,S);
PCI = [p; CI]
I got:
PCI =
11.9207e+000 158.1707e+000
-10.2221e-003 146.3862e+000
23.8516e+000 169.9553e+000
so the slope (the first parameter, 11.92) isn’t significant, and the interpretation is that there has been no change over that time. (Only the intercept, 158.2, is significantly different from zero.) You might want to see if your data correlate with something else — north Pacific sea surface temperatures, ENSO, the solar cycle, or some such. There is no specific linear trend with respect to time otherwise that I can see, and the regression data support that.
I believe there is interesting information in your data. You simply haven’t found it yet. You definitely have to explain those two multi-year declines in trade wind days.
Kristine
Kristine el 17 de Jun. de 2014
I got it to work. It's my professor that wanted me to do this, so he could update a paper written a couple of years ago. But I agree, its very interesting with the two significant declines in trade wind days.
Star Strider
Star Strider el 17 de Jun. de 2014
I don’t know what the hypothesis of the study is, but if it is to show that there is no linear change in trade wind days, that works. The trend is not significantly different from zero. It just seems to ignore the really interesting stuff.

Iniciar sesión para comentar.

Shashank Prasanna
Shashank Prasanna el 16 de Jun. de 2014

0 votos

where, n = 1
3) If you have the statistics toolbox: http://www.mathworks.com/help/stats/fitlm.html
Each page has examples

3 comentarios

Kristine
Kristine el 16 de Jun. de 2014
I have tried:
>> tbl=table(dato,trade_count);
>> mdl=fitlm(tbl,'linear')
But all I get is errors:
Error using qr LAPACK loading error: dlopen: cannot load any more object with static TLS
Error in LinearModel>lsfit (line 955) [Q,R,perm] = qr(X,0);
Error in LinearModel/fitter (line 660) [model.Coefs,model.MSE,model.CoefficientCovariance,model.R,model.Qy,model.DFE,model.Rtol] ...
Error in classreg.regr.FitObject/doFit (line 220) model = fitter(model);
Error in LinearModel.fit (line 863) model = doFit(model);
Error in fitlm (line 117) model = LinearModel.fit(X,varargin{:});
There is something wrong with your MATLAB path. You must not get errors with LAPACK. I am going to suggest what sean suggested below:
restoredefaultpath
rehash toolboxcache
And then run the same code below:
mdl=fitlm(dato,trade_count,'linear')
plot(mdl)
I get the following results:
Estimate SE tStat pValue
________ _______ _______ _______
(Intercept) -1825.1 1178.1 -1.5491 0.12942
x1 0.99512 0.59113 1.6834 0.10028
Kristine
Kristine el 17 de Jun. de 2014
Yes, you were right. There was something wrong with my matlab path. I got it to work, so thank you very much!:)

Iniciar sesión para comentar.

Jos (10584)
Jos (10584) el 16 de Jun. de 2014

0 votos

The function LSLINE will add a linear regression line to a plot.

1 comentario

Kristine
Kristine el 16 de Jun. de 2014
Not in this case, I just get the error:
Warning: No allowed line types or scatterplots found. Nothing done. > In lsline at 45

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 17 de Jun. de 2014

0 votos

See my polyfit() regression demo, attached. Adapt it to use order 1 for linear, and to use your specific data arrays.

Categorías

Más información sobre Descriptive Statistics en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Jun. de 2014

Comentada:

el 9 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by