MATLAB Answers

Error finding Linear Regression with polyfit and \

조회 수: 4(최근 30일)
JMG
JMG 2021년 8월 4일
댓글: Star Strider 2021년 8월 5일
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.

채택된 답변

Star Strider
Star Strider 2021년 8월 4일
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
Star Strider
Star Strider 2021년 8월 5일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Rik
Rik 2021년 8월 4일
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)
p = 1×2
0.0001 5.0795
b1=x\y
b1 = 0.0024
plot(x,y)
  댓글 수: 1
JMG
JMG 2021년 8월 5일
Thank you this also worked!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by