How to make a linear regression line?

102 views (last 30 days)
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

Accepted Answer

Kristine
Kristine on 16 Jun 2014
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 Comments
Sean de Wolski
Sean de Wolski on 16 Jun 2014
Edited: Sean de Wolski on 16 Jun 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 on 17 Jun 2014
Thank you! I got it to work:)

Sign in to comment.

More Answers (5)

Sean de Wolski
Sean de Wolski on 16 Jun 2014
If you go to "Tools" -> "Basic fitting" in the figure window you can add the line and the equation:

Star Strider
Star Strider on 16 Jun 2014
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 Comments
Kristine
Kristine on 17 Jun 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 on 17 Jun 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.

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 16 Jun 2014
where, n = 1
3) If you have the statistics toolbox: http://www.mathworks.com/help/stats/fitlm.html
Each page has examples
  3 Comments
Shashank Prasanna
Shashank Prasanna on 17 Jun 2014
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 on 17 Jun 2014
Yes, you were right. There was something wrong with my matlab path. I got it to work, so thank you very much!:)

Sign in to comment.


Jos (10584)
Jos (10584) on 16 Jun 2014
The function LSLINE will add a linear regression line to a plot.
  1 Comment
Kristine
Kristine on 16 Jun 2014
Not in this case, I just get the error:
Warning: No allowed line types or scatterplots found. Nothing done. > In lsline at 45

Sign in to comment.


Image Analyst
Image Analyst on 17 Jun 2014
See my polyfit() regression demo, attached. Adapt it to use order 1 for linear, and to use your specific data arrays.

Community Treasure Hunt

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

Start Hunting!