# 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 ...

45

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 ...

43

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", "σ"...

30

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

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

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 ...

27

Here's a method for doing weighted orthogonal regression of a straight line, based on the formulae in Krystek/Anton and York: ortlinfit[data_?MatrixQ, errs_?MatrixQ] := Module[{n = Length[data], c, ct, dk, dm, k, m, p, s, st, ul, vl, w, wt, xm, ym}, (* yes, I know I could have used FindFit[] for this... *) {ct, st, k} = Flatten[MapAt[...

27

Here I will attempt to provide a basic implementation of the random forest algorithm for classification. This is by no means fast and doesn't scale very well but otherwise is a nice classifier. I recommend reading Breiman and Cutler's page for information about random forests. The following are some helper functions that allow us to compute entropy and ...

27

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

Here are three points in space. SeedRandom; {p1, p2, p3} = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}} = RandomReal[{-3, 3}, {3, 3}]; If the center is $p=(x,y,z)$ and the radius is $r$, then the distance from $p$ to each $p_i$ must be exactly $r$. Thus, for each $i=1,2,3$, we have $$(x - x_i)^2 + (y - y_i)^2 + (z - z_i)^2 = r^2.$$ Furthermore, the ...

26

I would try plt=Show[ListPointPlot3D[data, ColorFunction -> "Rainbow"], Plot3D[fit["BestFit"], {x, 0, 180}, {y, 0, 0.1}, PlotStyle -> Directive[Yellow, Specularity[White, 20], Opacity[0.3]]], BoxRatios -> {1, 1, 1}] Then you can change perspective? GraphicsArray[{{plt, Show[plt, ViewPoint -> Front]}, {Show[plt, ViewPoint -&...

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

I like to draw the predicted and actual responses and connect them with a little line. That shows where the fit is good and where it isn't. With[{ actualpredicted={ data, Transpose[ Append[ Transpose[ fit["Data"][[All,{1,2}]]], fit["PredictedResponse"] ] ] } }, Show[ ListPointPlot3D[actualpredicted, ...

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

This is, at least in principle, a duplicate of jVincent's answer and the one I gave here. The general approach has been suggested by various people over the years, although I first encountered it here courtesy of Daniel Lichtblau the first time I needed to fit several datasets simultaneously. I've been meaning to post this package for a while, ideally after ...

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

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, ....

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