Simplified version Using the simpler version suggested by @ulrich-neumann :
data1 = Table[{i, i + 2}, {i, 1, 10}]
data2 = Table[3 i + 2, {i, 1, 10}]
fd = LinearModelFit[{data1, data2}]
testData = Table[{i, i + 2}, {i, 1, 3}](*{{1, 3}, {2, 4}, {3, 5}}*)
Map[Apply[fd, #] &, testData] (* {5., 8., 11.} WORKS AS EXPECTED *)
`
Now I want to embed this in a function :
testFunc1[f1_, data_] := Map[Apply[f1, #] &, data]
testFunc2[f1_, data_] := Table[Apply[f1, data[[i]]] , {i, 1, Length[data]}]
testFunc3[f1_, data_] := f1 @@@ data (* as suggested by @MarcoB *)
testFunc4[f1_, data_] := {#, f1 @@ #} & /@ data
I expect the results to be the same for testFunc1,2,3 but they aren't:
testFunc2[fd, testData] (* {5., 8., 11.} *)
testFunc3[fd, testData] (* {5., 8., 11.} *)
but
testFunc1[fd, testData] (* error messages plus {{{1, 3}, {2., 1.}.{{1, 3}, 3}}, .... *)
testFunc4[fd, testData] (* error messages plus {{{1, 3}, {2., 1.}.{{1, 3}, 3}}, .... *)
Question : Why do these not give the same output ?
original question I am generating a FittedModel and want to evaluate it on a list of data. This works fine when I use Table to generate the results, but not when I try to use Map. The simple example below takes two inputs x and x+2, and produces the output 3x+2. The fitted model works as expected, until I try to Map it over a list of inputs.
With[{data1 = Table[{i, i + 2}, {i, 1, 10}],
data2 = Table[3 i + 2, {i, 1, 10}]},
With[{fd = LinearModelFit[{data1, data2}],
testData = Table[{i, i + 2}, {i, 1, 3}]}, Print["function is ", fd];
(* check it works *)
Print["function applied to {4,6} is ", fd[4, 6]];
(* explicitly generate input data *)
Print[Grid[
Table[{"apply ", fd, " to args ", {i, i + 2}, " result:",
Apply[fd, {i, i + 2}]}, {i, 1, Length[testData]}],
Frame -> True]];
(* Use Table to find the value over test data *)
Print[Grid[
Table[{"apply ", fd, " to args ", testData[[k]], " result:",
Apply[fd, testData[[k]]]}, {k, 1, Length[testData]}],
Frame -> True]];
(* Try to map over test data *)
Print[Grid[
Map[{"apply ", fd, " to args ", #, " result:", Apply[fd, #]} &,
testData], Frame -> True]]]]
Output looks like this:
so I am guessing the FittedModel somehow picks up the wrong arguments when I try to use Map. Is there a way to make Map behave the same as Table ?
fd @@@ testData
or{#, fd @@ #}& /@ testData
? Do those work? $\endgroup$