1
$\begingroup$

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:

ClearAll[quietCheck]
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:

evaluating

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

returns

<|result->$Failed,messages->{f::argx}|>

and evaluating

quietCheck[4 + 35 - 1]

returns

<|result->38,messages->{}|>

as expected.

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

 data = Uncompress[
   "1:eJx91X9IE2EYB/BjDRERE5GwElOJMqTCQERieBrDRE00EFkRY8jYRZp4a95WCCEhw2z5YyhYgeYsZUmEjRERK8zMimpJIVEhohESkpouW9Bzb/9E7/e6Pwbjw3Pvj+fHZVhOV1krBEGQ9fRTJsl2q+7vf411n38Of4s4RGlqkyD8YwW5J+6VDDRBy7jdaOvow3ZzojTP7SOb5k2X3bplJkURpY+8vVw/Oue7S3EB3tayjnseGbCxfSZhk20dX8+tO6Dlj006HiyR+Xk7Ykmsye8kG+Dt2tKNidJbOG45XG5JDOK4w7Ou5FEfNpaHcbzP3vqkllAQ29nk0eLZKLY3tJWaY01wPXa+S03wDIGVX9OKhI2dXcPcheb5V1ZseSrGk13mzRD37nrDVmwsbpnO18vbyfv2rhQzmZs39TrDxWSdGjU4gvNH1VKQO4TXYzaI83fwU0V7jwvHVSfsvXN1Hq/3ZVdOrH8bvjO1rOPD/8lRHa55lge7AvfSEprqN646Rekib6ZaOsVTMhdvrzPf/2hewPY4nYbBHrIejRy9VeAs6ErRVyfMYGM5silwvkQpteUiWYi3MrVgDDiOblOXfQbHxfpTTbUyjmM93Y3nGXXR2OQC2Spvgvr4yRZ5e9ZvDKxspvXWeHtC47MwkyyiURMmbOx8HvxO6jBDXA5ZVCNHaWQxes5o6n7PGiLb4ONiznsPtSl4ll9YrAymteK7ZvdpVmAfsXkmOGHttp2irtaYE2r71e/DcayWNIz1X5ET9lgRtVGm0Ql7bDeFpYpk3bxtNNONVpK18/aQ2i/glUWpgbc5X994+qAMe4zNXS82ltthMg9vI9Qqlisy7Hc2CxSyNt52qAmskuH83L7/w86YUvzOCH3C+w6QeTW+tyX4DC+e0xMn/8nRb744YpA="
  ];

Evaluating on purpose

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

returns

<|result->FittedModel[-2.05346*10^10+<<6>>],messages->{}|>

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

quietCheck[f["result"]["RSquared"]]

returns

<|result->$Failed,messages->{Power::infy}|>

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?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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