Tuesday, November 11, 2014

11 11 2014 NEW Size Distribution Graphs with STATS!

y1sizedist.R
require(ggplot2)
## Loading required package: ggplot2
require(plyr)
## Loading required package: plyr
require(splitstackshape)
## Loading required package: splitstackshape
## Loading required package: data.table
y1size=read.csv('Y1size.csv')
#creates dataframe and reads in the CSV file for sizes
View(y1size)
#check data
y1size$Date<-as.Date(y1size$Date, "%m/%d/%Y")
#make R understand dates
y1meansize<-ddply(y1size,.(Date,Site,Pop),summarise, mean_size=mean(Length.mm,na.rm=T))
#create table of ave size for outplant and year one for each pop at each site
#print it out
print(y1meansize)
##          Date       Site Pop mean_size
## 1  2013-08-16    Fidalgo  2H     10.67
## 2  2013-08-16    Fidalgo  2N     11.60
## 3  2013-08-16    Fidalgo  2S     11.25
## 4  2013-08-16 Manchester  4H     10.53
## 5  2013-08-16 Manchester  4N     13.40
## 6  2013-08-16 Manchester  4S     11.30
## 7  2013-08-16 Oyster Bay  1H     10.49
## 8  2013-08-16 Oyster Bay  1N     10.90
## 9  2013-08-16 Oyster Bay  1S     12.15
## 10 2014-09-19 Oyster Bay  1H     27.96
## 11 2014-09-19 Oyster Bay  1N     35.81
## 12 2014-09-19 Oyster Bay  1S     27.98
## 13 2014-10-17    Fidalgo  2H     24.40
## 14 2014-10-17    Fidalgo  2N     29.10
## 15 2014-10-17    Fidalgo  2S     28.91
## 16 2014-10-24 Manchester  4H     21.49
## 17 2014-10-24 Manchester  4N     24.37
## 18 2014-10-24 Manchester  4S     23.99
#now we need to create subsets for each site for out plant and end of year 1
outmany1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2013-08-16"&Site=="Manchester")
outfidy1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2013-08-16"&Site=="Fidalgo")
outoysy1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2013-08-16"&Site=="Oyster Bay")
endmany1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2014-10-24"&Site=="Manchester")
endfidy1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2014-10-17"&Site=="Fidalgo")
endoysy1<-ddply(y1size,.(Length.mm,Pop,Tray,Sample,Area),subset,Date=="2014-09-19"&Site=="Oyster Bay")

