runehaubo / lmerTestR

Repository for the R-package lmerTest
48 stars 9 forks source link

Bugs in lmerTest #51

Open hsiangw opened 2 years ago

hsiangw commented 2 years ago

Hello,

I have been doing some testing with your lmerTest package and I think I have come across one issue in the accuracy of calculations and one user need that is somewhat important if the goal is to create output that is consistent with SAS lsmeans.

First, there appears to be an issue with Type II and III ANOVA when the model does not have fixed intercept (see: https://stats.stackexchange.com/questions/577654/mixed-model-without-fixed-intercept-numerator-df-for-type-ii-and-iii-anova-lme).

I believe if you make the following modification in single_anova(), after ddf <- match.arg(ddf) this issue would be resolved.

tempterm <- terms(object) allvar <- attr(terms(object),"term.labels")[!grepl("\:",attr(terms(object),"term.labels"))] isfactor <- names(Filter(is.factor, object@frame%>% dplyr::select(attr(terms(object),"term.labels")[!grepl("\:",attr(terms(object),"term.labels"))])))

checkfactor <- identical(sort(allvar),sort(isfactor))

if(attr(tempterm,"intercept")==0 & (type %in% c(2,3))&checkfactor){ object <- update(object, . ~ . + 1) warning("lmerTest: model without intercept could give inaccurate results for the first effect. The remaining effects are still valid for analysis. ") }

Second, there appears to be an issue of the ordering of effects in lmerTest vs how SAS handles effect ordering which makes direct matching a little problematic. One suggestion I would like to propose is modifying the lsmeans_contrast() with an additional argument match_sas (default is true)

New code might look like this…

L <- t(sweep(Lt, 2, wts, "*"))

L2 <- L %*% uX if(match_sas){ L2 <- L2[sort(rownames(L2)),] } L2

Instead of

L <- t(sweep(Lt, 2, wts, "*"))

L %*% uX

I am hoping you would consider making these changes, most especially the change related to inaccurate results with a fixed intercept.

Thanks