Error finding Linear Regression with polyfit and \
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JMG
am 4 Aug. 2021
Kommentiert: Star Strider
am 5 Aug. 2021
I am trying to plot the linear regression of my data throughout time however I have tried a few different methods but none of them having been working for me.
load data.mat;
y=data.MEAN_TEMPERATURE;
x=[1:numel(y)].';
p=polyfit(x,y,1)
b1=x\y
plot(x,y);
When I try using the plotfit or the \ method I get NaN as outputs. However when I plot it in a graph and then use the basic fitting tool I am able to get:
p1 = 0.00011312
p2 = 5.0801
for
y = p1*x + p2
So I am not sure what I am doing wrong here because clearly a linear regression can be found.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 4 Aug. 2021
There is a NaN value in the temperature data.
Try this:
LD = load('data.mat');
data = LD.data;
x = data.LOCAL_DATE;
xdn = datenum(x); % Copnvert To 'datenum' For Regression
y = data.MEAN_TEMPERATURE;
yv = ~ismissing(y); % Find Missing Values
[p,S,mu] = polyfit(xdn(yv), y(yv), 1); % Use Scaling & Centring
yfit = polyval(p,xdn(yv),S,mu); % Use Scaling & Centring
figure
plot(xdn(yv), y(yv))
hold on
plot(xdn(yv), yfit, '-r')
hold off
grid
datetick('x', 'yyyy', 'keepticks','keeplimits')
xlabel('Date')
ylabel('Mean Temperature (°C)')
A linear regression is not going to tell much, other than that the mean termperatur is increasing slightly although not significantly over time.
This:
mdl = fitlm(xdn, y)
produces:
mdl =
Linear regression model:
y ~ 1 + x1
Estimated Coefficients:
Estimate SE tStat pValue
__________ __________ ________ _______
(Intercept) -76.185 124.8 -0.61044 0.54161
x1 0.00011312 0.00017332 0.65267 0.51402
Number of observations: 3348, Error degrees of freedom: 3346
Root Mean Squared Error: 9.7
R-squared: 0.000127, Adjusted R-Squared: -0.000172
F-statistic vs. constant model: 0.426, p-value = 0.514
The parameters that fitlm produces are not the same as those polyfit produces, since here polyfit uses centring and scaling (my choice, not absolutely necessary).
.
2 Kommentare
Weitere Antworten (1)
Rik
am 4 Aug. 2021
Removing the NaNs will do the trick. Although I'm not sure this data should have a linear fit.
fn=websave('data.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/703197/data.mat');
S=load(fn);
data=S.data;
y=data.MEAN_TEMPERATURE;
y(isnan(y))=[];
x=[1:numel(y)].';
p=polyfit(x,y,1)
b1=x\y
plot(x,y)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!