Stack several trait columns into the long format used by multi-trait models
stackTraits.RdConverts a wide data frame (one column per trait) into the long format used by
multi-trait mmes models: one row per record and trait, a factor
identifying the trait, a single response column and a record key that pairs
the traits measured on the same experimental unit.
Arguments
- data
A data frame in wide format.
- traits
Character vector with the names of the trait columns to stack. Their order defines the levels of the trait factor.
- keep
Columns of
datacopied to every stacked row. By default, all columns that are not intraits.- trait
Name of the new trait factor column.
- value
Name of the new response column.
- record
Name of the new record-key factor column.
Details
Rows are stacked trait by trait, so row \(i\) of trait \(t\) is row
\((t-1)n+i\) of the output. The record key is taken from the row names of
data when these are unique and not the default 1..n; otherwise
it is "r1", "r2", .... Missing trait values are kept as NA (they
are handled by the naMethodY rules of mmes, so incomplete records
are allowed).
Using the record key in the residual,
rcov = ~ vsm(usm(trait), ism(record)), pairs the residuals of the traits
of each unit explicitly. With ism(units) the pairing is implied by the
order of the rows within each trait instead.
Value
A data frame with the keep columns and the record,
trait and value columns, with length(traits) * nrow(data)
rows.
Examples
set.seed(17)
n <- 60
DT <- data.frame(id = factor(rep(seq_len(n), each = 3)))
u <- matrix(rnorm(n * 2), n)
DT$trait_a <- u[DT$id, 1] + rnorm(nrow(DT), sd = 0.7)
DT$trait_b <- u[DT$id, 2] + rnorm(nrow(DT), sd = 1)
DTL <- stackTraits(DT, traits = c("trait_a", "trait_b"), keep = "id")
head(DTL)
table(DTL$trait)
# bivariate model with the residual paired through the record key
fit <- mmes(value ~ trait,
random = ~ vsm(usm(trait), ism(id)),
rcov = ~ vsm(usm(trait), ism(record)),
data = DTL, verbose = FALSE)
covmatrix_mmes(fit, 1)$correlation # genetic correlation