Multilevel/Split-plot model: Documentation error in covarianceParameters.m?

Hello,
I try to understand the implementation of nested effects and split plot designs in Matlab.
Is there a mistake in the official description of the fertilizer examples?
I compared
vs
I noticed that the equations derived from the Wilkinson notations are not consistent. For fitlme it seems correct, but for covarianceParameters the term for (1|Soil) is missing, isn't it? I expected it would also be represented by a term b_0k in the equation for the dependent variable y_ijk.
Furthermore, what does the Term b_0k*S_k mean in this case? I thought the term b_0k represents the intercept variance (for the random soil effect) and is a random number drawn from its corresponding normal distribution N(0,sigma_S^2). But it seems to be multiplied with another factor S_k which represents soil type and is a categorical?
Best regards

1 Kommentar

Torsten
Torsten am 27 Nov. 2026 um 18:31
Bearbeitet: Torsten am 27 Nov. 2026 um 18:32
AI confirms that you have spotted two real documentation errors (typos) in the MathWorks covarianceParameters page, and your intuition about the mathematical notation is spot on.
You should report it to
to get confirmation from "human" TMW employees.
Here is a breakdown of both issues in the documentation.
1. The Missing Term (and Mismatched Fixed Effects) in covarianceParameters
In the covarianceParameters example, the code runs the following formula:
matlab
lme = fitlme(tbl,"Yield ~ Fertilizer + (1|Soil) + (1|Soil:Tomato)");
However, the mathematical equation written right above it is:
There are two typos in this documentation equation compared to the MATLAB code:
  1. Missing (1|Soil) term: Just as you expected, the random intercept for soil is missing from the formula, even though the text right below it explicitly defines its prior distribution and the code includes (1|Soil).
  2. Wrong fixed effect variable: The code specifies Fertilizer as the fixed effect, yet the equation sums over instead of Fertilizer .
A corrected equation matching the code on that page should look like:
2. What Does Mean in the fitlme Example?
In the fitlme documentation, the equation is written as:
Your understanding is completely right: is already a scalar random realization drawn from for the k-th soil type. You do not multiply a random effect by a categorical label.
Here is why MathWorks wrote it that way (and why it is slightly abused notation):
  • What it actually means: In matrix notation (), the random-effects design matrix Z consists of dummy/indicator variables (0s and 1s) that "turn on" the specific random effect belonging to soil k.
  • Why it's confusing: In the fitlme text, MathWorks states: " represents the k th soil type". Strictly speaking, should either be defined as a dummy indicator variable 1 if observation i is in soil k, 0 otherwise, summed over , or if k is already fixed as the index of the observation's soil type (as in ), you simply write without . Multiplying by a categorical factor makes no mathematical sense—MathWorks informally wrote next to just as a label to remind the reader which grouping factor belongs to.
Summary of How MATLAB Actually Evaluates It
When you run fitlme(tbl, "Yield ~ Fertilizer + (1|Soil) + (1|Soil:Tomato)"), MATLAB constructs:
  • Fixed effects (\(X\)): Intercept + 3 dummy columns for Fertilizer (4 levels total).
  • Random effects (\(Z\)):
  • 3 dummy columns for Soil (corresponding to ).
  • 15 dummy columns for the Soil:Tomato combinations (3 soils × 5 tomatoes, corresponding to ).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Umar
Umar vor etwa 19 Stunden
Bearbeitet: Umar vor etwa 19 Stunden
Hi @Dr Zoidberg,
Your suspicion is well founded: the equation on the covarianceParameters page (R2026b) is inconsistent with the code beside it.
The example fits Yield ~ Fertilizer + (1|Soil) + (1|Soil:Tomato), yet the printed equation omits the soil random intercept b_0k (whose prior the next sentence states) and sums over tomato dummies I[T] instead of fertilizer dummies I[F]. The fitlme page is coherent, so the covarianceParameters equation looks like a truncated copy of it. A consistent version would be:
y_imjk = beta_0 + sum over m = 2 to 4 of beta_1m * I[F]_im + b_0k + b_0jk + epsilon_imjk
On b_0k * S_k: you are right that b_0k is a single draw from N(0, sigma_S^2) per soil level. S_k is only informal shorthand for an indicator that selects the correct b_0k for each observation. In y = X*beta + Z*b + epsilon, the columns of Z are exactly those zero-or-one indicators, so the notation is loose, not meaningful.
To test this empirically, I wrote the attached script and ran it in MATLAB Mobile. Because I did not have the original fertilizer.mat, the script first builds a simulated dataset with the same structure (3 soils, 5 tomato varieties, 4 fertilizers, 60 observations), saves it as fertilizerSim.mat, loads that file back, and then fits two models: the one the documentation code specifies, and the one the printed equation describes (Yield ~ Tomato + (1|Soil:Tomato)). The simulated values are my own, so the variance estimates will differ from the documentation's. The design-matrix dimensions, however, depend only on the structure of the data, so I would expect them to be the same for the genuine file, though I have not run it on that file.
Results from the saved and reloaded .mat file:
1. Code model: 60 x 4 fixed-effects matrix (intercept plus three Fertilizer dummies, no Tomato terms), 60 x 18 random-effects matrix (3 Soil plus 15 Soil:Tomato indicators), and two random-effects terms.
2. Equation model: 60 x 5 fixed-effects matrix (intercept plus four Tomato dummies), 60 x 15 random-effects matrix, and only one random-effects term.
These are structurally different models, so the printed equation cannot describe the code it accompanies. Please open the attached script and run it to see the full output; it prints a pass or fail verdict for each check.
I would report this as a documentation error via the feedback link on the page. Also, your title says covariancePattern.m, but the page you mean is covarianceParameters.
Hope this helps.

2 Kommentare

Hi Umar, thank you very much for your effort! I replaced covariancePattern by covarianceParameters in the title.
Hi @DrZoidberg, You're welcome, and thank you for fixing the title. Good luck 👍.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Agriculture finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2026b

Gefragt:

am 27 Nov. 2026 um 11:38

Kommentiert:

vor etwa 4 Stunden

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by