library(treeman) # Import mammals tree from treeman package - based on Bininda-Emonds et al. (2007) data(mammals) mammal_tree <- as(mammals, 'phylo') library(geiger) # Species are missing from the mammals phylogeny - tips will be added at the appropriate nodes # Add Felis catus mammal_tree_age <- getAge(mammals) possible_ages <- getSpnAge(mammals, 'n4039', mammal_tree_age) start_age <- runif(1, possible_ages[['end']], possible_ages[['start']]) end_age <- possible_ages[['end']] one_species_added <- addTip(mammals, tid='Felis_catus', sid='Felis_silvestris', strt_age=start_age, end_age=end_age, tree_age=mammal_tree_age, pid = 'n4041') summary(one_species_added) # Add Leopardus geoffroyi possible_ages2 <- getSpnAge(one_species_added, 'Caracal_caracal', mammal_tree_age) start_age2 <- runif(1, possible_ages2[['end']], possible_ages2[['start']]) end_age2 <- possible_ages2[['end']] two_species_added <- addTip(one_species_added, tid='Leopardus_geoffroyi', sid='Caracal_caracal', strt_age=start_age2, end_age=end_age2, tree_age=mammal_tree_age, pid = 'n4049') summary(two_species_added) summary(two_species_added[['Meles_meles']]) summary(two_species_added[['n3835']]) # Add Meles anakuma mammal_tree_age3 <- getAge(two_species_added) possible_ages3 <- getSpnAge(two_species_added, 'Meles_meles', mammal_tree_age3) start_age3 <- runif(1, possible_ages3[['end']], possible_ages3[['start']]) end_age3 <- possible_ages3[['end']] three_species_added <- addTip(two_species_added, tid='Meles_anakuma', sid='Meles_meles', strt_age=start_age3, end_age=end_age3, tree_age=mammal_tree_age3, pid = 'n3785') summary(three_species_added) # Add Canis familiaris mammal_tree_age4 <- getAge(three_species_added) possible_ages4 <- getSpnAge(three_species_added, 'Canis_lupus', mammal_tree_age4) start_age4 <- runif(1, possible_ages4[['end']], possible_ages4[['start']]) end_age4 <- possible_ages4[['end']] four_species_added <- addTip(three_species_added, tid='Canis_familiaris', sid = 'Canis_lupus', strt_age=start_age4, end_age=end_age4, tree_age=mammal_tree_age4, pid = 'n3943') four_tree <- as(four_species_added, 'phylo') name.check(four_tree, R_Thigh) summary(four_species_added[['Mogera_etigo']]) # Add Mogera Imaizumii mammal_tree_age5 <- getAge(four_species_added) possible_ages5 <- getSpnAge(four_species_added, 'Mogera_etigo', mammal_tree_age4) start_age5 <- runif(1, possible_ages5[['end']], possible_ages5[['start']]) end_age5 <- possible_ages5[['end']] five_species_added <- addTip(four_species_added, tid='Mogera_imaizumii', sid = 'Mogera_etigo', strt_age=start_age5, end_age=end_age5, tree_age=mammal_tree_age5, pid = 'n5581') summary(five_species_added[['Pongo_pygmaeus']]) # Add Pongo abelii mammal_tree_age6 <- getAge(five_species_added) possible_ages6 <- getSpnAge(five_species_added, 'Pongo_pygmaeus', mammal_tree_age5) start_age6 <- runif(1, possible_ages6[['end']], possible_ages6[['start']]) end_age6 <- possible_ages6[['end']] PGLS_Species <- addTip(five_species_added, tid='Pongo_abelii', sid = 'Pongo_pygmaeus', strt_age=start_age6, end_age=end_age6, tree_age=mammal_tree_age5, pid = 'n2942') PGLS_Tree <- as(PGLS_Species, 'phylo') # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Submission/Open Science Submission/Open Science Specimens/Code/Segments") L_Arm <- read.delim("Left arms.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Arm) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Arm_trimmed <- name.check(PGLS_Tree, L_Arm) L_Arm_Tree <- drop.tip(PGLS_Tree, L_Arm_trimmed$tree_not_data) any(is.na(L_Arm)) name.check(L_Arm_Tree, L_Arm) plot(L_Arm_Tree) library(caper) # Combine phylogeny and data Species<-row.names(L_Arm) L_Arm2<-cbind(Species,L_Arm) LArm<-comparative.data(phy = L_Arm_Tree, data = L_Arm2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LArm$dropped$tips LArm$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = LArm, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) library(ggplot2) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = LArm, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=L_Arm2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Arm, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LArm, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Arm2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Arm, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LArm, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Arm2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Arm, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LArm, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Arm2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Arm, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = LArm, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=L_Arm2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Arm, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = LArm, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=L_Arm2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Arm, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = LArm, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=L_Arm2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Arm, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LArm, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Arm2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Arm, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LArm, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Arm2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Arm, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LArm, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Arm2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() library(huxtable) library(nlme) # Summarise coefficients L_Arm_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Arm_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Arm_pgls_coef, "L Arm PGLS Coefficients") write.csv(L_Arm_ols_coef, "L Arm OLS Coefficients") # AIC and confidence intervals ArmAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) library(caret) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Arm, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Arm, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Arm, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Arm, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Arm, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Arm, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Arm, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Arm, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Arm, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Arm, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Left Forearm - same protocol # Import data L_Forearm <- read.delim("Left Forearms.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Forearm) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Forearm_trimmed <- name.check(PGLS_Tree, L_Forearm) L_Forearm_Tree <- drop.tip(PGLS_Tree, L_Forearm_trimmed$tree_not_data) any(is.na(L_Forearm)) name.check(L_Forearm_Tree, L_Forearm) plot(L_Forearm_Tree) # Combine phylogeny and data Species<-row.names(L_Forearm) L_Forearm2<-cbind(Species,L_Forearm) LForearm<-comparative.data(phy = L_Forearm_Tree, data = L_Forearm2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LForearm$dropped$tips LForearm$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = LForearm, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(L_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = LForearm, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=L_Forearm2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Forearm, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LForearm, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Forearm2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Forearm, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LForearm, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Forearm2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Forearm, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LForearm, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Forearm2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Forearm, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = LForearm, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=L_Forearm2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Forearm, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LForearm, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=L_Forearm2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Forearm, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LForearm, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=L_Forearm2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Forearm, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LForearm, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Forearm2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Forearm, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LForearm, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Forearm2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Forearm, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LForearm, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Forearm2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients L_Forearm_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Forearm_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Forearm_pgls_coef, "L Forearm PGLS Coefficients") write.csv(L_Forearm_ols_coef, "L Forearm OLS Coefficients") # AIC and confidence intervals ForearmAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Forearm, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Forearm, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Forearm, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Forearm, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Forearm, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Forearm, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Forearm, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Forearm, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Forearm, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Forearm, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Left Hand - same protocol # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") L_Hand <- read.delim("Left Hands.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Hand) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Hand_trimmed <- name.check(PGLS_Tree, L_Hand) L_Hand_Tree <- drop.tip(PGLS_Tree, L_Hand_trimmed$tree_not_data) any(is.na(L_Hand)) name.check(L_Hand_Tree, L_Hand) plot(L_Hand_Tree) # Combine phylogeny and data Species<-row.names(L_Hand) L_Hand2<-cbind(Species,L_Hand) LHand<-comparative.data(phy = L_Hand_Tree, data = L_Hand2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LHand$dropped$tips LHand$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = LHand, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(L_Hand2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = LHand, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Hand2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=L_Hand2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Hand, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LHand, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Hand2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Hand, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LHand, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Hand2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Hand, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LHand, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Hand2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Hand, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LHand, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=L_Hand2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Hand, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LHand, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=L_Hand2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Hand, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LHand, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=L_Hand2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Hand, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LHand, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Hand2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Hand, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LHand, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Hand2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Hand, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LHand, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Hand2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients L_Hand_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Hand_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Hand_pgls_coef, "L Hand PGLS Coefficients") write.csv(L_Hand_ols_coef, "L Hand OLS Coefficients") # AIC and confidence intervals HandAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Hand, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Hand, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Hand, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Hand, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Hand, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Hand, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Hand, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Hand, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Hand, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Hand, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Left Thigh - same protocol # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") L_Thigh <- read.delim("Left Thighs.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Thigh) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Thigh_trimmed <- name.check(PGLS_Tree, L_Thigh) L_Thigh_Tree <- drop.tip(PGLS_Tree, L_Thigh_trimmed$tree_not_data) any(is.na(L_Thigh)) name.check(L_Thigh_Tree, L_Thigh) plot(L_Thigh_Tree) # Combine phylogeny and data Species<-row.names(L_Thigh) L_Thigh2<-cbind(Species,L_Thigh) LThigh<-comparative.data(phy = L_Thigh_Tree, data = L_Thigh2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LThigh$dropped$tips LThigh$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = LThigh, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(L_Thigh2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = LThigh, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Thigh2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=L_Thigh2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Thigh, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LThigh, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Thigh2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Thigh, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LThigh, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Thigh2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Thigh, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LThigh, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Thigh2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Thigh, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LThigh, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=L_Thigh2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Thigh, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LThigh, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=L_Thigh2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Thigh, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LThigh, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=L_Thigh2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Thigh, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LThigh, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Thigh2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Thigh, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LThigh, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Thigh2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Thigh, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LThigh, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Thigh2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients L_Thigh_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Thigh_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Thigh_pgls_coef, "L Thigh PGLS Coefficients") write.csv(L_Thigh_ols_coef, "L Thigh OLS Coefficients") # AIC and confidence intervals ThighAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) confint(Mass.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Thigh, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Thigh, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Thigh, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Thigh, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Thigh, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Thigh, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Thigh, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Thigh, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Thigh, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Thigh, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Left Shank - same protocol # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") L_Shank <- read.delim("Left Shanks.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Shank) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Shank_trimmed <- name.check(PGLS_Tree, L_Shank) L_Shank_Tree <- drop.tip(PGLS_Tree, L_Shank_trimmed$tree_not_data) any(is.na(L_Shank)) name.check(L_Shank_Tree, L_Shank) plot(L_Shank_Tree) # Combine phylogeny and data Species<-row.names(L_Shank) L_Shank2<-cbind(Species,L_Shank) LShank<-comparative.data(phy = L_Shank_Tree, data = L_Shank2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LShank$dropped$tips LShank$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = LShank, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(L_Shank2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = LShank, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Shank2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=L_Shank2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Shank, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LShank, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Shank2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Shank, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LShank, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Shank2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Shank, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LShank, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Shank2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Shank, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LShank, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=L_Shank2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Shank, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LShank, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=L_Shank2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Shank, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LShank, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=L_Shank2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Shank, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LShank, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Shank2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Shank, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LShank, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Shank2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Shank, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LShank, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Shank2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients L_Shank_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Shank_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Shank_pgls_coef, "L Shank PGLS Coefficients") write.csv(L_Shank_ols_coef, "L Shank OLS Coefficients") # AIC and confidence intervals P_AIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Shank, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Shank, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Shank, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Shank, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Shank, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Shank, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Shank, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Shank, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Shank, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Shank, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Left Foot - same protocol # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") L_Foot <- read.delim("Left Feet.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, L_Foot) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset L_Foot_trimmed <- name.check(PGLS_Tree, L_Foot) L_Foot_Tree <- drop.tip(PGLS_Tree, L_Foot_trimmed$tree_not_data) any(is.na(L_Foot)) name.check(L_Foot_Tree, L_Foot) plot(L_Foot_Tree) # Combine phylogeny and data Species<-row.names(L_Foot) L_Foot2<-cbind(Species,L_Foot) LFoot<-comparative.data(phy = L_Foot_Tree, data = L_Foot2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct LFoot$dropped$tips LFoot$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = LFoot, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(L_Foot2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = LFoot, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(L_Foot2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=L_Foot2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(L_Foot, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = LFoot, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=L_Foot2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(L_Foot, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = LFoot, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=L_Foot2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(L_Foot, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = LFoot, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=L_Foot2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(L_Foot, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = LFoot, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=L_Foot2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(L_Foot, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = LFoot, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=L_Foot2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(L_Foot, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = LFoot, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=L_Foot2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(L_Foot, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = LFoot, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=L_Foot2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(L_Foot, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = LFoot, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=L_Foot2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(L_Foot, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(L_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = LFoot, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(L_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=L_Foot2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(L_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(L_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients L_Foot_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) L_Foot_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(L_Foot_pgls_coef, "L Foot PGLS Coefficients") write.csv(L_Foot_ols_coef, "L Foot OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=L_Foot, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=L_Foot, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=L_Foot, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=L_Foot, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=L_Foot, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=L_Foot, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=L_Foot, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=L_Foot, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=L_Foot, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=L_Foot, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Axial segments # Skulls # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Skull <- read.delim("Skulls.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Skull) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Skull_trimmed <- name.check(PGLS_Tree, Skull) Skull_Tree <- drop.tip(PGLS_Tree, Skull_trimmed$tree_not_data) any(is.na(Skull)) name.check(Skull_Tree, Skull) plot(Skull_Tree) # Combine phylogeny and data Species<-row.names(Skull) Skull2<-cbind(Species,Skull) Skull_comp<-comparative.data(phy = Skull_Tree, data = Skull2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Skull_comp$dropped$tips Skull_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Skull_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Skull2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Skull2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Skull2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Skull2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Skull, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Skull_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Skull2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Skull2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Skull2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Skull, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Skull_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Skull2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Skull2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Skull2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Skull, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Skull_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Skull2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Skull2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Skull2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Skull, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Skull2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Skull2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Skull, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Skull2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Skull2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Skull, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Skull2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Skull2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Skull, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Skull2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Skull2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Skull, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Skull2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Skull2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Skull, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Skull_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Skull2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Skull_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Skull2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Skull2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Skull2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Skull), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Skull_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Skull_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Skull_pgls_coef, "Skull PGLS Coefficients") write.csv(Skull_ols_coef, "Skull OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Skull, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Skull, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Skull, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Skull, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Skull, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Skull, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Skull, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Skull, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Skull, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Skull, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Necks # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Neck <- read.delim("Necks.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Neck) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Neck_trimmed <- name.check(PGLS_Tree, Neck) Neck_Tree <- drop.tip(PGLS_Tree, Neck_trimmed$tree_not_data) any(is.na(Neck)) name.check(Neck_Tree, Neck) plot(Neck_Tree) # Combine phylogeny and data Species<-row.names(Neck) Neck2<-cbind(Species,Neck) Neck_comp<-comparative.data(phy = Neck_Tree, data = Neck2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Neck_comp$dropped$tips Neck_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Neck_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Neck2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Neck2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Neck2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Neck2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Neck, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Neck_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Neck2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Neck2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Neck2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Neck, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Neck_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Neck2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Neck2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Neck2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Neck, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Neck_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Neck2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Neck2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Neck2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Neck, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Neck2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Neck2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Neck, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Neck2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Neck2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Neck, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Neck2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Neck2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Neck, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Neck2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Neck2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Neck, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Neck2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Neck2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Neck, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Neck_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Neck2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Neck_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Neck2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Neck2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Neck2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Neck), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Neck_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Neck_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Neck_pgls_coef, "Neck PGLS Coefficients") write.csv(Neck_ols_coef, "Neck OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Neck, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Neck, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Neck, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Neck, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Neck, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Neck, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Neck, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Neck, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Neck, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Neck, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Torsos # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Torso <- read.delim("Torsos.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Torso) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Torso_trimmed <- name.check(PGLS_Tree, Torso) Torso_Tree <- drop.tip(PGLS_Tree, Torso_trimmed$tree_not_data) any(is.na(Torso)) name.check(Torso_Tree, Torso) plot(Torso_Tree) # Combine phylogeny and data Species<-row.names(Torso) Torso2<-cbind(Species,Torso) Torso_comp<-comparative.data(phy = Torso_Tree, data = Torso2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Torso_comp$dropped$tips Torso_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Torso_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Torso2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Torso2, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Torso2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Torso2, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Torso, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Torso_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Torso2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Torso2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Torso2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Torso, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Torso_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Torso2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Torso2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Torso2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Torso, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Torso_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Torso2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Torso2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Torso2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Torso, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Torso2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Torso2, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Torso, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Torso2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Torso2, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Torso, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Torso2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Torso2, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Torso, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Torso2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Torso2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Torso, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Torso2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Torso2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Torso, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Torso_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Torso2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Torso_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Torso2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Torso2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Torso2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Torso), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Torso_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Torso_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Torso_pgls_coef, "Torso PGLS Coefficients") write.csv(Torso_ols_coef, "Torso OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Torso, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Torso, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Torso, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Torso, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Torso, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Torso, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Torso, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Torso, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Torso, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Torso, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Tail1 # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Tail1 <- read.delim("Tail 1.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Tail1) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Tail1_trimmed <- name.check(PGLS_Tree, Tail1) Tail1_Tree <- drop.tip(PGLS_Tree, Tail1_trimmed$tree_not_data) any(is.na(Tail1)) name.check(Tail1_Tree, Tail1) plot(Tail1_Tree) # Combine phylogeny and data Species<-row.names(Tail1) Tail12<-cbind(Species,Tail1) Tail1_comp<-comparative.data(phy = Tail1_Tree, data = Tail12, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Tail1_comp$dropped$tips Tail1_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Tail1_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Tail12, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Tail12, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Tail12) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Tail12, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Tail1, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Tail12, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Tail12) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Tail12, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Tail1, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Tail12, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Tail12) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Tail12, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Tail1, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Tail12, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Tail12) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Tail12, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Tail1, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Tail12) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Tail12, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Tail1, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Tail12) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Tail12, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Tail1, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Tail12) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Tail12, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Tail1, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Tail12) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Tail12, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Tail1, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Tail12) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Tail12, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Tail1, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail1_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail12, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail1_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Tail12, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Tail12) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Tail12, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail1), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Tail1_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Tail1_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Tail1_pgls_coef, "Tail1 PGLS Coefficients") write.csv(Tail1_ols_coef, "Tail1 OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Tail1, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Tail1, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Tail1, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Tail1, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Tail1, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Tail1, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Tail1, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Tail1, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Tail1, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Tail1, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Tail2 # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Tail2 <- read.delim("Tail 2.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Tail2) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Tail2_trimmed <- name.check(PGLS_Tree, Tail2) Tail2_Tree <- drop.tip(PGLS_Tree, Tail2_trimmed$tree_not_data) any(is.na(Tail2)) name.check(Tail2_Tree, Tail2) plot(Tail2_Tree) # Combine phylogeny and data Species<-row.names(Tail2) Tail22<-cbind(Species,Tail2) Tail2_comp<-comparative.data(phy = Tail2_Tree, data = Tail22, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Tail2_comp$dropped$tips Tail2_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Tail2_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Tail22, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Tail22, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Tail22) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Tail22, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Tail2, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Tail22, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Tail22) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Tail22, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Tail2, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Tail22, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Tail22) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Tail22, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Tail2, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Tail22, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Tail22) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Tail22, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Tail2, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Tail22) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Tail22, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Tail2, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Tail22) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Tail22, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Tail2, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Tail22) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Tail22, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Tail2, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Tail22) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Tail22, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Tail2, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Tail22) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Tail22, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Tail2, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail2_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail22, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail2_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Tail22, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Tail22) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Tail22, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail2), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Tail2_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Tail2_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Tail2_pgls_coef, "Tail2 PGLS Coefficients") write.csv(Tail2_ols_coef, "Tail2 OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Tail2, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Tail2, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Tail2, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Tail2, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Tail2, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Tail2, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Tail2, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Tail2, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Tail2, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Tail2, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Tail3 # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Tail3 <- read.delim("Tail 3.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Tail3) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Tail3_trimmed <- name.check(PGLS_Tree, Tail3) Tail3_Tree <- drop.tip(PGLS_Tree, Tail3_trimmed$tree_not_data) any(is.na(Tail3)) name.check(Tail3_Tree, Tail3) plot(Tail3_Tree) # Combine phylogeny and data Species<-row.names(Tail3) Tail32<-cbind(Species,Tail3) Tail3_comp<-comparative.data(phy = Tail3_Tree, data = Tail32, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Tail3_comp$dropped$tips Tail3_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Tail3_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Tail32, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Tail32, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Tail32) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Tail32, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Tail3, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Tail32, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Tail32) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Tail32, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Tail3, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Tail32, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Tail32) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Tail32, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Tail3, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Tail32, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Tail32) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Tail32, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Tail3, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Tail32) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Tail32, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Tail3, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Tail32) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Tail32, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Tail3, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Tail32) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Tail32, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Tail3, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Tail32) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Tail32, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Tail3, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Tail32) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Tail32, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Tail3, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail3_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail32, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail3_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Tail32, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Tail32) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Tail32, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail3), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Tail3_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Tail3_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Tail3_pgls_coef, "Tail3 PGLS Coefficients") write.csv(Tail3_ols_coef, "Tail3 OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Tail3, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Tail3, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Tail3, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Tail3, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Tail3, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Tail3, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Tail3, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Tail3, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Tail3, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Tail3, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv # Tail4 # Import data setwd("D:/Users/Sam Coatham/Documents/Convex Hulls/Code/PGLS/New segments") Tail4 <- read.delim("Tail 4.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, Tail4) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset Tail4_trimmed <- name.check(PGLS_Tree, Tail4) Tail4_Tree <- drop.tip(PGLS_Tree, Tail4_trimmed$tree_not_data) any(is.na(Tail4)) name.check(Tail4_Tree, Tail4) plot(Tail4_Tree) # Combine phylogeny and data Species<-row.names(Tail4) Tail42<-cbind(Species,Tail4) Tail4_comp<-comparative.data(phy = Tail4_Tree, data = Tail42, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct Tail4_comp$dropped$tips Tail4_comp$dropped$unmatched.rows # Mass model.pgls <- pgls(log(Skin_Mass) ~ log(Hull_Mass), data = Tail4_comp, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(Tail42, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(Skin_Mass ~ Hull_Mass, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(Tail42, aes(x = log(Hull_Mass), y = log(Skin_Mass))) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(Skin_Mass ~ Hull_Mass, data=Tail42) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(Tail42, aes(x = Hull_Mass, y = Skin_Mass)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(Tail4, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(Tail42, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=Tail42) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(Tail42, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(Tail4, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(Tail42, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=Tail42) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(Tail42, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(Tail4, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(Tail42, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=Tail42) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(Tail42, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(Tail4, aes(x = Skin_Ixx, y = Hull_Ixx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(Skin_Ixx ~ Hull_Ixx, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(Skin_Ixx ~ Hull_Ixx, data=Tail42) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(Tail42, aes(x = Hull_Ixx, y = Skin_Ixx)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(Tail4, aes(x = Skin_Iyy, y = Hull_Iyy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(Skin_Iyy ~ Hull_Iyy, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(Skin_Iyy ~ Hull_Iyy, data=Tail42) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(Tail42, aes(x = Hull_Iyy, y = Skin_Iyy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(Tail4, aes(x = Skin_Izz, y = Hull_Izz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(Skin_Izz ~ Hull_Izz, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(Skin_Izz ~ Hull_Izz, data=Tail42) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(Tail42, aes(x = Hull_Izz, y = Skin_Izz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(Tail4, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=Tail42) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(Tail42, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(Tail4, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=Tail42) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(Tail42, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(Tail4, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail4_comp, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(Tail42, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = Tail4_comp, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(Tail42, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=Tail42) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(Tail42, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(Tail4), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients Tail4_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) Tail4_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(Tail4_pgls_coef, "Tail4 PGLS Coefficients") write.csv(Tail4_ols_coef, "Tail4 OLS Coefficients") # AIC and confidence intervals P_Aic <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=Tail4, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=Tail4, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=Tail4, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=Tail4, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=Tail4, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=Tail4, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=Tail4, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=Tail4, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=Tail4, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=Tail4, method = 'lm', trControl = ctr) Mass.cv CMx.cv CMy.cv CMz.cv Ixx.cv Iyy.cv Izz.cv Ixy.cv Ixz.cv Iyz.cv R_Arm <- read.delim("Right arms.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Arm) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Arm_trimmed <- name.check(PGLS_Tree, R_Arm) R_Arm_Tree <- drop.tip(PGLS_Tree, R_Arm_trimmed$tree_not_data) any(is.na(R_Arm)) name.check(R_Arm_Tree, R_Arm) plot(R_Arm_Tree) library(caper) # Combine phylogeny and data Species<-row.names(R_Arm) R_Arm2<-cbind(Species,R_Arm) RArm<-comparative.data(phy = R_Arm_Tree, data = R_Arm2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RArm$dropped$tips RArm$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RArm, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) library(ggplot2) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RArm, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Arm2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Arm, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RArm, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Arm2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Arm, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RArm, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Arm2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Arm, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RArm, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Arm2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Arm, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RArm, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Arm2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Arm, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RArm, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Arm2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Arm, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RArm, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Arm2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Arm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Arm, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RArm, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Arm2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Arm, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RArm, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Arm2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Arm, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RArm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RArm, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Arm2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Arm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Arm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() library(huxtable) library(nlme) # Summarise coefficients R_Arm_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Arm_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Arm_pgls_coef, "R Arm PGLS Coefficients") write.csv(R_Arm_ols_coef, "R Arm OLS Coefficients") # AIC and confidence intervals ArmAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) library(caret) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Arm, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Arm, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Arm, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Arm, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Arm, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Arm, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Arm, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Arm, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Arm, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Arm, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all) # Right Forearm - same protocol # Import data R_Forearm <- read.delim("Right Forearms.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Forearm) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Forearm_trimmed <- name.check(PGLS_Tree, R_Forearm) R_Forearm_Tree <- drop.tip(PGLS_Tree, R_Forearm_trimmed$tree_not_data) any(is.na(R_Forearm)) name.check(R_Forearm_Tree, R_Forearm) plot(R_Forearm_Tree) # Combine phylogeny and data Species<-row.names(R_Forearm) R_Forearm2<-cbind(Species,R_Forearm) RForearm<-comparative.data(phy = R_Forearm_Tree, data = R_Forearm2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RForearm$dropped$tips RForearm$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RForearm, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RForearm, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Forearm2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Forearm, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RForearm, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Forearm2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Forearm, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RForearm, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Forearm2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Forearm, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RForearm, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Forearm2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Forearm, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RForearm, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Forearm2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Forearm, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RForearm, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Forearm2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Forearm, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RForearm, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Forearm2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Forearm2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Forearm, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RForearm, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Forearm2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Forearm, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RForearm, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Forearm2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Forearm, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RForearm, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RForearm, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Forearm2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Forearm2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Forearm), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients R_Forearm_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Forearm_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Forearm_pgls_coef, "R Forearm PGLS Coefficients") write.csv(R_Forearm_ols_coef, "R Forearm OLS Coefficients") # AIC and confidence intervals ForearmAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Forearm, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Forearm, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Forearm, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Forearm, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Forearm, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Forearm, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Forearm, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Forearm, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Forearm, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Forearm, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all) # Right Hand - same protocol # Import data R_Hand <- read.delim("Right hands.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Hand) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Hand_trimmed <- name.check(PGLS_Tree, R_Hand) R_Hand_Tree <- drop.tip(PGLS_Tree, R_Hand_trimmed$tree_not_data) any(is.na(R_Hand)) name.check(R_Hand_Tree, R_Hand) plot(R_Hand_Tree) # Combine phylogeny and data Species<-row.names(R_Hand) R_Hand2<-cbind(Species,R_Hand) RHand<-comparative.data(phy = R_Hand_Tree, data = R_Hand2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RHand$dropped$tips RHand$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RHand, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RHand, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Hand2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Hand, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RHand, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Hand2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Hand, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RHand, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Hand2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Hand, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RHand, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Hand2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Hand, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RHand, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Hand2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Hand, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RHand, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Hand2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Hand, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RHand, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Hand2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Hand2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Hand, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RHand, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Hand2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Hand, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RHand, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Hand2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Hand, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RHand, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RHand, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Hand2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Hand2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Hand), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients R_Hand_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Hand_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Hand_pgls_coef, "R Hand PGLS Coefficients") write.csv(R_Hand_ols_coef, "R Hand OLS Coefficients") # AIC and confidence intervals HandAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Hand, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Hand, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Hand, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Hand, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Hand, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Hand, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Hand, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Hand, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Hand, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Hand, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all) # Right Thigh - same protocol # Import data R_Thigh <- read.delim("Right thighs.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Thigh) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Thigh_trimmed <- name.check(PGLS_Tree, R_Thigh) R_Thigh_Tree <- drop.tip(PGLS_Tree, R_Thigh_trimmed$tree_not_data) any(is.na(R_Thigh)) name.check(R_Thigh_Tree, R_Thigh) plot(R_Thigh_Tree) # Combine phylogeny and data Species<-row.names(R_Thigh) R_Thigh2<-cbind(Species,R_Thigh) RThigh<-comparative.data(phy = R_Thigh_Tree, data = R_Thigh2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RThigh$dropped$tips RThigh$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RThigh, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RThigh, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Thigh2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Thigh, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RThigh, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Thigh2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Thigh, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RThigh, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Thigh2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Thigh, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RThigh, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Thigh2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Thigh, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RThigh, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Thigh2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Thigh, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RThigh, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Thigh2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Thigh, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RThigh, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Thigh2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Thigh2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Thigh, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RThigh, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Thigh2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Thigh, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RThigh, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Thigh2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Thigh, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RThigh, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RThigh, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Thigh2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Thigh2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Thigh), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients R_Thigh_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Thigh_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Thigh_pgls_coef, "R Thigh PGLS Coefficients") write.csv(R_Thigh_ols_coef, "R Thigh OLS Coefficients") # AIC and confidence intervals ThighAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Thigh, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Thigh, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Thigh, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Thigh, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Thigh, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Thigh, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Thigh, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Thigh, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Thigh, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Thigh, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all) # Right Shank - same protocol # Import data R_Shank <- read.delim("Right shanks.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Shank) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Shank_trimmed <- name.check(PGLS_Tree, R_Shank) R_Shank_Tree <- drop.tip(PGLS_Tree, R_Shank_trimmed$tree_not_data) any(is.na(R_Shank)) name.check(R_Shank_Tree, R_Shank) plot(R_Shank_Tree) # Combine phylogeny and data Species<-row.names(R_Shank) R_Shank2<-cbind(Species,R_Shank) RShank<-comparative.data(phy = R_Shank_Tree, data = R_Shank2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RShank$dropped$tips RShank$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RShank, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RShank, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Shank2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Shank, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RShank, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Shank2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Shank, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RShank, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Shank2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Shank, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RShank, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Shank2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Shank, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RShank, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Shank2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Shank, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RShank, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Shank2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Shank, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RShank, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Shank2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Shank2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Shank, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RShank, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Shank2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Shank, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RShank, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Shank2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Shank, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RShank, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RShank, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Shank2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Shank2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Shank), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients R_Shank_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Shank_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Shank_pgls_coef, "R Shank PGLS Coefficients") write.csv(R_Shank_ols_coef, "R Shank OLS Coefficients") # AIC and confidence intervals ShankAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Shank, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Shank, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Shank, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Shank, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Shank, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Shank, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Shank, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Shank, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Shank, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Shank, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all) # Right Foot - same protocol # Import data R_Foot <- read.delim("Right Feet.txt", header = TRUE, row.names = 1) name.check(PGLS_Tree, R_Foot) # All missing species have been added to the phylogeny # Remove species from phylogeny that are not represented in the dataset R_Foot_trimmed <- name.check(PGLS_Tree, R_Foot) R_Foot_Tree <- drop.tip(PGLS_Tree, R_Foot_trimmed$tree_not_data) any(is.na(R_Foot)) name.check(R_Foot_Tree, R_Foot) plot(R_Foot_Tree) # Combine phylogeny and data Species<-row.names(R_Foot) R_Foot2<-cbind(Species,R_Foot) RFoot<-comparative.data(phy = R_Foot_Tree, data = R_Foot2, names.col = Species, vcv = TRUE, na.omit = FALSE, warn.dropped = TRUE) #check that everything is correct RFoot$dropped$tips RFoot$dropped$unmatched.rows # Mass model.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RFoot, lambda = 'ML') # Look at the model outputs anova(model.pgls) # Look at the model coefficients summary(model.pgls) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() #Model diagnostics for PGLS par(mfrow = c(2, 2)) #plot the model diagnostics plot(model.pgls) par(mfrow = c(1, 1)) #Likelihood profiles for $\lambda$ in PGLS models lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Mass.pgls <- pgls(log10(Skin_Mass) ~ log10(Hull_Mass), data = RFoot, lambda = 1) # Look at the model outputs anova(Mass.pgls) # Look at the model coefficients summary(Mass.pgls) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.pgls)[2], intercept = coefficients(Mass.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Mass.ols = lm(log10(Skin_Mass) ~ log10(Hull_Mass), data=R_Foot2) summary(Mass.ols) # Look at the model outputs anova(Mass.ols) # Look at the model coefficients summary(Mass.ols) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Mass), y = log10(Skin_Mass))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Mass.ols)[2], intercept = coefficients(Mass.ols)[1]) + theme_bw() # CM(x) #scatter plot of log Skin CMx vs log Hull CMx, colored by the species. ggplot(R_Foot, aes(x = Skin_CMx, y = Hull_CMx)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMx on Hull CMx model.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMx.pgls <- pgls(Skin_CMx ~ Hull_CMx, data = RFoot, lambda = 1) # Look at the model outputs anova(CMx.pgls) summary(CMx.pgls) AIC(CMx.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.pgls)[2], intercept = coefficients(CMx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMx.ols = lm(Skin_CMx ~ Hull_CMx, data=R_Foot2) summary(CMx.ols) # Look at the model outputs anova(CMx.ols) summary(CMx.ols) confint(CMx.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_CMx, y = Skin_CMx)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMx.ols)[2], intercept = coefficients(CMx.ols)[1]) + theme_bw() # CM(y) #scatter plot of log Skin CMy vs log Hull CMy, colored by the species. ggplot(R_Foot, aes(x = Skin_CMy, y = Hull_CMy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMy on Hull CMy model.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMy.pgls <- pgls(Skin_CMy ~ Hull_CMy, data = RFoot, lambda = 1) # Look at the model outputs anova(CMy.pgls) summary(CMy.pgls) AIC(CMy.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.pgls)[2], intercept = coefficients(CMy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMy.ols = lm(Skin_CMy ~ Hull_CMy, data=R_Foot2) summary(CMy.ols) # Look at the model outputs anova(CMy.ols) summary(CMy.ols) confint(CMy.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_CMy, y = Skin_CMy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMy.ols)[2], intercept = coefficients(CMy.ols)[1]) + theme_bw() # CM(z) #scatter plot of log Skin CMz vs log Hull CMz, colored by the species. ggplot(R_Foot, aes(x = Skin_CMz, y = Hull_CMz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin CMz on Hull CMz model.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) CMz.pgls <- pgls(Skin_CMz ~ Hull_CMz, data = RFoot, lambda = 1) # Look at the model outputs anova(CMz.pgls) summary(CMz.pgls) AIC(CMz.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.pgls)[2], intercept = coefficients(CMz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) CMz.ols = lm(Skin_CMz ~ Hull_CMz, data=R_Foot2) summary(CMz.ols) # Look at the model outputs anova(CMz.ols) summary(CMz.ols) confint(CMz.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_CMz, y = Skin_CMz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(CMz.ols)[2], intercept = coefficients(CMz.ols)[1]) + theme_bw() # Ixx #scatter plot of log Skin Ixx vs log Hull Ixx, colored by the species. ggplot(R_Foot, aes(x = log10(Skin_Ixx), y = log10(Hull_Ixx))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixx on Hull Ixx model.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixx.pgls <- pgls(log10(Skin_Ixx) ~ log10(Hull_Ixx), data = RFoot, lambda = 1) # Look at the model outputs anova(Ixx.pgls) summary(Ixx.pgls) AIC(Ixx.pgls) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.pgls)[2], intercept = coefficients(Ixx.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixx.ols = lm(log10(Skin_Ixx) ~ log10(Hull_Ixx), data=R_Foot2) summary(Ixx.ols) # Look at the model outputs anova(Ixx.ols) summary(Ixx.ols) confint(Ixx.ols) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Ixx), y = log10(Skin_Ixx))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixx.ols)[2], intercept = coefficients(Ixx.ols)[1]) + theme_bw() # Iyy #scatter plot of log Skin Iyy vs log Hull Iyy, colored by the species. ggplot(R_Foot, aes(x = log10(Skin_Iyy), y = log10(Hull_Iyy))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyy on Hull Iyy model.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyy.pgls <- pgls(log10(Skin_Iyy) ~ log10(Hull_Iyy), data = RFoot, lambda = 1) # Look at the model outputs anova(Iyy.pgls) summary(Iyy.pgls) AIC(Iyy.pgls) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.pgls)[2], intercept = coefficients(Iyy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyy.ols = lm(log10(Skin_Iyy) ~ log10(Hull_Iyy), data=R_Foot2) summary(Iyy.ols) # Look at the model outputs anova(Iyy.ols) summary(Iyy.ols) confint(Iyy.ols) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Iyy), y = log10(Skin_Iyy))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyy.ols)[2], intercept = coefficients(Iyy.ols)[1]) + theme_bw() # Izz #scatter plot of log Skin Izz vs log Hull Izz, colored by the species. ggplot(R_Foot, aes(x = log10(Skin_Izz), y = log10(Hull_Izz))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Izz on Hull Izz model.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Izz.pgls <- pgls(log10(Skin_Izz) ~ log10(Hull_Izz), data = RFoot, lambda = 1) # Look at the model outputs anova(Izz.pgls) summary(Izz.pgls) AIC(Izz.pgls) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.pgls)[2], intercept = coefficients(Izz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Izz.ols = lm(log10(Skin_Izz) ~ log10(Hull_Izz), data=R_Foot2) summary(Izz.ols) # Look at the model outputs anova(Izz.ols) summary(Izz.ols) confint(Izz.ols) # plot the models ggplot(R_Foot2, aes(x = log10(Hull_Izz), y = log10(Skin_Izz))) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Izz.ols)[2], intercept = coefficients(Izz.ols)[1]) + theme_bw() # Ixy #scatter plot of log Skin Ixy vs log Hull Ixy, colored by the species. ggplot(R_Foot, aes(x = Skin_Ixy, y = Hull_Ixy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixy on Hull Ixy model.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixy.pgls <- pgls(Skin_Ixy ~ Hull_Ixy, data = RFoot, lambda = 1) # Look at the model outputs anova(Ixy.pgls) summary(Ixy.pgls) AIC(Ixy.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.pgls)[2], intercept = coefficients(Ixy.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixy.ols = lm(Skin_Ixy ~ Hull_Ixy, data=R_Foot2) summary(Ixy.ols) # Look at the model outputs anova(Ixy.ols) summary(Ixy.ols) confint(Ixy.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_Ixy, y = Skin_Ixy)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixy.ols)[2], intercept = coefficients(Ixy.ols)[1]) + theme_bw() # Ixz #scatter plot of log Skin Ixz vs log Hull Ixz, colored by the species. ggplot(R_Foot, aes(x = Skin_Ixz, y = Hull_Ixz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Ixz on Hull Ixz model.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Ixz.pgls <- pgls(Skin_Ixz ~ Hull_Ixz, data = RFoot, lambda = 1) # Look at the model outputs anova(Ixz.pgls) summary(Ixz.pgls) AIC(Ixz.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.pgls)[2], intercept = coefficients(Ixz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Ixz.ols = lm(Skin_Ixz ~ Hull_Ixz, data=R_Foot2) summary(Ixz.ols) # Look at the model outputs anova(Ixz.ols) summary(Ixz.ols) confint(Ixz.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_Ixz, y = Skin_Ixz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Ixz.ols)[2], intercept = coefficients(Ixz.ols)[1]) + theme_bw() # Iyz #scatter plot of log Skin Iyz vs log Hull Iyz, colored by the species. ggplot(R_Foot, aes(x = Skin_Iyz, y = Hull_Iyz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) # Fit a PGLS model of Skin Iyz on Hull Iyz model.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RFoot, lambda = 'ML') anova(model.pgls) summary(model.pgls) ggplot(R_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(model.pgls)[2], intercept = coefficients(model.pgls)[1]) + theme_bw() # Model diagnostics par(mfrow = c(2, 2)) plot(model.pgls) par(mfrow = c(1, 1)) # Create a likelihood profile of the lambda estimate lambda.profile <- pgls.profile(model.pgls, "lambda") plot(lambda.profile) # 1) PGLS model with lambda = 1 (i.e., equivalent to independent contrasts) Iyz.pgls <- pgls(Skin_Iyz ~ Hull_Iyz, data = RFoot, lambda = 1) # Look at the model outputs anova(Iyz.pgls) summary(Iyz.pgls) AIC(Iyz.pgls) # plot the models ggplot(R_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.pgls)[2], intercept = coefficients(Iyz.pgls)[1]) + theme_bw() # 2) OLS (i.e. the same as a PGLS with lambda=0) Iyz.ols = lm(Skin_Iyz ~ Hull_Iyz, data=R_Foot2) summary(Iyz.ols) # Look at the model outputs anova(Iyz.ols) summary(Iyz.ols) confint(Iyz.ols) # plot the models ggplot(R_Foot2, aes(x = Hull_Iyz, y = Skin_Iyz)) + geom_point(aes(color=row.names(R_Foot), size=10, alpha=0.7)) + geom_abline(slope = coefficients(Iyz.ols)[2], intercept = coefficients(Iyz.ols)[1]) + theme_bw() # Summarise coefficients R_Foot_ols_coef <- huxreg(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) R_Foot_pgls_coef <- allCoef(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) write.csv(R_Foot_pgls_coef, "R Foot PGLS Coefficients") write.csv(R_Foot_ols_coef, "R Foot OLS Coefficients") # AIC and confidence intervals FootAIC <- AIC(Mass.pgls, CMx.pgls, CMy.pgls, CMz.pgls, Ixx.pgls, Iyy.pgls, Izz.pgls, Ixy.pgls, Ixz.pgls, Iyz.pgls) confint(Mass.ols, CMx.ols, CMy.ols, CMz.ols, Ixx.ols, Iyy.ols, Izz.ols, Ixy.ols, Ixz.ols, Iyz.ols) conf0 <- confint(Mass.ols) conf1 <- confint(CMx.ols) conf2 <- confint(CMy.ols) conf3 <- confint(CMz.ols) conf4 <- confint(Ixx.ols) conf5 <- confint(Iyy.ols) conf6 <- confint(Izz.ols) conf7 <- confint(Ixy.ols) conf8 <- confint(Ixz.ols) conf9 <- confint(Iyz.ols) Confint <- data.frame(conf0, conf1, conf2, conf3, conf4, conf5, conf6, conf7, conf8, conf9) write.csv(Confint, "Confidence Intervals") #LOOCV ctr <- trainControl(method="LOOCV") # Leave-One-Out Cross-Validation metric <- "RSME" Mass.cv <- train(Skin_Mass ~ Hull_Mass, data=R_Foot, method = 'lm', trcontrol = ctr) CMx.cv <- train(Skin_CMx ~ Hull_CMx, data=R_Foot, method = 'lm', trControl = ctr) CMy.cv <- train(Skin_CMy ~ Hull_CMy, data=R_Foot, method = 'lm', trControl = ctr) CMz.cv <- train(Skin_CMz ~ Hull_CMz, data=R_Foot, method = 'lm', trControl = ctr) Ixx.cv <- train(Skin_Ixx ~ Hull_Ixx, data=R_Foot, method = 'lm', trControl = ctr) Iyy.cv <- train(Skin_Iyy ~ Hull_Iyy, data=R_Foot, method = 'lm', trControl = ctr) Izz.cv <- train(Skin_Izz ~ Hull_Izz, data=R_Foot, method = 'lm', trControl = ctr) Ixy.cv <- train(Skin_Ixy ~ Hull_Ixy, data=R_Foot, method = 'lm', trControl = ctr) Ixz.cv <- train(Skin_Ixz ~ Hull_Ixz, data=R_Foot, method = 'lm', trControl = ctr) Iyz.cv <- train(Skin_Iyz ~ Hull_Iyz, data=R_Foot, method = 'lm', trControl = ctr) A <- Mass.cv$results$RMSE B <- CMx.cv$results$RMSE C <- CMy.cv $results$RMSE D <- CMz.cv$results$RMSE E <- Ixx.cv$results$RMSE F <- Iyy.cv$results$RMSE G <- Izz.cv$results$RMSE H <- Ixy.cv $results$RMSE I <- Ixz.cv $results$RMSE J <- Iyz.cv$results$RMSE RMSE_all <- data.frame(A,B,C,D,E,F,G,H,I,J) View(RMSE_all)