In the previous chapter, we introduced how linear models are fitted and how coefficients are estimated. However, these coefficients depend on how categorical factors are encoded in the design matrix (for example, using the first level as a reference). As a result, they are not directly expressed in the units of the response variable. In this section, we show how to recover estimated marginal means (EMMs)—also known as least-squares means—from the model coefficients, along with their variances.
1) Estimating coefficients
data <-read.csv("../data/example_1.csv") |>mutate(gen =as.factor(gen), block =as.factor(block))n <-12n_b <-3n_g <-4X <-model.matrix(yield ~1+ block + gen, data = data)y <-matrix(data[, "yield"])print(X)
The \(L\) matrix represents the levels we want to compute. For example, if we want to extract the EMMs of the genotypes, we should include the intercept, the fraction of each block level that we have to add to the mean, and the levels that we want to compute from the genotypes. Notice how L has as many rows as levels are in the factor to be solved.
We will discuss more in detail about the Coefficient Matrix once we start digging into Linear Mixed Models. For now, we will just say that \(C_{ii}\) corresponds to \(Var(\hat\beta)\), so
# Compute the errors to compute sigma^2 y_hat <- X %*% betaerrors <- y - y_hatSSE <-sum(errors^2)sigma_2 <- SSE / (n -6)C_11 <-solve(t(X)%*%X) * sigma_2C_11
We can also use lm to fit the model and extract the coefficients and emmeans to retrieve the estimated marginal means. The function emmeans does the job automatically but it also returns the \(L\) and \(C_{11}\) matrices used, so we can explore them.
mod <-lm(formula = yield ~1+ block + gen, data = data)beta_mod <-coef(mod)mod
BLUEs_diff <- L_diff %*% beta_modrownames(BLUEs_diff) <- diff_mm$contrasts@levels$contrastrownames(L_diff) <-rownames(BLUEs_diff)var_diff <- L_diff %*% C_11_emm %*%t(L_diff)var_diff |>round(4) # Notice how now there are covariances between the levels, since they have one of the genotypes in common