Re: Weights syntax in NonlinearRegress/Fit
- To: mathgroup at smc.vnet.net
- Subject: [mg34125] Re: Weights syntax in NonlinearRegress/Fit
- From: adam.smith at hillsdale.edu (Adam Smith)
- Date: Sat, 4 May 2002 04:29:18 -0400 (EDT)
- References: <aaols2$een$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
See if the following helps. I tried to do things in the way that is easiest to visualize. It probably is not the most elegant or efficient way. Adam Smith In[18]:= Needs["Statistics`NonlinearFit`"] Define a function for the data In[19]:= f[x_] := 1000*E^(-0.25*x)+10 Now create a list of perfect data without any error In[20]:= perfectdata=Table[{x, f[x]}, {x, 0,20, 0.5}]; Now create a list of data with a random error that is +/- the square root of the "perfectdata. There are more elegant ways in Mathematica to do this, but this seems the easiest to understand. In[21]:= data = Table[{x, f[x]+Sqrt[f[x]]*Random[Real,{-1,1}]}, {x, 0, 20, 0.5}]; Now create a list of weights based on the "noisy" data. I choose to weight by the square root of the number (statistical weighting). In[22]:= mywts = Table[ data[[i,2]]^.5,{i,1,Length[data]}]; This next one is just a test for all the weights being equal - the default in NonlinearFit. In[23]:= equalwts = Table[ 1,{i,1,Length[data]}]; This is just to visualize the two data sets. In[24]:= plot1 = ListPlot[data,PlotStyle->PointSize[0.018]]; ListPlot[perfectdata,PlotStyle->PointSize[0.018]]; Define the model that I want to fit In[26]:= fmodel[a_, b_,c_][x_] := b*E^(-a*x)+c; Try the default with the default weighting In[27]:= NonlinearRegress[data, fmodel[a, b,c][x], {x}, {a, b,c}, RegressionReport ->{BestFitParameters}] Out[27]= {BestFitParameters ->{a->0.243643,b->997.273,c->6.67955}} Now check my equal weighting list. It should give the same as above. Examination shows this is true. Using RegressionReport->{BestFitParameters, ParameterCITable} reveals that the uncertainties in the parameters differ for different weighting also. I just left them off because they don't translate well into asii text. In[28]:= NonlinearRegress[data, fmodel[a, b,c][x], {x}, {a, b,c}, RegressionReport->BestFitParameters},Weights->equalwts] Out[28]= {BestFitParameters->{a->0.243643,b->997.273,c->6.67955}} Now try the weighting function I created above and named "mywts". Note the changes from the above non-weighted parameters. In[29]:= NonlinearRegress[data, fmodel[a, b,c][x], {x}, {a, b,c}, RegressionReport->{BestFitParameters},Weights->mywts] Out[29]= {BestFitParameters->{a->0.239198,b->997.654,c->2.38525}} There is another way using a pure function that avoids manually constructing the list of weights. In[30]:= NonlinearRegress[data, fmodel[a, b,c][x], {x}, {a, b,c}, RegressionReport->{BestFitParameters},Weights->(Sqrt[#]&)] Out[30]= {BestFitParameters->{a->0.239198,b->997.654,c->2.38525}} The following checks the last method by plotting the fit function over the data. "plot1" is defined above and is just the "noisy data". In[31]:= ffit[x_] = fmodel[a, b,c][x] /. (BestFitParameters /. %) plot2 = Plot[ffit[x], {x, 0, 12}]; Show[plot1, plot2]; Dion Lewis <dlewis at kerr.phys.utas.edu.au> wrote in message news:<aaols2$een$1 at smc.vnet.net>... > Hi, > > It seems I have an identical problem to Frederic Fontaine dated 22 Jun > 1999 that didn't get answered in the threads. > > I'm ALSO having troubles with the syntax of the Weights option in > NonlinearRegress. I want to input an equation not simply values. > I'm not a member of this list so Im not sure if this will get back to me > either... > I'd appreciate any help, for ease I have cut and pasted his eloquent > version... > > > NonlinearRegress, Weights, Syntax > Subject: [mg34125] NonlinearRegress,Weights, Syntax > From: Frederic Fontaine <F.Fontaine at fz-rossendorf.de> To: mathgroup at smc.vnet.net > > Hi, > > I've been fighting against the syntax of the option "Weights" in the > NonlinearRegress command. I want to fit large data sets and I don't want > to type hundreds of values. I'd rather use a function defining the weight > of each data points. But how to do it with Mathematica 3 for Unix? > For instance: > NonlinearRegress[dataset, fitfunction(x,para),{x},{para},Weights->{???}] > I would appreciate someone helping me remove the question marks! > > **Edited from F.F.** > > Regards, > Dion Lewis > University of Tasmania > Physics Department