# fitting different data sets with different models with NonlinearModelFit

**URL:** <https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [October 8, 2013, 3:58pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679 "2013-10-08T15:58:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgonzalez](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sgonzalez/32/7669_2.png) [@sgonzalez](https://community.wolfram.com/u/sgonzalez)\
**Post date:** [October 8, 2013, 3:58pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/1 "2013-10-08T15:58:48Z")

</div>

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.

```auto
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"]

```

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [October 8, 2013, 7:25pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/2 "2013-10-08T19:25:45Z")

</div>

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](http://community.wolfram.com/groups/-/m/t/53970)

I]([How does one use LinearModelFit with multiple y variables?](https://wolfram.discourse.group/t/217/1)) 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:

> In Wolfram Library there is an upload:
> 
> Multiple-Response Fitting by Bruce Miller
> 
> Daniel sent a few links - perhaps they will be useful:  
> [http://mathematica.stackexchange.com/questions/866/simultaneously-fitting-multiple-datasets  
> ](http://mathematica.stackexchange.com/questions/866/simultaneously-fitting-multiple-datasets)[http://forums.wolfram.com/mathgroup/archive/2011/Sep/msg00555.html](http://forums.wolfram.com/mathgroup/archive/2011/Sep/msg00555.html) (this one links to the four below)  
> [http://forums.wolfram.com/mathgroup/archive/2004/Oct/msg00031.html](http://forums.wolfram.com/mathgroup/archive/2004/Oct/msg00031.html)[http://forums.wolfram.com/mathgroup/archive/2006/Apr/msg00011.html  
> ](http://forums.wolfram.com/mathgroup/archive/2006/Apr/msg00011.html)[http://forums.wolfram.com/mathgroup/archive/2006/Apr/msg00020.html](http://forums.wolfram.com/mathgroup/archive/2006/Apr/msg00020.html)[http://forums.wolfram.com/mathgroup/archive/2010/Apr/msg00280.html](http://forums.wolfram.com/mathgroup/archive/2010/Apr/msg00280.html)

---

<div class="post-metadata">

**Author:** ![sgonzalez](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sgonzalez/32/7669_2.png) [@sgonzalez](https://community.wolfram.com/u/sgonzalez)\
**Post date:** [October 8, 2013, 8:00pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/3 "2013-10-08T20:00:12Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![sgonzalez](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sgonzalez/32/7669_2.png) [@sgonzalez](https://community.wolfram.com/u/sgonzalez)\
**Post date:** [October 8, 2013, 8:13pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/4 "2013-10-08T20:13:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [October 9, 2013, 5:04pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/5 "2013-10-09T17:04:57Z")

</div>

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  
[Least Squares Fitting -- from Wolfram MathWorld](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.

---

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [October 9, 2013, 5:15pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/6 "2013-10-09T17:15:31Z")

</div>

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,

```auto
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.)

---

<div class="post-metadata">

**Author:** ![sgonzalez](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sgonzalez/32/7669_2.png) [@sgonzalez](https://community.wolfram.com/u/sgonzalez)\
**Post date:** [October 10, 2013, 1:45pm UTC](https://community.wolfram.com/t/fitting-different-data-sets-with-different-models-with-nonlinearmodelfit/679/7 "2013-10-10T13:45:46Z")

</div>

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
