Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass contrasts for the fixed effects #63

Open
timflutre opened this issue Oct 22, 2024 · 2 comments
Open

Allow to pass contrasts for the fixed effects #63

timflutre opened this issue Oct 22, 2024 · 2 comments

Comments

@timflutre
Copy link

A minor, yet useful enhancement would be to allow the user to specify contrasts for the fixed effects.

By looking at the code of mmer, a contrasts argument could be used on line 261 baseX <- model.matrix(newfixed, mf) maybe? But I don't know how it would work with multivariate models...

@covaruber
Copy link
Owner

Could you elaborate a more complete example using another software like base lm or lmer to understand a bit more what do you mean? With some mock up data if possible.

Cheers,

@timflutre
Copy link
Author

Sure, sorry, here it is:

library(lme4)
library(sommer)

## simulate some data with a LMM y = X beta + Z u + epsilon:
set.seed(12345)
p <- 3
q <- 20
n <- p * q
dat <- expand.grid(geno=paste0("geno", 1:q),
                   block=LETTERS[1:p],
                   KEEP.OUT.ATTRS=FALSE)
str(dat)
head(dat)

## listContr <- list("block"="contr.treatment")
listContr <- list("block"="contr.sum")
X <- model.matrix(~ block, dat, contrasts.arg=listContr)
beta <- rnorm(p)

Z <- model.matrix(~ 0 + geno, dat)
u <- rnorm(q)

y <- X %*% beta + Z %*% u + rnorm(n)
dat$yield <- y

## plot the data:
hist(dat$yield)
boxplot(yield ~ block, dat)

## fit with lmer
fit1 <- lmer(yield ~ block + (1|geno), dat, contrasts=listContr)
cbind("truth"=beta, "estim"=fixef(fit1))

## fit with mmer:
fit2 <- mmer(yield ~ block, ~ geno, data=dat)
cbind("truth"=beta, "estim"=fit2$Beta[,"Estimate"])

By default for unordered factor, R chooses the "treatment" contrasts. But I often uses the "sum" contrast instead for my simulations. It would be great if we could give listContr (see above) to mmer.

p.s.: see here for more details about coding and contrast matrices in R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants