I am using LinearModelFit in conjunction with Quiet and Check because I need to control when messages are generated. I think messages leak out from Check.

I am using the following code:

SetAttributes[quietCheck, HoldFirst]
quietCheck[x_] := Module[{res, msg = {}},
  res = Quiet[Check[x, msg = $MessageList; $Failed]];
  <|"result" -> res, "messages" -> msg|>

Let's check that the trap works:


quietCheck[Message[f::argx, 1, 2]]



and evaluating

quietCheck[4 + 35 - 1]



as expected.

Now I will test it on a more realistic scenario. This is the data I'm currently using:

 data = Uncompress[

Evaluating on purpose

f = quietCheck[LinearModelFit[Take[data, 5], Table[x^i, {i, 1, 4}], {x}]]



Notice how there appear to be no messages generated. Next, evaluating




notice that now there's a message generated, when trying to access the "RSquared" 'property' of the previous output.

This can only mean one of two things, either:

  1. FittedModel-the output of LinearModelFit-does not store the R squared value, but it re-calculates it on the fly every time the relevant property is requested; this sounds counter-intuitive but it's possible, or
  2. there's a leak of messages somewhere.

Does anybody have any idea what is happening here?


1 Answer 1


The behavior you see is independent of quietCheck:

In[17]:= LinearModelFit[Take[data, 5], {x, x^2, x^3, x^4}, {x}];

In[18]:= %["RSquared"]

During evaluation of In[18]:= Power::infy: Infinite expression 1/0 encountered.

Out[18]= 1.

meaning your conclusion

FittedModel does not store the R squared value, but it re-calculates it on the fly

must be true.


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.