---
# System prepended metadata

title: Lab Exersize 2

---

# Lab Exersize 2

Tutorial 1 
```
library(borealis)
data("pupfish", package="geomorph")
str(pupfish)
edge.landmarks <- 11:56
pup.links <- matrix(c(edge.landmarks[-length(edge.landmarks)], edge.landmarks[-1]), 
                    ncol = 2, byrow = FALSE)
pup.links <- rbind(pup.links[-c(23,28,38),],
                   matrix(c(4,11, 1,3, 3,9, 9,38, 33,7, 34,8, 4,48, 1,46, 49,56), 
                          ncol = 2, byrow = TRUE) )

landmark.plot(pupfish, links = pup.links)
pup.gpa <- align.procrustes(pupfish$coords)
names(pup.gpa)
pup.gdf <- listed.gdf(pup.gpa)
names(pup.gdf)
names(pup.gdf$gdf)
pup.gdf$gdf$Csize <- pupfish$CS
pup.gdf$gdf$sex <- pupfish$Sex
pup.gdf$gdf$pop <- pupfish$Pop
pup.gdf$gdf$pop.sex <- as.factor(paste(pup.gdf$gdf$pop, pup.gdf$gdf$sex, sep = "_"))
pup.pca <- gm.prcomp(pup.gdf$gdf$coords)

plot(pup.pca, col = as.factor(pup.gdf$gdf$pop.sex))
legend("topright", levels(pup.gdf$gdf$pop.sex), 
       col = c(1:length(levels(pup.gdf$gdf$pop.sex))),
       pch = 1, box.lwd = 0)
i <- 1e4-1 # number of iterations
size.model <- procD.lm(coords ~ log(Csize), data=pup.gdf$gdf, iter=i)
anova(size.model)
sex.model <- procD.lm(coords ~ log(Csize) + sex, data=pup.gdf$gdf, iter=i) 
anova(sex.model)
anova(size.model, sex.model)
pop.model <- procD.lm(coords ~ log(Csize) + pop, data=pup.gdf$gdf, iter=i) 
sex.pop.model <- procD.lm(coords ~ log(Csize) + sex + pop, data=pup.gdf$gdf, iter=i) 
anova(size.model, pop.model, sex.pop.model)
sex.by.pop.model <- procD.lm(coords ~ log(Csize) + sex * pop, data=pup.gdf$gdf, iter=i) 
anova(sex.pop.model, sex.by.pop.model)
sex.by.pop.model <- procD.lm(coords ~ log(Csize) + sex + pop + sex:pop, data=pup.gdf$gdf, iter=i) 
pop.unique.model <- procD.lm(coords ~ log(Csize) * pop, data=pup.gdf$gdf, iter=i)
anova(pop.model, pop.unique.model)
plotAllometry(fit = pop.unique.model, size = pup.gdf$gdf$Csize, 
              col = pup.gdf$gdf$pop, xlab = "log centroid size")
```

