Group Abstract Group Abstract

Message Boards Message Boards

fitting different data sets with different models with NonlinearModelFit

GROUPS:
I have two different data sets to be fitted with two different models with the NonlinearModelFit, that is model1 will fit data1 and model2 will fit data2. The data takes also into account the error of the measurement. Both models share the same parameters a and b which are the ones we are interested in fitting. In the attached and runnable code I fitted the parameters without taking into account the errors. My question is how to consider the errors through the Weights command of the NonlinearModelFit where each error data set should be associated to the considered data set. How to do this association/splitting when fitting? Thanks a lot in advance.
 data1 = {{1, 1, 1/2}, {2, 2, 0.1}, {3, 3, 0.4}, {4, 4, 0.2}, {5, 5, 0.3}, {6, 2, 0.1}}
 
 data1xy = Table[{data1[[i, 1]], data1[[i, 2]]}, {i, 1, Length[data1]}]
 
 error1 = Table[data1[[i, 3]], {i, 1, Length[data1]}]
 
 data2 = {{1, 2, 0.1}, {2, 4, 0.2}, {3, 5, 0.5}, {4, 7, 0.3}, {5, 3, 0.2}, {6, 1, 0.1}}
 
 data2xy = Table[{data2[[i, 1]], data2[[i, 2]]}, {i, 1, Length[data2]}]

error2 = Table[data2[[i, 3]], {i, 1, Length[data2]}]

allData = Join[{1, Sequence @@ #} & /@ data1xy, {2, Sequence @@ #} & /@ data2xy]

model1par[index_, x_] := KroneckerDelta[index - 1]*(1 + a*x + b*x^2) + KroneckerDelta[index - 2]*(1 + a*x + b*x^3)

fit1par = NonlinearModelFit[allData, model1par[index, x], {a, b}, {index, x}, PrecisionGoal -> 2, WorkingPrecision -> 10]

fit1par["BestFitParameters"]

fit1par["EstimatedVariance"]

fit1par["ParameterConfidenceIntervalTable"]
Answer
7 months ago
Hi, you may find some of the material in this post to be useful for your own situation:

http://community.wolfram.com/groups/-/m/t/53970

I
n short, the built in model fitters don't have this ability, but it's usually feasible to write some code to resolve the issue. In particular consider the links below:
POSTED BY: Sean Clarke
Answer
7 months ago
Hi Sean,
first of all thank you for your kindly attention.

I know the links you provided to me, but as far as know none of them treat what I want to do, none of them consider errors in a serious way. I want to find the way to select each error data set depending the data to be fitted with a given model. I did (in the simple code attached as an example) something similiar to the example given in the link below (see at the end, it is done without errors) but now I want to consider errors.

http://mathematica.stackexchange.com/questions/15905/combined-fitting-via-nonlinearmodelfit

To sum up, I agree with you I have to write a code to resolve the issue, but after fighting for a week I still have not found the right way to deal with.

Any idea would be more than appreciated.

Thanks a lot.

Sergi
Answer
7 months ago
I checked this Fitting Multiple-Response Data Bruce Miller's package, but it does not consider errors.

I mean, I know how to fit different data sets with different models, I provided a simple example, but now I have to find the way to select the corresponding error data sets when fitting a given data sets with the corresponding model. 

Thanks a lot
Answer
7 months ago
There are probably some math whizzes on the Community who could derive the
parameter uncertainties from the curvatures of the total error expression created
in the Fitting Multiple-Response Data package. This sounds like something
one could find in a textbook.  Something like
   http://mathworld.wolfram.com/LeastSquaresFitting.html
equations 22 and following.

There is a physicists' method - vary each parameters from the fitted value just 
enough to increase the chi-squared per degree of freedom by 1. 
POSTED BY: Bruce Miller
Answer
7 months ago
That was sub-optimal.  Your question is about the Weights option for NonlinearModelFit
and fitting different models to two sets of data at once with shared parameters.

Are the two data sets easy to distinguish, so that the expression to fit can have an If ?
For example,
If[x>1000, model1, model2]
Then just Join the data lists and use the Weights option to specify weights for the
one data list.

Could one of the data lists be scaled or shifted so the above would work?
(The model would need the same scaling or shifting.)
POSTED BY: Bruce Miller
Answer
7 months ago
Hello Bruce,

first of all let me acknowledge your useful suggestion. I shifted my data sets as well as the models and everything works correctly through the If command.

Thank you very much,

All my best,

Sergi
Answer
6 months ago