How to make a linear regression line?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (5)
Sean de Wolski
am 16 Jun. 2014
5 Stimmen
If you go to "Tools" -> "Basic fitting" in the figure window you can add the line and the equation:

1 Kommentar
Abhishek Chakraborty
am 9 Mär. 2020
How to get the R^2 value?
Star Strider
am 16 Jun. 2014
1 Stimme
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 Kommentare
Kristine
am 17 Jun. 2014
Star Strider
am 17 Jun. 2014
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
am 17 Jun. 2014
Star Strider
am 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.
Shashank Prasanna
am 16 Jun. 2014
0 Stimmen
where, n = 1
Each page has examples
3 Kommentare
Kristine
am 16 Jun. 2014
Shashank Prasanna
am 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
am 17 Jun. 2014
Image Analyst
am 17 Jun. 2014
0 Stimmen
See my polyfit() regression demo, attached. Adapt it to use order 1 for linear, and to use your specific data arrays.
1 Kommentar
Kristine
am 17 Jun. 2014
Kategorien
Mehr zu Descriptive Statistics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!