Closed Generalized closed 1 month ago
The qdrg()
function does provide a contrasts
argument, and that can be used to ensure that the right contrasts are generated:
``` r
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
foo = ubds
foo$B = ordered(foo$B)
attr(foo$C, "contrasts") = "contr.helmert"
mod = lm(y ~ A + B*C, data = foo)
rg = qdrg(formula = ~ A + B*C, data = foo, coef = coef(mod),
vcov = vcov(mod), df = df.residual(mod),
contrasts = attr(model.matrix(mod), "contrasts"))
head(rg@linfct)
## (Intercept) A2 A3 B.L B.Q C1 C2 B.L:C1 B.Q:C1
## 1 1 0 0 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483
## 2 1 1 0 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483
## 3 1 0 1 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483
## 4 1 0 0 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966
## 5 1 1 0 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966
## 6 1 0 1 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966
## B.L:C2 B.Q:C2
## 1 7.071068e-01 -0.4082483
## 2 7.071068e-01 -0.4082483
## 3 7.071068e-01 -0.4082483
## 4 7.850462e-17 0.8164966
## 5 7.850462e-17 0.8164966
## 6 7.850462e-17 0.8164966
Created on 2024-07-10 with reprex v2.1.0
That said, without the contrasts
argument, it defaults to "contr.treatment"
for all three. It does seem that it could be doing a better job with defaults, and I will look at it.
OK, I fixed this so the right contrasts are used by default:
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
options(width = 110)
foo = ubds
foo$B = ordered(foo$B)
attr(foo$C, "contrasts") = "contr.helmert"
mod = lm(y ~ A + B*C, data = foo)
rg = qdrg(formula = ~ A + B*C, data = foo, coef = coef(mod),
vcov = vcov(mod), df = df.residual(mod))
head(rg@linfct)
## (Intercept) A2 A3 B.L B.Q C1 C2 B.L:C1 B.Q:C1 B.L:C2 B.Q:C2
## 1 1 0 0 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483 7.071068e-01 -0.4082483
## 2 1 1 0 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483 7.071068e-01 -0.4082483
## 3 1 0 1 -7.071068e-01 0.4082483 -1 -1 7.071068e-01 -0.4082483 7.071068e-01 -0.4082483
## 4 1 0 0 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966 7.850462e-17 0.8164966
## 5 1 1 0 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966 7.850462e-17 0.8164966
## 6 1 0 1 -7.850462e-17 -0.8164966 -1 -1 7.850462e-17 0.8164966 7.850462e-17 0.8164966
Created on 2024-07-11 with reprex v2.1.0
I think that resolves this issue. Thanks for calling it to my attention.
Thank you very, very much for this quick fix!
I needed to git a GEE model for numerical data and wanted to assess the trends in means. I worked with multiply imputed data. It worked, but I needed to use a feature of GEE that emmeans did not support.
Therefore, I needed to use qdrg() over pooled myself coefficients and the necessary covariance estimator. Unfortunately, qdrg() ignored the fact, that in the pooled beta estimates I provided "Visit" variable was an ordinal factor, and treated it as an unordered one. Finally, the grid was incorrect.
It failed because qdrg() treated Visit as an unordered factor, rather than ordered one. The "L", "Q" and "C" columns were missing, despite the fact it existed in my pooled vector:
I followed your advice and it worked well (I publish the code in a separate issue to not make this excessively long: https://github.com/rvlenth/emmeans/issues/502):
By using your approach I was able get rid of qdrg() this time only because emmeans knows the glmtoolbox package. If it did not, I would have to use qdrg() anyway.
My question is: Is there a way to tell the qdrg() to "make use" of ordered predictors?** so it can create appropriate
lincft
contrasts?