Linear Regression gives NaN
49 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John Ziggs
el 20 de Mayo de 2021
Comentada: Star Strider
el 20 de Mayo de 2021
Hi,
I am trying to do and plot a linear regression for the dataset attached. I tried following examples online, but I am getting NaN. This is my code and my data is attached.
clc
close all
clear all
%% Map
filename = 'TV_NYMA';
[num,string,vt] = xlsread(filename);
Year = num(:,1);
County = string(:,2);
VOC = num(:,3);
NOx = num(:,4);
CO = num(:,5);
PM25 = num(:,6);
Lat = num(:,7);
Lon = num(:,8);
%%
standard_NOx = normalize(NOx);
standard_VOC = normalize(VOC);
figure
scatter(standard_NOx,standard_VOC)
title('NOx vs VOC')
xlabel('NOx emissions standardized')
ylabel('VOC emissions standardized')
%%
X = [ones(length(standard_NOx),1) standard_NOx]
b = X\standard_VOC
regression_line = [ones(size(standard_NOx,1),1) standard_NOx]*b
I was wondering what am I doing wrong.
Thanks.
2 comentarios
dpb
el 20 de Mayo de 2021
I didn't download the data, but why not
b=polyfit(standard_NOx,standard_VOC,1);
yhat=polyval(b,[min(standard_NOX) max(standard_NOX)]);
or, if have one of Statistics or Curve Fitting Toolboxes, there are other higher-level routines as well...
Just out of curiosity, did you look at what was returned for the coefficients matrix? Was it NaN there, already? IF so, there's probaby a NaN in the mix in the data somewhere.
Respuesta aceptada
Star Strider
el 20 de Mayo de 2021
There are 8 NaN values in those variables.
Eliminate them and it works —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/625018/TV_NYMA.xlsx','VariableNamingRule','preserve')
% Nox_Nan = nnz(isnan(T1.Nox))
% VOC_NaN = nnz(isnan(T1.VOC))
T1 = T1(~[isnan(T1.Nox) & isnan(T1.VOC)],:)
standard_NOx = normalize(T1.Nox);
standard_VOC = normalize(T1.VOC);
X = [ones(length(standard_NOx),1) standard_NOx];
b = X\standard_VOC
regression_line = [ones(size(standard_NOx,1),1) standard_NOx]*b;
figure
plot(standard_NOx, standard_VOC, 'p')
hold on
plot(standard_NOx, regression_line, '-r')
hold off
grid
.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Descriptive Statistics en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!