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 Kommentare

Image Analyst
Image Analyst am 12 Okt. 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 am 13 Okt. 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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 10 Okt. 2018

0 Stimmen

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 Kommentare

Hi @Star Strider I tried the code but I get several warnings:
Warning: Rank deficient, rank = 4, tol = 1.431811e+133. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 4.455183e+132. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 1.351145e+132. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 4.068247e+131. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 1.143089e+131. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 1.842881e+130. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 5.038164e+129. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 5.022077e+129. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Rank deficient, rank = 2, tol = 5.020462e+129. > In nlinfit>LMfit (line 587) In nlinfit (line 284) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the model is insensitive to some of its parameters. That may be because those parameters are not present in the model, or otherwise do not affect the predicted values. It may also be due to numerical underflow in the model function, which can sometimes be avoided by choosing better initial parameter values, or by rescaling or recentering. Parameter estimates may be unreliable. > In nlinfit (line 381) In NonLinearModel/fitter (line 1127) In classreg.regr.FitObject/doFit (line 94) In NonLinearModel.fit (line 1434) In fitnlm (line 99)
Also my results are a bit odd:
Nonlinear regression model: sea ~ b1 + b2*sst^b3 + b4*at^b5
Estimated Coefficients: Estimate SE tStat pValue _______ ___ ____
b1 100 0 Inf 0
b2 1.071e-12 0 Inf 0
b3 99.356 0 Inf 0
b4 -4376.3 0 -Inf 0
b5 86.292 0 Inf 0
Number of observations: 19, Error degrees of freedom: 19 Root Mean Squared Error: 2.31e+131 R-Squared: -4.46e+258, Adjusted R-Squared -4.22e+258 F-statistic vs. zero model: NaN, p-value = NaN
Star Strider
Star Strider am 12 Okt. 2018
I would not worry about the warnings.
My objective is to get your code to work, and it now does.
If you were asking questions about other topics (perhaps physiology and some others), I could offer some expertise. I do not have sufficient background in climatology to help you with your model (although I certainly worry about the implications of your data).
Keegan Carvalho
Keegan Carvalho am 12 Okt. 2018
Bearbeitet: Keegan Carvalho am 12 Okt. 2018
I can understand @Star Strider and thank you for the code. However, I tried to change the beta values such as beta0 = [1000 1000 1000 1000 1000]; and I get the following errors:
Error using nlinfit>checkFunVals (line 649) The function you provided as the MODELFUN input has returned Inf or NaN values.
Error in nlinfit (line 251) if funValCheck && ~isfinite(sse), checkFunVals(r); end
Error in NonLinearModel/fitter (line 1127) nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
Error in classreg.regr.FitObject/doFit (line 94) model = fitter(model);
Error in NonLinearModel.fit (line 1434) model = doFit(model);
Error in fitnlm (line 99) model = NonLinearModel.fit(X,varargin{:});
So I would like to know, how do I decide the appropriate beta values? Because I wanted to get my coefficients as positive numbers. In my example above, b2 = 1.071e-12, which is not correct to me. Any advice on that?
Star Strider
Star Strider am 12 Okt. 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 am 13 Okt. 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 am 13 Okt. 2018
My pleasure.
This is not my area of expertise. I suggest you do an Interweb search for an appropriate model.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 13 Okt. 2018

0 Stimmen

You should use scatteredInterpolant(). Write back if you can't figure it out.

1 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 13 Okt. 2018

0 Stimmen

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.

11 Kommentare

Keegan Carvalho
Keegan Carvalho am 14 Okt. 2018
This is an interesting approach @Image Analyst, but however I was interested in deriving relationships between sea with sst and at. So far, I've tried nonlinear reg and but i am having a problem with R2 as they are in infinite values. Any alternatives?
Image Analyst
Image Analyst am 14 Okt. 2018
Not sure I know what you want now. I thought you wanted a function where you could enter sst and at and get out the predicted sea. And I gave that to you with the F() function. Now I have no idea what you want. Do you want an analytical function? As you can see from the surface fit, there are many peaks and valleys and ridges so there will most likely be no analytical formula you can get with something like multilinear regression (a 2-D polynomial) what will give you that surface unless you overfit it (bad!) with a very high order polynomial.
Keegan Carvalho
Keegan Carvalho am 14 Okt. 2018
Bearbeitet: Keegan Carvalho am 14 Okt. 2018
Image Analyst I've been thinking of the same as I seem to get pretty bad equations and R^2 values. Maybe the nonlinear regression might not be the best. However, I did find the F() method interesting.
So is it advisable to use the F() function and state that it can be used to predict sea levels with sst and at? I wanted to use this in my thesis. I would like your opinion since I could consider this as an alternative to regression
Walter Roberson
Walter Roberson am 14 Okt. 2018
"So is it advisable to use the F() function and state that it can be used to predict sea levels with sst and at?"
In my opinion: NO. In my opinion, the proper response would be to say that the available data is insufficient to find a justifiable model of.
Star Strider
Star Strider am 14 Okt. 2018
@Walter —
My sentiments, exactly.
Keegan Carvalho
Keegan Carvalho am 15 Okt. 2018
@Walter Thank you for your advice. However, since this does not work out, is there any other method where I could find the relationships? I welcome any suggestions and opinions. Thanks
Image Analyst
Image Analyst am 15 Okt. 2018
This F just shows you what the values of sea might have been if you wanted to interpolate something in between, but as I showed you in the scatterplots in your other question, there is essentially no discernible pattern. Everything is essentially random and uncorrelated. The scatter plots show that and the crazy looking image I showed above show that. So for you to think you can just plug in some new value of at and sst and get a reasonably accurate prediction for sea is just fantasy or overly wishful thinking.
The suggestion is that you need to collect much, MUCH more data. Even then there may be no influence. For example if I were to plot the birthdate vs the height and weight of all the people working in my building, hoping to get a nice relationship, it just won't happen because there is no relationship. Your data appears to show no relationship, though perhaps one might emerge if you had tons more data, not just 17.
Walter Roberson
Walter Roberson am 16 Okt. 2018
"For example if I were to plot the birthdate vs the height"
You might find that the mean height increased as they grew from toddlers to 17-ish, and that the mean height started decreasing again in 40s (people shrink) and 70s (men die off faster than women), but you would not get far in predictions of individual heights.
Walter Roberson
Walter Roberson am 16 Okt. 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 am 22 Okt. 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 am 27 Okt. 2018
Thank you Image Analyst for your help and guidance

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by