MATLAB Answers

Error in nonlinear regression

17 views (last 30 days)
I am trying to make a nonlinear regression model of the attached csv file. I used the following code:
if true
X = [sst,at];
y = sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
beta0 = [100 100 100 100 100];
mdl = fitnlm(X,y,modelfun,beta0)
% code
I am getting the following errors:
Error using internal.stats.parseArgs (line 42) Wrong number of arguments.
Error in (line 1385) internal.stats.parseArgs(paramNames, paramDflts, otherArgs{:});
Error in fitnlm (line 99) model =,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


Image Analyst
Image Analyst on 12 Oct 2018
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?
Keegan Carvalho
Keegan Carvalho on 13 Oct 2018
@Image Analyst I wanted to develop a nonlinear relationship between sea level with sea surface temp (sst) and air temp (at). So I wanted to get an equation like sea ~ b1 + b2(sst)^b3 +b4(at)^b5

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 10 Oct 2018
You are not extracting your table variables correctly.
This works (extracting the data from your table):
X = [data.sst,];
y = data.sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
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) + ...
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.


Show 3 older comments
Star Strider
Star Strider on 12 Oct 2018
Choose a set that seems reasonable. With some models, there are ways to approximate the signs and magnitudes of initial estimates. For other models, not. (I was just experimenting with the model you posted, and it bears no relations to climatology or reality. There is certainly nothing to recommend it.)
When I ran your initial data, the only predictor that was reasonable was the year. There is probably too much noise (or not enough data) otherwise to demonstrate a relationship. That is simply reality.
Keegan Carvalho
Keegan Carvalho on 13 Oct 2018
Thank you for your advice @Star Strider. If that is the case, then is there any other way to determine a relationship between the variables with sea level? Or should I do a one-on-one relationship instead? i.e. sea~sst and sea~at?
Star Strider
Star Strider on 13 Oct 2018
My pleasure.
This is not my area of expertise. I suggest you do an Interweb search for an appropriate model.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 13 Oct 2018
You should use scatteredInterpolant(). Write back if you can't figure it out.

  1 Comment

Star Strider
Star Strider on 13 Oct 2018
Keegan Carvalho has a complete data set, so interpolation would likely not gain anything.

Sign in to comment.

Image Analyst
Image Analyst on 13 Oct 2018
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 =;
% Extract various components.
%================================ 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);
% 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.


Show 8 older comments
Walter Roberson
Walter Roberson on 16 Oct 2018
You have 17 data points. To have any useful statistical significance, you need at least 5 points per degree of freedom. I figure that you therefore cannot justify any model with more than 3 degrees of freedom, and that you might be able to say "suggestive" with 4 degrees of freedom, but beyond that you should having nothing to say.
When I look at the image that Image Analyst produced for you, it is pretty clear that you cannot meaningfully create it with two degrees of freedom, and you would be pretty hard pressed to create it with four degrees of freedom.
I think you need to let go of the idea that you have enough data for a meaningful analysis.
Keegan Carvalho
Keegan Carvalho on 22 Oct 2018
Thank you Star Strider and Walter for your help and guidance. Much appreciated. I can say that I atleast learned more than I asked. Cheers!
Keegan Carvalho
Keegan Carvalho on 27 Oct 2018
Thank you Image Analyst for your help and guidance

Sign in to comment.