which modelfun can be used in fitnlm for nonlinear regression?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
1 vote
Hi to run the fitnlm I need a function modelfun as input parameter. This function has to be defined. My questions are -
- Are there some rules how to define this function?
- Are there some "standard"-functions that will be used in that case?
- Does this modelfun function has to have sth in common with the original function I would like to approximate with nonlinear regression?
- I thought about using a spline function as the modelfun. Does it make sence here?
Thank you & Best Regards
Accepted Answer
Star Strider
on 17 May 2015
There are more ways to define ‘modelfun’ in fitnlm than the other nonlinear fitting functions. However I would use an anonymous function or function file for it, because you can use them with all the other nonlinear fitting functions as well. (You cannot use the string form with the others.) I would not use a spline, since the idea is to estimate the parameters and not get a precise fit.
If for instance you have a decaying exponential of the form:
f(t) = a * exp(b*t) * sin(c*t + d)
your ‘modelfun’ (objective function) would be:
f = @(b,t) b(1).*exp(b(2).*t).*sin(b(3).*t+b(4));
Note that the order of the arguments must be the order of the parameter vector (here ‘b’) first and the independent variable vector second.
The reason to choose the anonymous function or function file is that while fitnlm makes nonlinear curve fitting relatively easy and produces a number of relevant statistics on the fit, it does not provide confidence intervals on the parameters or data. You would need to repeat your regression using nlinfit, and use nlparci and nlpredci to get those statistics, and you can use the anonymous function or function file form for all functions, including those of the Optimization Toolbox and Global Optimization Toolbox.
6 Comments
uli man
on 17 May 2015
Thank you, I think it´s better when I describe what exactly I want to do: I need to approximate the following function:
m(x1,x2,x3)=x1^2+x2^2+x3^2.
I already tried to use as modelfun the following term:
modelfun = @(b,x) b(1)*X(:,1).^2+b(2)*X(:,2).^2+b(3)*X(:,3).^2
But unfortunately it doesn´t make sence to me, because b(1), b(2) and b(3) are always 1. What could be here an anonymos function that I can use? My goal is to get a model based on the given data (matrix X, vector Y with responce values based on columns of matrix X) and then to evaluate this model on new data.
Thank you
Star Strider
on 17 May 2015
My pleasure.
The curve-fitting functions you referenced are for just that — curve fitting — determining the best parameter set to fit a function of independent variables to a matching set of dependent variables.
You are doing optimisation — finding a set of parameters that minimise a function, with or without constraints on those parameters. There are several functions — the simplest being fminsearch, and the functions in the Optimization Toolbox and Global Optimization Toolbox — designed specifically for what you want to do.
So since you are not fitting data but optimising a function, a subtle but important difference, you need to use the correct function.
The function you use to define your optimisation problem will be a function file or an anonymous function, possibly along with separate functions to define whatever constraints you wish to impose on the optimisations.
You would write your function:
m(x1,x2,x3)=x1^2+x2^2+x3^2
as:
m = @(x) x(1).^2+x(2).^2+x(3)^2;
Be sure to vectorise it to do array operations unless you intend to do matrix operations.
The problem I’m now having is that you initially describe a rather straightforward optimisation problem and then mention that you want to create a function using a matrix X as your independent variable and a vector Y as your dependent variable. You can certainly do this, but you need to define what now appears to be a curve-fitting problem in some detail. There are too many possible approaches to it to cover here without specific details.
Please define your specific problem in some detail.
uli man
on 17 May 2015
Thank you again. I started to use matlab just some weeks ago and don´t know about all the functionality Matlab has to solve the specific math problems. I also allow that I try to use wrong functions to solve my problem.
Here are more details for what I have been trying to do:
I have a matrix X with n rows and 3 columns. Based on each row Xi I calculate Yi using
m(xi1,xi2,xi3)=xi1^2+xi2^2+xi3^2=Yi
These Yi will be stored in a columnvector Y. So I have strong dependency between the rows in Matrix X and the rows in vector Y. What I have to do know is not to use the original function
m(x1,x2,x3)=x1^2+x2^2+x3^2
but do a regression estimate of this function with smoothing spline.
Because the original function isn´t linear i decided to use fitnlm with fitnlm(X,y,modelfun,beta0) to do nonlinear regression. The Input parameters are:
- X is my nx3 matrix
- y is my nx1 vector
- beta0 is an array with 3 parameters b(1), b(2) and b(3)
- as modelfun I assume I have to use the smoothing spline to estimate the original function.
Does it make sence now?
Best Regards
Star Strider
on 17 May 2015
I would not use a smoothing spline. However, to do a spline, you have to have the Curve Fitting Toolbox. I don’t have it, so I can’t help you with its functions. There may be File Exchange routines that will do what you want, but I’ve not used them and have no experience with them.
You mentioned earlier that this regression function:
modelfun = @(b,x) b(1)*X(:,1).^2+b(2)*X(:,2).^2+b(3)*X(:,3).^2;
B = nlinfit(X, Y, modelfun, rand(3,1));
yields ‘B’ as all ones. I would expand the output list to nlinfit to see how exact the fit is:
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(X, Y, modelfun, rand(3,1));
paying special attention to ‘R’ (the residuals), ‘CovB’ (parameter covariance matrix), and ‘MSE’ (mean squared error of the fit).
I do not know what your data are or what information you want from them.
uli man
on 18 May 2015
I stick on smoothing spline because it´s part of my task. This is an extract with other function: In our first example we set X=(X1;X2) with independent standard normally distributed random variables X1 andX2 and choose m(x1;x2)=2*x1+x2+ 2. In this case Y=m(X) is normally distributed with expectation 2 and variance 5. We estimate the density of Y by the estimate using a fully data- driven smoothing spline estimate to estimate the linear function m.
In my case I have X=(X1,X2,X3), m(x1,x2,x3)=x1^2+x2^2+x3^2 and Y=m(X). And I also want to use data driven smoothing spline but it seems that I don´t know how exactly to use it...
Star Strider
on 18 May 2015
I did not know you had to use smoothing splines, so I was suggesting you not use them.
The spline functions are part of the Curve Fitting Toolbox. I don’t have it, so I can’t help you with it. You can use the spline function to do interpolation, but I do not know if that is what you want to do.
I will add ‘smoothing spline’ and ‘Curve Fitting Toolbox’ to the appropriate tag fields so that someone who has the toolbox and has used smoothing splines can possibly provide help.
More Answers (0)
Categories
Find more on Smoothing in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)