0
$\begingroup$

I'm trying to do multiple linear regression and having a hard time understanding the error messages I'm getting because web search doesn't find them or because the messages use terminology that isn't defined in the documentation.

Here's the synthetic data set I'm using at the moment:

data = {{5, 10, 20, 30}, {7, 13, 25, 36}, {9, 16, 30, 42}, {11, 19, 35, 48}, {13, 22, 40, 54}, {15, 25, 45, 60}, {17, 28, 50, 66}, {19, 31, 55, 72}, {21, 34, 60, 78}, {23, 37, 65, 84}, {25, 40, 70, 90}}

That's built by data = Transpose[{x1, x2, x3, x4}] where x1 = Table[2*x + 5, {x, 0, 10, 1}] and so forth.

When I try to fit it like this:

ftp = LinearModelFit[data, {c1, c2, c3}, p1]

I get this message which leaves it unclear what's wrong. Is the problem with the list of functions {c1, c2, c3} (in what sense are these functions? My functions are 2*x + 5, etc. Is the list too short or too long? What does the (3) and (1) refer to?) And what exactly are the "coordinates" and "variables" mentioned in the message? Documentation uses neither of these terms in this context.

LinearModelFit::fitc: Number of coordinates (3) is not equal to the number of variables (1).

Many aimless permutations ensued, including this random effort, which at least produced a new but equally murky message:

ftp = LinearModelFit[data, {c1, c2, c3}, {p1, p2, p3}]
LinearModelFit::desmat: Unable to construct a numeric design matrix. Nominal variables may need to be specified, or non-numeric entries for numeric variables may need to be replaced.
$\endgroup$
  • $\begingroup$ try LinearModelFit[data, {c1, c2, c3}, {c1, c2, c3}] $\endgroup$ – kglr Jul 16 '17 at 17:45
  • $\begingroup$ Could you please address the documentation issues listed above? Why do I need three third-position parameters (coordinates? variables?) for a multiple regression over a single linear variable? Also FYI this code led to a new error that does make some sense at least: LinearModelFit::rank: The rank of the design matrix 2 is less than the number of terms 4 in the model. The model and results based upon it may contain significant numerical error. $\endgroup$ – Bradjcox Jul 16 '17 at 17:52
  • 2
    $\begingroup$ LinearModelFit interprets each 4 item sub-list as three independent values with the fourth dependent on the first three. From your description it sounds like you really want four separate fits, but its not terribly clear. $\endgroup$ – george2079 Jul 16 '17 at 18:48
  • 2
    $\begingroup$ LinearModeFit does have a "different" syntax that takes a bit of getting used to. What you want (and is shown in the online help) is something like the following: LinearModelFit[data, {c1, c2, c3}, {c1, c2, c3}]. A more "natural" syntax is found with NonlinearModelFit where one has the input parameters as data, model, coefficients, and predictor variables (You can fit linear models with NonlinearModelFit.) Not to be insulting but looking carefully at the documentation with the idea that there are plenty of subtleties helps considerably. $\endgroup$ – JimB Jul 16 '17 at 22:20
  • 2
    $\begingroup$ The rank of the design matrix is 2 and less than the number of predictors is because you have more predictors than the data can support. All 3 of your predictor variables are perfectly correlated with each other. In essence each of the 3 provides the exact same information for predicting the response variable. (In fact all 4 variables in your dataset are perfectly correlated with each other: Correlation[data].) $\endgroup$ – JimB Jul 16 '17 at 22:45
3
$\begingroup$

You are working too hard. It can be done by mapping LinearModelFit over the data. However, because LinearModelFit assumes a domain of 1, 2, 3, ... when the data elements are given in the singleton form y1, y2, y3, ..., pairs of the form {xk, yk} must be generated.

nPts = 11;
domain = Range[nPts] - 1;
data =
 {Table[{x, 2 x + 5}, {x, domain}], 
  Table[{x, 3 x + 15}, {x, domain}],
  Table[{x, 4 x + 20}, {x, domain}], 
  Table[{x, 6 x + 30}, {x, domain}]};

{c1, c2, c3, c4} = LinearModelFit[#, p, p] & /@ data;
Column @ {c1, c2, c3, c4}

fits

Of course, in this case the fit is perfect.

Thread /@ Through[{c1, c2, c3, c4}[domain]]
{{5., 7., 9., 11., 13., 15., 17., 19., 21., 23., 25.}, 
 {15., 18., 21., 24., 27., 30., 33., 36., 39., 42., 45.}, 
 {20., 24., 28., 32., 36., 40., 44., 48., 52., 56., 60.}, 
 {30., 36., 42., 48., 54., 60., 66., 72., 78., 84., 90.}}
Through[{c1, c2, c3, c4}["FitResiduals"]]
{{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}, 
 {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}, 
 {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}, 
 {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}
$\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.