Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'LinearModelFit of various arrays' topicStudent Support Forum > General > Archives > "LinearModelFit of various arrays"

Help | Reply To Topic
Author Comment/Response
Asgard
09/24/12 03:52am

I am trying to fit some data included in different arrays. Let's say for example I have
Frequency1-> Array1[[a,b]]
Frequency2-> Array2[[a,b]]
Frequency3-> Array3[[a,b]]
Frequency4-> Array4[[a,b]]
So what I want to do is a linear fit among the arrays, for each value of the array [[a,b]]; i.e, the linear fit should be something like Fit[[a,b]]. I use LinearModelFit instead of FindFit or Fit because I am also interested in the residuals. I would like to end up with an array FitSlope with all the fitted slopes and another array FitErrors with the error of the slopes, for each array element.

Initially I thought about using a loop, such as

For[k = 1, k < 351,
For[l = 1, l < 351,
MyFit =
LinearModelFit[{{Frequency1, Array1[[k, l]]}, {Frequency_2,
Array2[[k, l]]}, {Frequency3,
Array3[[k, l]]}, {Frequency4, Array4[[k, l]]}}, x, x];
FitSlope[[k, l]] = MyFit["BestFitParameters"][[2]];
FitErrors[[k, l]] = MyFit["ParameterErrors"][[2]]
; l++]
; k++];

Obviously this is very inefficient with 100x100 arrays or bigger, turns out to be very slow and includes loops that are not good practice in Mathematica. Thus, I tried to use an Array to do this, such as:

MyFit2 = Array[xx, {100, 100}];
MyFit2 =
LinearModelFit[{{Frequency1, Array1}, {Frequency2,
Array2}, {Frequency3,Array3}, {Frequency4,Array4}},
x, x];

Which is giving me an error "First argument is not a vector or matrix".
Hence, I created and array with constant value for the different frequencies, which is still giving me the same error.

So the question is, what am I doing wrong? Else, is this the best way to optimize such fitting?

Thank you very much

URL: ,
Help | Reply To Topic