I'm generating a simple linear model ($y=mx+b$) by fitting to the background of some data, and then trying to plug in the x-values of the data I'm actually interested in.
I'm a bit surprised that FittedModel
s aren't listable, so I tried using Map
. This turned out to be exceedingly slow. For example:
lm = LinearModelFit[Table[{x, 3 x + 6.2}, {x, 100}], x, x];
AbsoluteTiming[lm/@Range[300000];]
AbsoluteTiming[3 Range[300000] + 6.2;]
AbsoluteTiming[3 # + 6.2&/@Range[300000];]
AbsoluteTiming[Normal[lm]/.x -> Range[300000];]
(* {25.037, Null} *)
(* {0.002096, Null} *)
(* {0.010351, Null} *)
(* {0.004413, Null} *)
Using Normal
seems to be the fastest way of using the linear model. Is this really the best way to extract values from a FittedModel
?
What is the best (or most usual) way to extract many values from a model?
This seems like it would be a common problem, so I'm certain there must be something about it either here or elsewhere, but perhaps I'm not using the correct search terms.