#Plot the distributions of the sizes for each pop at each site for outplant and end of year 1.
#This allows us to visualize differences in populations.
ggplot()+
  geom_density(data=outmany1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  geom_density(data=endmany1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  scale_colour_manual(values=c("blue","purple","orange"))+
  scale_fill_manual(values=c("blue","purple","orange"))+
  ggtitle("Size Comparison\nAugust 2013 vs. October 2014")

plot of chunk unnamed-chunk-1

ggplot()+
  geom_density(data=outfidy1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  geom_density(data=endfidy1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  scale_colour_manual(values=c("blue","purple","orange"))+
  scale_fill_manual(values=c("blue","purple","orange"))+
  ggtitle("Size Comparison\nAugust 2013 vs. October 2014")

plot of chunk unnamed-chunk-1

ggplot()+
  geom_density(data=outoysy1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  geom_density(data=endoysy1,aes(x=Length.mm,group=Pop,colour=Pop,fill=Pop),alpha=0.3)+
  scale_colour_manual(values=c("blue","purple","orange"))+
  scale_fill_manual(values=c("blue","purple","orange"))+
  ggtitle("Size Comparison\nAugust 2013 vs. September 2014")

plot of chunk unnamed-chunk-1

#Now we need to do some anovas as we assume the data is normally distributed.
#First we have to create a column of pop labels that don't have the site designation in them
y1size$Pop2<-y1size$Pop
y1size$Pop2<-revalue(y1size$Pop2,c("1H"="H","2H"="H","4H"="H","1N"="N","2N"="N","4N"="N","1S"="S","2S"="S","4S"="S"))
#Here we subset the data set to only include data from the end of year 1
endy1<-ddply(y1size,.(Length.mm,Site,Pop,Tray,Sample,Area,Pop2),subset,Date>="2014-09-19")
#Run the ANOVA comparing site,pop, and site:pop to show significant differences
sizeaov<-aov(endy1$Length.mm~endy1$Site+endy1$Pop2+endy1$Site:endy1$Pop2,endy1)
summary(sizeaov)
##                         Df Sum Sq Mean Sq F value Pr(>F)    
## endy1$Site               2  14168    7084   263.5 <2e-16 ***
## endy1$Pop2               2   8254    4127   153.5 <2e-16 ***
## endy1$Site:endy1$Pop2    4   3368     842    31.3 <2e-16 ***
## Residuals             2283  61368      27                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Everything is significantly different. YAY!
#Time to do a Tukey test to illucidate what is different from what exactly. 
sizesvptukey<-TukeyHSD(sizeaov)
print(sizesvptukey)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = endy1$Length.mm ~ endy1$Site + endy1$Pop2 + endy1$Site:endy1$Pop2, data = endy1)
## 
## $`endy1$Site`
##                         diff    lwr    upr p adj
## Manchester-Fidalgo    -4.281 -4.862 -3.701     0
## Oyster Bay-Fidalgo     2.451  1.772  3.129     0
## Oyster Bay-Manchester  6.732  6.000  7.463     0
## 
## $`endy1$Pop2`
##       diff    lwr     upr p adj
## N-H  4.545  3.922  5.1683     0
## S-H  2.975  2.358  3.5911     0
## S-N -1.571 -2.198 -0.9432     0
## 
## $`endy1$Site:endy1$Pop2`
##                                diff      lwr      upr  p adj
## Manchester:H-Fidalgo:H     -2.90887  -4.2476  -1.5702 0.0000
## Oyster Bay:H-Fidalgo:H      3.55552   2.1100   5.0010 0.0000
## Fidalgo:N-Fidalgo:H         4.69502   3.5223   5.8677 0.0000
## Manchester:N-Fidalgo:H     -0.02951  -1.4073   1.3483 1.0000
## Oyster Bay:N-Fidalgo:H     11.40509   9.6848  13.1254 0.0000
## Fidalgo:S-Fidalgo:H         4.50490   3.3091   5.7007 0.0000
## Manchester:S-Fidalgo:H     -0.41572  -1.7355   0.9041 0.9879
## Oyster Bay:S-Fidalgo:H      3.58281   1.9898   5.1758 0.0000
## Oyster Bay:H-Manchester:H   6.46439   4.9055   8.0232 0.0000
## Fidalgo:N-Manchester:H      7.60389   6.2941   8.9137 0.0000
## Manchester:N-Manchester:H   2.87936   1.3831   4.3756 0.0000
## Oyster Bay:N-Manchester:H  14.31397  12.4974  16.1306 0.0000
## Fidalgo:S-Manchester:H      7.41377   6.0832   8.7443 0.0000
## Manchester:S-Manchester:H   2.49316   1.0501   3.9362 0.0000
## Oyster Bay:S-Manchester:H   6.49168   4.7951   8.1882 0.0000
## Fidalgo:N-Oyster Bay:H      1.13950  -0.2793   2.5583 0.2355
## Manchester:N-Oyster Bay:H  -3.58503  -5.1776  -1.9925 0.0000
## Oyster Bay:N-Oyster Bay:H   7.84957   5.9529   9.7463 0.0000
## Fidalgo:S-Oyster Bay:H      0.94938  -0.4886   2.3874 0.5085
## Manchester:S-Oyster Bay:H  -3.97124  -5.5139  -2.4286 0.0000
## Oyster Bay:S-Oyster Bay:H   0.02729  -1.7548   1.8093 1.0000
## Manchester:N-Fidalgo:N     -4.72453  -6.0743  -3.3748 0.0000
## Oyster Bay:N-Fidalgo:N      6.71007   5.0121   8.4080 0.0000
## Fidalgo:S-Fidalgo:N        -0.19012  -1.3535   0.9733 0.9999
## Manchester:S-Fidalgo:N     -5.11074  -6.4013  -3.8202 0.0000
## Oyster Bay:S-Fidalgo:N     -1.11221  -2.6811   0.4567 0.4052
## Oyster Bay:N-Manchester:N  11.43460   9.5890  13.2802 0.0000
## Fidalgo:S-Manchester:N      4.53441   3.1645   5.9043 0.0000
## Manchester:S-Manchester:N  -0.38621  -1.8656   1.0932 0.9966
## Oyster Bay:S-Manchester:N   3.61232   1.8848   5.3399 0.0000
## Fidalgo:S-Oyster Bay:N     -6.90020  -8.6142  -5.1862 0.0000
## Manchester:S-Oyster Bay:N -11.82081 -13.6235 -10.0181 0.0000
## Oyster Bay:S-Oyster Bay:N  -7.82228  -9.8337  -5.8109 0.0000
## Manchester:S-Fidalgo:S     -4.92061  -6.2322  -3.6090 0.0000
## Oyster Bay:S-Fidalgo:S     -0.92209  -2.5083   0.6641 0.6791
## Oyster Bay:S-Manchester:S   3.99853   2.3168   5.6802 0.0000
#WOW That's a lot of differences. It would be easier to say what isn't significantly different.
#Manchester Fid Pop is not sig diff from Fidalgo Dabob pop
#Manchester Oys pop is not sig diff from Fidalgo Dabob pop
#Fidalgo Fid pop is not sig diff from Oyster Bay Dabob pop
#Fidalgo Oys pop is not sig diff from Oyster Bay Dabob pop
#Oyster Bay Oys pop is essentially equivalent to Oyster Bay Dabob pop
#Fidalgo Oys pop is not sig diff from Fidalgo Fid pop
#Oyster Bay Oys pop is not sig diff from Fidalgo Fid pop
#Manchester Oys pop is not sig diff from Manchester Fid pop
#Oyster Bay Oys pop is not sig diff from Fidalgo Oys Pop

No comments:

Post a Comment