3
$\begingroup$

I want to fit the following model: $y_t=\beta x_t+\epsilon_t$

For that, I've tried the following

In[3]:= Transpose[data]

Out[3]= {{0, 1, 3, 5}, {1, 0, 2, 4}}

In[5]:= LinearModelFit[Transpose[data], IncludeConstantBasis -> False]

During evaluation of In[5]:= LinearModelFit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.

Out[5]= LinearModelFit[{{0, 1, 3, 5}, {1, 0, 2, 4}}, 
 IncludeConstantBasis -> False]

What is my mistake?

$\endgroup$
4
$\begingroup$

The form of LinearModelFit that you are trying to use is

LinearModelFit[{m, v}]

where m is a design matrix, and v is a response vector. But in your input, m, is {0, 1, 3, 5}, which is not a matrix. Try this instead:

LinearModelFit[{
  {{0}, {1}, {3}, {5}},
  {1, 0, 2, 4}
  },
 IncludeConstantBasis -> False
 ]
$\endgroup$
4
$\begingroup$

Since you have only one paramter, you can use Grid Search method.

$\epsilon_t=y_t-\beta x_t$ and $error=\sum_{i=1}^4(y_i-\beta x_i)^2$.

We see that $0\leq\beta\leq 1$ from ListPlot.

 data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
    ListPlot[data, AspectRatio -> 1]

enter image description here

ϵ[β_] := Total[(#2 - β #1)^2 & @@@ data]
βval = Subdivide[0, 1., 100000];
Extract[βval, Ordering[ϵ[βval], 1]]

0.74286

$\endgroup$
3
$\begingroup$

As an alternative you could use the other structure for LinearModelFit:

data = Transpose[{{0, 1, 3, 5}, {1, 0, 2, 4}}]
lm = LinearModelFit[data, x, x, IncludeConstantBasis -> False];
lm["ParameterTable"]

Parameter table

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.