# Trend (orthogonal-polynomial) analysis decomposes the omnibus effect of a
# quantitative factor (dose, time, trial block, stimulus intensity) into
# independent linear, quadratic, cubic, ... components, as in the trend
# analysis of Maxwell, Delaney, and Kelley (2027, Chapter 6).
# 1. The a = 4 coefficient table in the integer form of the textbook
# appendix (Table A.10). Printing puts the trends in rows and appends a
# final sum-of-squared-coefficients (sum c^2) column: linear -3 -1 1 3,
# and per-trend sum c^2 of 20, 4, 20.
orthogonal_polynomial(4, type = "integer")
# 2. The default orthonormal form (identical to stats::contr.poly(4)); every
# trend has sum c^2 = 1, the scale lm() and aov() use internally.
orthogonal_polynomial(4)
# 3. A worked trend analysis in the style of MDK Chapter 6. An outcome is
# measured at a = 5 equally spaced levels of a quantitative factor (say
# stimulus intensity 1..5), n = 8 per level, from a population with a
# strong linear and a mild quadratic trend. Assigning the trend contrasts
# to the factor makes lm() report each trend test directly.
set.seed(113)
intensity <- factor(rep(1:5, each = 8))
contrasts(intensity) <- orthogonal_polynomial(levels(intensity))
y <- c(10, 9, 7, 6, 6)[as.integer(intensity)] +
rnorm(length(intensity), sd = 1.2)
round(coef(summary(lm(y ~ intensity))), 3)
# 4. The same trends through DMAR's contrast_test(), which reports each
# trend's estimate, standard error, t, p, and confidence interval from a
# fitted one-way model. The integer columns are the contrast weights.
op <- orthogonal_polynomial(5, type = "integer")
contrast_test(aov(y ~ intensity),
contrasts = list(linear = op[, "linear"],
quadratic = op[, "quadratic"],
cubic = op[, "cubic"]))
# 5. Unequally spaced doses (0, 1, 2, 4 mg): integer coefficients no longer
# exist, but the orthonormal trends remain uniquely defined.
orthogonal_polynomial(c("0 mg", "1 mg", "2 mg", "4 mg"),
scores = c(0, 1, 2, 4))
# 6. Confirm a returned set is mutually orthogonal.
is_orthogonal_set(orthogonal_polynomial(5, type = "integer"))
Run the code above in your browser using DataLab