Error in nonlinear regression
3 次查看(过去 30 天)
显示 更早的评论
I am trying to make a nonlinear regression model of the attached csv file. I used the following code:
if true
data=readtable('ban1.csv')
sea=data(:,1)
sst=data(:,2)
at=data(:,3)
X = [sst,at];
y = sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(X,y,modelfun,beta0)
% code
end
I am getting the following errors:
Error using internal.stats.parseArgs (line 42) Wrong number of arguments.
Error in NonLinearModel.fit (line 1385) internal.stats.parseArgs(paramNames, paramDflts, otherArgs{:});
Error in fitnlm (line 99) model = NonLinearModel.fit(X,varargin{:});
Would be grateful if someone can help me, if possible with code. I would also like to know where I am going wrong.
Thank you
2 个评论
Image Analyst
2018-10-12
Which is x and which is y? Why are you having 2 x but only 1 corresponding y? What do you want along the x/horizontal/independent axis and what do you want along the y/vertical/dependent axis?
采纳的回答
Star Strider
2018-10-10
You are not extracting your table variables correctly.
This works (extracting the data from your table):
X = [data.sst, data.at];
y = data.sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(X,y,modelfun,beta0)
and so does this (using your table, and simply rearranging its columns to be compatible with what fitnlm expects):
new_table = data(:, [2 3 1]);
modelfun = @(b,x)b(1) + b(2).*x(:,1).^b(3) + ...
b(4).*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(new_table,modelfun,beta0)
The ‘carbig’ model is likely not appropriate for your sea level and temperature data, so I am not surprised that the model fails, even though the code now runs without error.
6 个评论
Star Strider
2018-10-13
My pleasure.
This is not my area of expertise. I suggest you do an Interweb search for an appropriate model.
更多回答(2 个)
Image Analyst
2018-10-13
You should use scatteredInterpolant(). Write back if you can't figure it out.
1 个评论
Star Strider
2018-10-13
Keegan Carvalho has a complete data set, so interpolation would likely not gain anything.
Image Analyst
2018-10-13
Try using scatteredInterpolant(). Below is a complete demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
format bank
% Read in data.
data=importdata('ban1.csv')
data = data.data;
% Extract various components.
sea=data(:,1)
sst=data(:,2)
at=data(:,3)
%================================ MAIN PART RIGHT HERE ==============================================
% Make the scattered interpolant.
F = scatteredInterpolant(sst, at, sea)
% Get a grid of points at every pixel location in the RGB image.
resolution = 300; % Number of points across that you want ot evaluate it at.
x = linspace(min(sst), max(sst), resolution);
y = linspace(min(at), max(at), resolution);
[xGrid, yGrid] = meshgrid(x, y);
xq = xGrid(:);
yq = yGrid(:);
% Evaluate the interpolant at query locations (xq,yq).
vq = F(xq, yq);
fittedImage = reshape(vq, resolution, resolution);
%================================ END OF MAIN PART ==============================================
imshow(fittedImage, [], 'XData', x, 'YData', y);
axis on;
xlabel('sst', 'FontSize', fontSize);
ylabel('at', 'FontSize', fontSize);
title('sea as a function of sst and at', 'FontSize', fontSize);
colorbar;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% For fun, overlay the known points.
hold on;
plot(sst, at, 'r*', 'MarkerSize', 20, 'LineWidth', 2);

You can use a colormap if you want.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!