1
$\begingroup$

I get the following results when I run the following regression on datass:

datass = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}};
testsOls = LinearModelFit[datass, x, x];

FittedModel[1.2 + 0.5x]

testsOls["FitResiduals"]
{-0.2, -1.7, -0.7, 0.3, 2.3}

However, when I run LinearModelFit in sublists like the following, I get a different type of result:

datas = {{{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}}, 
        {{3, 4}, {5, 6}, {9, 9}, {8, 6}, {5, 5}}};
testOls = LinearModelFit[#, x, x] & @@@ datas

{FittedModel[-1. + 1.x], FittedModel[2. + 1.x]}

Through[testOls["FitResiduals"]]
{{2.22045*10^-16, -2.22045*10^-16}, {-8.88178*10^-16, 0.}}

I ran Clear["Global*'] many times and even exited Mathematica before reopening and rerunning both experiments.

Why do I

  1. Get different FittedModels for the same data (datass = datas[[1]]) and
  2. The Dimensions of my FitResiduals in the second experiment is [2,2] as opposed to [2,5]?
$\endgroup$
2
  • $\begingroup$ use Map (/@), that is, testOls = LinearModelFit[#, x, x] & /@ datas, (not Apply (@@@)). $\endgroup$
    – kglr
    Aug 23, 2018 at 1:54
  • $\begingroup$ @kglr Thanks. It works. But why not Apply? I thought the correct application was f@@@datas = {f[{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}], f[{3, 4}, {5, 6}, {9, 9}, {8, 6}, {5, 5}]}? $\endgroup$ Aug 23, 2018 at 1:58

1 Answer 1

3
$\begingroup$
  1. You can use Map (/@) in place of Apply (@@@):

testOls = LinearModelFit[#, x, x] & /@ datas

{FittedModel[1.2 + 0.5 x], FittedModel[2. + 0.666667 x]}

  1. If you have to use Apply (@@@) you can do

LinearModelFit[{##}, x, x] & @@@ datas

{FittedModel[1.2 +0.5 x],FittedModel[2. +0.666667 x]}

... why not Apply?

To see why does LinearModelFit[#, x, x] & @@@ datas not work try

foo[#, x, x] & @@@ datas

{foo[{0, 1}, x, x], foo[{3, 4}, x, x]}

That is, when you use @@@ you are effectively using

{LinearModelFit[{0, 1}, x, x], LinearModelFit[{3, 4}, x, x]}

which, correctly, gives

{FittedModel[-1.+1. x],FittedModel[2. +1. x]}

$\endgroup$
2
  • $\begingroup$ What I still find hard to understand is the following: f @@@ datas returns the whole list {f[{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}], f[{3, 4}, {5, 6}, {9, 9}, {8, 6}, {5, 5}]}. I would expect that foo[#, x, x] & @@@ datas returned {foo[{{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}}, x, x], foo[{{3, 4}, {5, 6}, {9, 9}, {8, 6}, {5, 5}}, x, x]} the whole list. But no. Instead, Map returns foo with the whole list. Where is the error in my reasoning? $\endgroup$ Aug 24, 2018 at 0:58
  • $\begingroup$ @ThadeuFreitasFilho, foo[##, x, x] & @@@ datas (note the SlotSequence (##)) returns {foo[{0, 1}, {1, 0}, {3, 2}, {5, 4}, {1, 4}, x, x], foo[{{3, 4}, {5, 6}, {9, 9}, {8, 6}, {5, 5}}, x, x]}, that is it picks all slots from datas; and foo[#, x, x] & @@@ datas picks the first slot (# which is the same as #1) from datas and foo[#4, x, x] & @@@ datas picks the forth slot etc. foo[{##}, x, x] & @@@ datas picks all slots wrapped in List ({ ..}. Hope this helps. $\endgroup$
    – kglr
    Aug 24, 2018 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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