# Tag Info

55

It is possible to include the number of peaks (denoted $n$ below) in minimum searching. First we create some test data: peakfunc[A_, μ_, σ_, x_] = A^2 E^(-((x - μ)^2/(2 σ^2))); dataconfig = {{.7, -12, 1}, {2.2, 0, 5}, {1, 9, 2}, {1, 15, 2}}; datafunc = peakfunc[##, x] & @@@ dataconfig; data = Table[{x, Total[datafunc] + .1 RandomReal[{-1, 1}]}, {x, -...

48

I submitted this question to answer it myself, since I recently updated my Bayesian inference repository on GitHub with a function called BayesianLinearRegression that does just this. I wrote a general introduction to its functionalities on the Wolfram Community and the example notebook on GitHub shows some more advanced uses of the function. I also ...

46

First, let's enumerate some of the functions: Fit[] is the simplest of fitting functions. It has been introduced in v5, and hasn't been updated since v6 (as of v11). It finds a least-squares fit as a linear combination of functions. As such it can only be used for simple functions. Fit[] can fit $c\sin{x}$ but not $\sin{c x}$, where c is an unknown ...

44

Here is another approach. It could be improved (I am sure) to properly determined the principle axes and translation (if I get time I will aim to update): lin = {#1^2, #1, #2, 2 #1 #2, #2^2} & @@@ points; lm = LinearModelFit[lin, {1, a, b, c, d}, {a, b, c, d}] Exploring model: lm["ParameterTable"] Determining quadric formula: pa = lm["...

32

Solution Following @MichaelSeifert's advice, here is a working solution. First define a compact expression for a Gaussian g[x_, xo_, σ_, a_] := a Exp[-((x - xo)^2/(2 σ^2))] /(σ Sqrt[2 π]) To get g[x, xo, σ, Ao] Creates a list of variables for the $k^{\text{th}}$ term. kvar[k_Integer] := ToExpression@ Map[StringJoin[#, ToString[k]] &, {"x", "σ"...

31

Since the question isn't clear about which datasets are which and arguably has too many parameters, I'll use the example from here instead: $$\begin{array}{l} A+B\underset{k_2}{\overset{k_1}{\leftrightharpoons }}X \\ X+B\overset{k_3}{\longrightarrow }\text{products} \\ \end{array} \Bigg\} \Longrightarrow A+2B\longrightarrow \text{products}$$ We solve ...

30

Here is a standard direct way to get the principal exes and other transformation data. Find the mean of the points, subtract it to center them, and take the singular value decomposition. The third and second components thereof give the rotation and scaling data necessary to form a circle on which the first component, viewed as a point set, roughly lies. The ...

30

The following is based on the fact that the determinant of a matrix is equal to zero when two rows are the same. Thus, if you plug any of the points in, you get a true statement. SeedRandom; pts = RandomReal[{-1, 1}, {5, 2}]; row[{x_, y_}] := {1, x, y, x*y, x^2, y^2}; eq = Det[Prepend[row /@ pts, row[{x, y}]]] == 0 (* Out: 0.0426805-0.0293168x-0....

29

Luca, I think that you may be misunderstanding the meaning of "linear" in the names of those functions, and in general in reference to linear models. Just to make sure that we agree on the nomenclature here, in a fitting model you have parameters, and predictor variables. For instance, in a simple quadratic model such as y = a x^2 + b x + c we typically ...

28

I'm not sure if this addesses all of the issues you are having but here is an implementation I put together some time ago that allows us to use LinearModelFit and BSplineBasis to do spline regression. The benefit of this approach is that all of the properties of FittedModel are immediately available to us. This allows for checking for fit, residual ...

28

Let's rename things slightly to make it more consistent g = Fit[newdata, {1, x, x^2, x^3, x^4}, x]; To find inflection points, you can just put (blue) points where the second derivative is zero. Plot[g, {x, 20, 60}, Epilog -> {Red, PointSize[0.02], Point[newdata], Blue, Point[{x, g} /. Solve[D[g, {x, 2}] == 0]]}, PlotRange -> {{-5, 70}, {-5, ...

26

The question is not so innocent as is appears. Without a penalty on the number of peaks the "best" model is overfitting the data. The answer by Silvia demonstrates this already. And, think about it, you got what you wanted: adding more peaks will fit the data better. Always! One may revert to adding an ad-hoc penalty function on the number of peaks. But ...

26

vars = {w, x, y, z}; terms = MonomialList[(Plus @@ vars)^3] /. _Integer x_ :> x; cols = Join @@ {vars, terms} (* {w,x,y,z,w^3,w^2 x,w^2 y,w^2 z,w x^2,w x y,w x z,w y^2, w y z,w z^2,x^3,x^2 y,x^2 z,x y^2,x y z,x z^2,y^3,y^2 z,y z^2,z^3} *) For the data dt = Table[Join[RandomInteger[10, 4], {RandomReal[]}], {100}]; evaluate all models with up to ...

26

Fitting an ellipse: i = Import["http://i.stack.imgur.com/W7HJk.jpg"]; lineByCenter[center_, semi_, angle_] := Rotate[Line[{#1 - #2, #1 + #2}], angle, #1]& [center,{0,semi}] sa = 1 /. ComponentMeasurements[ Binarize@i, "SemiAxes"] angle = 1 /. ComponentMeasurements[ Binarize@i, "...

25

Let's first solve it without constraints. Using fitdata from your question one needs to set reasonable starting values. You may have to experiment (I like to use Manipulate) to get in the ball park. nlm = NonlinearModelFit[fitdata, a*Exp[b*x] + c*Exp[d*x] + o, {{a, 0.1}, {b, 3}, {c, 0.3}, {d, -4}, {o, 1}}, x] yields nlm["BestFitParameters"] (* {a -...

25

Fit is limited to using a series of basis functions. It finds the parameters multiplied by the basis functions that fits the data in a least squares sense. FindFitis capable of using very general functions that don't work with the Fit model. It will also find parameters that fits the data in a least squares sense. LinearModelFit is the same as Fit with the ...

25

All 3 previous answers show how to "fix" the issue. Here I'll show "why" there is an issue. The cause of the issue is not the fault of the data. It is because of poor default starting values and because of the form of the predictive function. The predictive function divides by $a+b x$ and this blows things up when $a+b x=0$. Below is the code to show ...

24

Inspired by WReach's answer, I started playing with Query based approach and here's what I came up with: data = {"days" -> {1, 2, 6, 8}, "area" -> {3, 6, 8, 2}, "frequency" -> {1, 4, 4, 2}, "height" -> {2, 3, 11, 6}} With the data in the above form, we just create a Dataset simply as follows: dataset = Dataset[data]; Don't worry ...

23

Use PlotRange -> All. Most plot functions tend to throw away points that aren't nicely clustered with the bulk: Show[{ListPlot[data, PlotRange -> All], Plot[ab, {x, 0, 2}, PlotStyle -> Red]}] As you can see, there is a number of points that completely mess up the fit.

21

Note Added in proof Oleksander's answer provides a better fit to the data than my solution below and circumvents the reiteration-and-solving-individually problem I describe below. This is not an answer to your question specifically, but rather is one method to use ParametricNDSolve to fit experimental data. Defining the problem $\require{mhchem}$ ...

21

The mathematica help is very thorough and is very indicative of what you should do next. By way of the histogram diagram obtained, you can compare your data against the proposed distribution. Show[Histogram[w[[2, 1]], Automatic, "ProbabilityDensity"], Plot[PDF[h["FittedDistribution"], x], {x, 0, 1500}, PlotStyle -> Thick]] The reference points you to ...

21

In analytic geometry, the ellipse is defined as the set of points (X,Y) of the Cartesian plane that, in non-degenerate cases, satisfy the implicit equation with and where Lets fit points with second-order curve (which include ellipse). elipse = a11*x^2 + a22*y^2 + 2*a12*x*y + 2*a13*x + 2*a23*y + a33; coeff = {a11, a22, a12, a13, a23, a33}; fitResult=...

21

The approach mentioned in my comment using a polar representation: r[a_, b_, theta_, t_] := a Sqrt/Sqrt[ (1 + (a/b)^2) + (1-(a/b)^2) Cos[2 (t - theta)] ]; some random sample data: data = With[ {a = 1, b = 1/2, theta = Pi/3 , x0 = 1, y0 = 1} , Table[{x0, y0} + {Cos[t], Sin[t] } r[a, b, theta, t] + RandomVariate[NormalDistribution[0, ....

21

Intuition is sometimes tricky on fitting procedures. This is of course not a Mathematica issue, but a problem of fitting in general. You can see the problem in parameter space (hence it depends on the details of parameter space). Defining for the residuals (square root) Res1[ff_, aa_, bb_] := Norm[data[[All, 2]] - (m1 /. {f -> ff, a -> aa, b -> ...

20

I think as much discussion as can reasonably be had on this issue has already taken place on this site, although the solution might not be readily apparent without the benefit of experience. This is not in any way meant as a criticism of the question (which is well-posed and relates to a commonly-encountered, important issue), but rather will be my excuse ...

20

I've found one difference between NonLinearModelFit and FindFit, and that is that NonLinearModelFit does not allow you to use the NormFunction to adjust how normalization as weighting is done. By default NonLinearModelFit seeks to reduce the sum of the squares of the residuals, so it will be equivalent to FindFit with NormFunction -> (Norm[Abs[#], 2] &...

20

Step 1: find the curve in the black and white image. For this, I will use a shortest path search, and to make sure it finds the path I'm looking for (instead of one of the short cuts through the labels), I will give it a few "path markers" along the way that the path should visit in order: First, load the image, get the black pixels: img = Import["http://i....

19

Since nobody who supposedly thought this was a good question seems actually to have wanted to write an answer, here is one from me. Let us define a bimodal distribution: dist = MixtureDistribution[ {0.6, 0.4}, {NormalDistribution[-0.8, 1.3], NormalDistribution[2.7, 0.4]} ]; pdfplot = Plot[PDF[dist, x], {x, -5, 5}] To simulate your data we draw some ...

19

I was playing around, trying to come up with a purely query-based solution. The result is by no means natural, but perhaps it holds some academic interest or will spark some insight into a better answer: Dataset[ { "days" -> {1, 2, 6, 8} , "area" -> {3, 6, 8, 2} , "frequency" -> {1, 4, 4, 2} , "height" -> {2, 3, 11, 6} } ][ Transpose, ...

Only top voted, non community-wiki answers of a minimum length are eligible