Closed MarcRieraDominguez closed 8 months ago
Hmmmm. Well the issue seems to be the way I try to match the averaged coefficients with columns in the model matrix. In one part of the code, I get the model matrix for each variable instead of each term -- which worked for many models but not this one. I'll see what I can do. These averaged models are really dicey because they often include coefficients that are not in the main model,, such as for the first level of a factor. This could take me a while to puzzle through.
I posted an earlier comment that was dumb because it was based on the wrong model. However, I do now get:
> emmeans(quad.poly.avg, specs = pairwise ~ am, data = mtcars)
$emmeans
am emmean SE df lower.CL upper.CL
0 207 15.8 27 174 239
1 197 19.6 27 157 237
Results are given on the poly (not the response) scale.
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
am0 - am1 10 22.8 27 0.439 0.6643
Note: contrasts are still on the poly scale
Warning message:
In (function (object, at, cov.reduce = mean, cov.keep = get_emm_option("cov.keep"), :
There are unevaluated constants in the response formula
Auto-detection of the response transformation may be incorrect
The stuff about response transformations is mysterious and annoying... but similar to what you got with manual specs. The estimates differ somewhat from what you have. Not sure why.
I think I've got it now; will push up soon. The results for quad.i.avg
are different because the coefficients are different -- check it out. And I have figured out what was happening with the warnings about response transformations, and we don't get those any more.
Hi Russell, I've tested version 1.8.9, I no longer get errors with averaged models with poly() in the formula. I'll be closing this issue. Thank you very much!
Describe the bug
I am unable to obtain estimated marginal means from model-averaged objects (class
averaging
, obtained withMuMIn
package), when the model-averaged objects contain a quadratic term fitted withpoly(x, 2, raw = TRUE)
. Quadratic terms fitted withI(x^2)
return a warning but yield a sensible result. The error withpoly()
appears to be related to parsing, which is completely outside of my current R knowledge.Quadratic terms and model averaging are discussed in the documentation, but are not considered at the same time.
To reproduce
Created on 2023-10-12 with reprex v2.0.2
Created on 2023-10-12 with reprex v2.0.2
Expected behavior
The estimated marginal means, as calculated by
emmeans
on objects that contain quadratic terms fitted withI(x^2)
.Additional context
I came across this when working with model-averaged binomial GLMs (n successes / k failures), containing pairwise interactions (numerical:categorical, numerical:numerical), and polynomial terms fitted with
poly(x, 2, raw = TRUE)
.