Problem 1
```
pop.unique.model2 <- procD.lm(coords ~ log(Csize) * pop + sex, data=pup.gdf$gdf, iter=i)
anova(sex.pop.model, pop.unique.model2)
plotAllometry(fit = pop.unique.model2, size = pup.gdf$gdf$Csize, 
              col = pup.gdf$gdf$pop.sex, xlab = "log centroid size")
```
Anova Results 
```
Analysis of Variance, using Residual Randomization
Permutation procedure: Randomization of null model residuals 
Number of permutations: 10000 
Estimation method: Ordinary Least Squares 
Effect sizes (Z) based on F distributions

                                       ResDf Df      RSS        SS        MS      Rsq      F
coords ~ log(Csize) + sex + pop (Null)    50  1 0.027002                     0.000000       
coords ~ log(Csize) * pop + sex           49  1 0.025351 0.0016509 0.0016509 0.029343 3.1909
Total                                     53    0.056262                                    
                                            Z      P Pr(>F)
coords ~ log(Csize) + sex + pop (Null)                     
coords ~ log(Csize) * pop + sex        2.5575 0.0067       
Total  
```
![](https://i.imgur.com/9y2jJwY.png)

Sex and population are an important factors influencing pupfish shape (permutation-based Procrustes ANOVA, F= 3.1909, p = .0067). The interaction between population and size was significant in determining that these groups have unique allometries. 

Tutorial 2 
```
create.tps(
  input.filename = "Ofas.RNAi.nota.digitization.csv",
  output.filename = "Ofas.RNAi.nota.tps",
  id.factors = c('digitizer','treatment'),
  include.scale = TRUE,
  invert.scale = TRUE)
bug.nota <- read.tps("Ofas.RNAi.nota.tps", keep.original.ids = TRUE)
nota.lines <- matrix(c(1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,1, 2,6),
                     ncol = 2, byrow = TRUE)
bug.nota <- align.reflect(bug.nota, links = nota.lines)
bug.gpa <- align.procrustes(bug.nota)
x <- bug.gpa$Csize > 500
bug.gpa$Csize[x] <- NA

x <- bug.gpa$metadata$treatment == "vr"
bug.gpa$metadata$treatment[x] <- "vg"

x <- bug.gpa$metadata$treatment == "wildtype"
bug.gpa$metadata$treatment[x] <- "wt"
bug.gdf <- listed.gdf(bug.gpa)
bug.pca <- gm.prcomp(bug.gdf$gdf$coords)
ggGMMplot(bug.pca, 
          group = bug.gdf$gdf$treatment, 
          group.title = 'treatment', 
          convex.hulls = TRUE, 
          include.legend = TRUE )
```
Problem 2 
```
i <- 1e4-1 
bug.gdf <- listed.gdf(bug.gpa)
bug.model1 <- procD.lm(coords ~ log(Csize), data = bug.gdf$gdf, iter = i)
bug.model2 <- procD.lm(coords ~ log(Csize) + digitizer, data = bug.gdf$gdf, iter = i)
anova(bug.model1, bug.model2)

```
First, I wanted to look at whether or not the digitizer affected the size of the milkweed bug. 
```
Analysis of Variance, using Residual Randomization
Permutation procedure: Randomization of null model residuals 
Number of permutations: 10000 
Estimation method: Ordinary Least Squares 
Effect sizes (Z) based on F distributions

                                ResDf Df    RSS     SS      MS     Rsq      F      Z     P Pr(>F)
coords ~ log(Csize) (Null)         88  1 2.2163                0.00000                           
coords ~ log(Csize) + digitizer    80  8 0.9453 1.2711 0.15888 0.57208 13.446 5.9842 1e-04       
Total                              89    2.2218   
```
The sample digitizer had a significant effect on the size of the sample, meaning there was non insignificant human error (permutation-based Procrustes ANOVA, F= 13.446, p < .001)

Then I wanted to see if there was any allometry changes between treatment. This was done by modeling treatment and size (and controlling for digitizer), and then modeling an interaction between shape and treatment, to see if the rate of the change in size was affected by treatment. 

```
bug.model3 <- procD.lm(coords ~ log(Csize) + treatment + digitizer, data = bug.gdf$gdf, iter = i)
bug.model4 <- procD.lm(coords ~ log(Csize) * treatment + digitizer, data = bug.gdf$gdf, iter = i)
anova(bug.model3, bug.model4)
```
```
 Analysis of Variance, using Residual Randomization
Permutation procedure: Randomization of null model residuals 
Number of permutations: 10000 
Estimation method: Ordinary Least Squares 
Effect sizes (Z) based on F distributions

                                                   ResDf Df     RSS       SS       MS       Rsq      F       Z      P Pr(>F)
coords ~ log(Csize) + treatment + digitizer (Null)    71  1 0.13921                   0.0000000                             
coords ~ log(Csize) * treatment + digitizer           62  9 0.12141 0.017802 0.001978 0.0080123 1.0101 0.12622 0.4545       
Total  
```
There is no evidence of allometry between bug size and treatments (permutation-based Procrustes ANOVA, F= 1.0101, p = .4545)





