How to make a linear regression line?

89 ビュー (過去 30 日間)
Kristine
Kristine 2014 年 6 月 16 日
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

採用された回答

Kristine
Kristine 2014 年 6 月 16 日
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 件のコメント
Sean de Wolski
Sean de Wolski 2014 年 6 月 16 日
編集済み: Sean de Wolski 2014 年 6 月 16 日
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 2014 年 6 月 17 日
Thank you! I got it to work:)

サインインしてコメントする。

その他の回答 (5 件)

Sean de Wolski
Sean de Wolski 2014 年 6 月 16 日
If you go to "Tools" -> "Basic fitting" in the figure window you can add the line and the equation:
  1 件のコメント
Abhishek Chakraborty
Abhishek Chakraborty 2020 年 3 月 9 日
How to get the R^2 value?

サインインしてコメントする。


Star Strider
Star Strider 2014 年 6 月 16 日
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 件のコメント
Kristine
Kristine 2014 年 6 月 17 日
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 2014 年 6 月 17 日
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.

サインインしてコメントする。


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

サインインしてコメントする。


Jos (10584)
Jos (10584) 2014 年 6 月 16 日
The function LSLINE will add a linear regression line to a plot.
  1 件のコメント
Kristine
Kristine 2014 年 6 月 16 日
Not in this case, I just get the error:
Warning: No allowed line types or scatterplots found. Nothing done. > In lsline at 45

サインインしてコメントする。


Image Analyst
Image Analyst 2014 年 6 月 17 日
See my polyfit() regression demo, attached. Adapt it to use order 1 for linear, and to use your specific data arrays.
  1 件のコメント
Kristine
Kristine 2014 年 6 月 17 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGaussian Process Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by