if (FALSE) {
# Examples use geometric morphometric data
# See the package, geomorph, for details about obtaining such data
data("PupfishHeads")
names(PupfishHeads)
# Head Size Analysis (Univariate)-------------------------------------------------------
fit <- lm.rrpp(log(headSize) ~ sex + locality/year, SS.type = "I",
data = PupfishHeads, print.progress = FALSE, iter = 999)
summary(fit)
anova(fit, effect.type = "F") # Maybe not most appropriate
anova(fit, effect.type = "Rsq") # Change effect type, but still not
# most appropriate
# Mixed-model approach (most appropriate, as year sampled is a random
# effect:
anova(fit, effect.type = "F", error = c("Residuals", "locality:year",
"Residuals"))
# Change to Type III SS
fit <- lm.rrpp(log(headSize) ~ sex + locality/year, SS.type = "III",
data = PupfishHeads, print.progress = FALSE, iter = 999,
verbose = TRUE)
summary(fit)
anova(fit, effect.type = "F", error = c("Residuals", "locality:year",
"Residuals"))
# Coefficients Test
coef(fit, test = TRUE)
# Predictions (holding alternative effects constant)
sizeDF <- data.frame(sex = c("Female", "Male"))
rownames(sizeDF) <- c("Female", "Male")
sizePreds <- predict(fit, sizeDF)
summary(sizePreds)
plot(sizePreds)
# Diagnostics plots of residuals
plot(fit)
# Body Shape Analysis (Multivariate) -----------
data(Pupfish)
names(Pupfish)
# Note:
dim(Pupfish$coords) # highly multivariate!
fit <- lm.rrpp(coords ~ log(CS) + Sex*Pop, SS.type = "I",
data = Pupfish, print.progress = FALSE, iter = 999,
verbose = TRUE)
summary(fit, formula = FALSE)
anova(fit)
coef(fit, test = TRUE)
# Predictions (holding alternative effects constant)
shapeDF <- expand.grid(Sex = levels(Pupfish$Sex),
Pop = levels(Pupfish$Pop))
rownames(shapeDF) <- paste(shapeDF$Sex, shapeDF$Pop, sep = ".")
shapeDF
shapePreds <- predict(fit, shapeDF)
summary(shapePreds)
summary(shapePreds, PC = TRUE)
# Plot prediction
plot(shapePreds, PC = TRUE)
plot(shapePreds, PC = TRUE, ellipse = TRUE)
# Diagnostics plots of residuals
plot(fit)
# PC-plot of fitted values
groups <- interaction(Pupfish$Sex, Pupfish$Pop)
plot(fit, type = "PC", pch = 19, col = as.numeric(groups))
# Regression-like plot
plot(fit, type = "regression", reg.type = "PredLine",
predictor = log(Pupfish$CS), pch=19,
col = as.numeric(groups))
# Body Shape Analysis (Distances) ----------
D <- dist(Pupfish$coords) # inter-observation distances
length(D)
Pupfish$D <- D
fitD <- lm.rrpp(D ~ log(CS) + Sex*Pop, SS.type = "I",
data = Pupfish, print.progress = FALSE, iter = 999)
# These should be the same:
summary(fitD, formula = FALSE)
summary(fit, formula = FALSE)
# GLS Example (Univariate) -----------
data(PlethMorph)
fitOLS <- lm.rrpp(TailLength ~ SVL, data = PlethMorph,
print.progress = FALSE, iter = 999)
fitGLS <- lm.rrpp(TailLength ~ SVL, data = PlethMorph, Cov = PlethMorph$PhyCov,
print.progress = FALSE, iter = 999)
anova(fitOLS)
anova(fitGLS)
sizeDF <- data.frame(SVL = sort(PlethMorph$SVL))
# Prediction plots
# By specimen
plot(predict(fitOLS, sizeDF)) # Correlated error
plot(predict(fitGLS, sizeDF)) # Independent error
# With respect to independent variable (using abscissa)
plot(predict(fitOLS, sizeDF), abscissa = sizeDF) # Correlated error
plot(predict(fitGLS, sizeDF), abscissa = sizeDF) # Independent error
# GLS Example (Multivariate) -----------
Y <- as.matrix(cbind(PlethMorph$TailLength,
PlethMorph$HeadLength,
PlethMorph$Snout.eye,
PlethMorph$BodyWidth,
PlethMorph$Forelimb,
PlethMorph$Hindlimb))
PlethMorph$Y <- Y
fitOLSm <- lm.rrpp(Y ~ SVL, data = PlethMorph,
print.progress = FALSE, iter = 999)
fitGLSm <- lm.rrpp(Y ~ SVL, data = PlethMorph,
Cov = PlethMorph$PhyCov,
print.progress = FALSE, iter = 999)
anova(fitOLSm)
anova(fitGLSm)
# Prediction plots
# By specimen
plot(predict(fitOLSm, sizeDF)) # Correlated error
plot(predict(fitGLSm, sizeDF)) # Independent error
# With respect to independent variable (using abscissa)
plot(predict(fitOLSm, sizeDF), abscissa = sizeDF) # Correlated error
plot(predict(fitGLSm, sizeDF), abscissa = sizeDF) # Independent error
}
Run the code above in your browser using DataLab