# NOT RUN {
data("aastveit.barley.covs")
data("aastveit.barley.height")
if(require(reshape2) & require(pls)){
# First, PCA of each matrix separately
Z <- acast(aastveit.barley.height, year ~ gen, value.var="height")
Z <- sweep(Z, 1, rowMeans(Z))
Z <- sweep(Z, 2, colMeans(Z)) # Double-centered
sum(Z^2)*4 # Total SS = 10165
sv <- svd(Z)$d
round(100 * sv^2/sum(sv^2),1) # Prop of variance each axis
# Aastveit Figure 1. PCA of height
biplot(prcomp(Z),
main="aastveit.barley - height", cex=0.5)
U <- aastveit.barley.covs
rownames(U) <- U$year
U$year <- NULL
U <- scale(U) # Standardized covariates
sv <- svd(U)$d
round(100 * sv^2/sum(sv^2),1) # Proportion of variance on each axis
# }
# NOT RUN {
# Now, PLS relating the two matrices
m1 <- plsr(Z~U)
loadings(m1)
# Aastveit Fig 2a (genotypes), but rotated differently
biplot(m1, which="y", var.axes=TRUE)
# Fig 2b, 2c (not rotated)
biplot(m1, which="x", var.axes=TRUE)
# }
# NOT RUN {
}
# }
# NOT RUN {
# Adapted from section 7.4 of Turner & Firth,
# "Generalized nonlinear models in R: An overview of the gnm package"
# who in turn reproduce the analysis of Chadoeuf & Denis (1991),
# "Asymptotic variances for the multiplicative interaction model"
require(agridat)
require(gnm)
data("aastveit.barley.height")
dath <- aastveit.barley.height
dath$year = factor(dath$year)
set.seed(1)
m2 <- gnm(height ~ year + gen + Mult(year, gen), data = dath)
# Turner: "To obtain parameterization of equation 1, in which sig_k is the
# singular value for component k, the row and column scores must be constrained
# so that the scores sum to zero and the squared scores sum to one.
# These contrasts can be obtained using getContrasts"
gamma <- getContrasts(m2, pickCoef(m2, "[.]y"),
ref = "mean", scaleWeights = "unit")
delta <- getContrasts(m2, pickCoef(m2, "[.]g"),
ref = "mean", scaleWeights = "unit")
# estimate & std err
gamma <- gamma$qvframe
delta <- delta$qvframe
# change sign of estimate
gamma[,1] <- -1 * gamma[,1]
delta[,1] <- -1 * delta[,1]
# conf limits based on asymptotic normality, Chadoeuf table 8, p. 350,
round(cbind(gamma[,1], gamma[, 1] +
outer(gamma[, 2], c(-1.96, 1.96))) ,3)
round(cbind(delta[,1], delta[, 1] +
outer(delta[, 2], c(-1.96, 1.96))) ,3)
# }
# NOT RUN {
# }
Run the code above in your browser using DataCamp Workspace