Message Boards Message Boards

0
|
3954 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Beginner question on LinearModelFit

Posted 11 years ago
Hi,

Apologies for the simple nature of the question, but I'm having trouble interpreting the LinearModelFit documentation.

How do I do a simple multivariate regression  in Mathematica ?

For example, in R, I could do something like :

r1 <- read.table("test.txt",header=TRUE)
l1 <- lm(Yval ~ X1 + X2,data=r1)
summary(l1)


What would be the equivalent in Mathematica ? I assume LinearModelFit would be the correct choice of function, but I'm finding the documentation a little perplexing !

Thanks !
POSTED BY: John Smith 2
In your R code, you have associated X1 and X2 with the appropiate colums of data during Import.

In Mathematica, you give LinearModelFit a list of triples. This is what the data looks like:
data={{1, 1, 0.909297}, {1, 2, 0.14112}, {1, 3, -0.756802}, {1,
  4, -0.958924}, {1, 5, -0.279415}, {2, 1, 0.14112}, {2,
  2, -0.756802}, {2, 3, -0.958924}, {2, 4, -0.279415}, {2, 5,
  0.656987}, {3, 1, -0.756802}, {3, 2, -0.958924}, {3,
  3, -0.279415}, {3, 4, 0.656987}, {3, 5, 0.989358}, {4,
  1, -0.958924}, {4, 2, -0.279415}, {4, 3, 0.656987}, {4, 4,
  0.989358}, {4, 5, 0.412118}, {5, 1, -0.279415}, {5, 2,
  0.656987}, {5, 3, 0.989358}, {5, 4, 0.412118}, {5, 5, -0.544021}}

It is a list of triples. each triple represents the two independant and the dependant variable. So where it says {1, 1, 0.909297}, that means in your terms x1 is 1, x2 is 1, and Y is 0.909297. Before using LinearModelFit, manipulate your data so that it is in this format. Once you have done this, you can call LinearModelFit on it:
LinearModelFit[data, {x, y}, {x, y}]
Manipulating the data to get it into the right form requires some knowledge of Mathematica programming. Let's say that the data above came from an excel file and there was one colum for the X1 values, one for the X2 values, and one for the Y values. Running Import on the data may produce extra curly brackets like this:
Import["mydata.xls"]
{{{1., 1., 0.909297}, {1., 2., 0.14112}, {1., 3., -0.756802}, {1., 4., -0.958924}, {1., 5., -0.279415}, {2., 1., 0.14112}, {2., 2., -0.756802}, {2., 3., -0.958924}, {2., 4., -0.279415}, {2., 5., 0.656987}, {3., 1., -0.756802}, {3., 2., -0.958924}, {3., 3., -0.279415}, {3., 4., 0.656987}, {3., 5., 0.989358}, {4., 1., -0.958924}, {4., 2., -0.279415}, {4., 3., 0.656987}, {4., 4., 0.989358}, {4., 5., 0.412118}, {5., 1., -0.279415}, {5., 2., 0.656987}, {5., 3., 0.989358}, {5., 4., 0.412118}, {5., 5., -0.544021}}}
Run the First command to remove the extra brackets.
Another useful command is Transpose. This does exactly the same thing as a matrix transposition.
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract