This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
forked from ajijohn/ClimateBiology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobomussel.Rmd
330 lines (241 loc) · 9.26 KB
/
Robomussel.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---
title: "Robomussel Notebook"
output: html_notebook
---
#ROBOMUSSEL ANALYSIS
This is an [Robomussel Markdown](http://rmarkdown.rstudio.com) Notebook to analyze mussel intertidal data.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*.
```{r}
#load libraries
library(plyr)
library(dplyr)
library(reshape2)
library(tidyr)
library(ggplot2)
source("./analysis/TempcyclesAnalysis.R")
```
#SITES
# WaOr Tatoosh Island, WA 1660.1 48.39 124.74
# WaOr Boiler Bay, OR 1260.7 44.83 124.05
# WaOr Strawberry Hill, OR 1196 44.25 124.12
# CenCal Hopkins, CA 327.1 36.62 121.90
# CenCal Piedras Blancas, CA 208.11 35.66 121.28
# CenCal Cambria, CA 185.66 35.54 121.10
# SoCal Lompoc, CA 84.175 34.72 120.61
# SoCal Jalama, CA 57.722 34.50 120.50
# SoCal Alegria, CA 37.284 34.47 120.28
# SoCal Coal Oil Point (COP), CA 0 34.41 119.88
Load data
```{r}
#-----------------
#Site data
site.dat= read.csv("./data//musselREADME.csv")
#Load robomussel data
te.max <- readRDS("./data/tedat.rds")
#te.max= read.csv("tedat.csv")
```
```{r}
#TODO Check the impact of NA and duplicates
#Fix duplicate CP in WA
te.max[which(te.max$lat==48.45135),"site"]<-"CPWA"
```
#Analysis Plots
Housekeeping first
```{r}
#count by site, subsite, year
te.count = te.max %>% group_by(year,site, subsite) %>% summarise( count=length(MaxTemp_C) )
te.count= as.data.frame(te.count)
#subset sites
te.max2= subset(te.max, te.max$site %in% c("SD","BB","PD") ) # "HS",
#te.max2= subset(te.max, te.max$lat %in% c(48.39137,44.83064,35.66582,34.46717) )
# USWACC 48.5494 -123.0059667 Colins Cove
# USWACP 48.45135 -122.9617833 Cattle Point
#* USWASD 48.39136667 -124.7383667 Strawberry Point
#* USORBB 44.83064 -124.06005 Boiler Bay
#* USCAPD 35.66581667 -121.2867167 Piedras
# USCAAG 34.46716667 -120.2770333 Alegria
#time series
te.max1= subset(te.max2, te.max2$year==2002)
#restrict to summer
#May 1 through September: 121:273
te.max1= subset(te.max1, te.max1$doy>120 & te.max1$doy<274)
#ggplot(data=te.max1, aes(x=doy, y = MaxTemp_C, color=subsite ))+geom_line() +theme_bw()+facet_wrap(~site)
#by tidal height
#ggplot(data=te.max1, aes(x=doy, y = MaxTemp_C, color=height ))+geom_line() +theme_bw()+facet_wrap(~site)
```
#Maximum selected temperatures for selected sites (by Latitude)
```{r}
fig2a<- ggplot(data=te.max1, aes(x=doy, y = MaxTemp_C, color=subsite ))+geom_line(alpha=0.8) +theme_bw()+
facet_wrap(~lat, nrow=1)+ guides(color=FALSE)+labs(x = "Day of year",y="Maximum daily temperature (°C)")
fig2a
```
Alt
# AJI - Applied lowess smoothing to see the trends.
```{r}
fig2a_alt <- te.max1 %>% as.data.frame() %>% ggplot(aes(doy,MaxTemp_C,group=subsite,color=subsite)) +
stat_smooth(se = FALSE) +
ggtitle("Maximum Temperatures by Latitude")+ theme_bw()+
facet_wrap(~lat, nrow=1)+ guides(color=FALSE)+
xlab("Day of year") + ylab("Maximum daily temperature (°C)")
fig2a_alt
```
# Frequency Analysis -
https://github.com/georgebiogeekwang/tempcycles/
power spectrum
x: frequency (1/days)
y: log amplitude
```{r}
fseq= exp(seq(log(0.001), log(1), length.out = 400))
sites= c("SD","BB","PD") #levels(te.max2$site)
subsites= levels(te.max2$subsite)
pow.out= array(NA, dim=c(length(sites),length(subsites),length(fseq) ) )
for(site.k in 1:length(sites))
{
te.dat= te.max2[which(te.max2$site==sites[site.k]),]
subsites1= levels(te.dat$subsite)
for(subsite.k in 1:length(subsites)) {
te.dat1= te.dat[which(te.dat$subsite==subsites1[subsite.k]),]
pow.out[site.k, subsite.k,] <- spec_lomb_phase(te.dat1$MaxTemp_C, te.dat1$j, freq=fseq)$cyc_range
}
}
dimnames(pow.out)[[1]]<- sites
dimnames(pow.out)[[2]]<- 1:75
#to long format
for(site.k in 1:length(sites)){
pow1= pow.out[site.k,,]
pow1= na.omit(pow1)
pow1m= melt(pow1)
pow1m$site= sites[site.k]
if(site.k==1)pow=pow1m
if(site.k>1)pow=rbind(pow,pow1m)
}
colnames(pow)[1:3]=c("subsite","freq","cyc_range")
#correct freq values
pow$freq= fseq[pow$freq]
#sort by frequency
pow= pow[order(pow$site, pow$subsite, pow$freq),]
pow$subsite= factor(pow$subsite)
#freq, amp plot
#add latitude
site.dat1= te.max %>% group_by(site) %>% summarise( lat=lat[1],zone=zone[1],tidal.height..m.=tidal.height..m.[1],substrate=substrate[1] )
match1= match(pow$site, site.dat1$site)
pow$lat= site.dat1$lat[match1]
```
```{r}
fig2b<- ggplot(data=pow, aes(x=log(freq), y = log(cyc_range/2), color=subsite))+geom_line(alpha=0.8) +theme_classic()+facet_wrap(~lat, nrow=1)+ guides(color=FALSE)+
geom_vline(xintercept=-2.639, color="gray")+geom_vline(xintercept=-1.946, color="gray")+geom_vline(xintercept=-3.40, color="gray")+geom_vline(xintercept=-5.9, color="gray")+
labs(x = "log (frequency) (1/days)",y="log (amplitude)")
#TODO #add lines for 1 week, 2 week, month, year
fig2b
```
#===================================================
#Quilt plot
#Mean Daily maximum by Lat/Month
```{r}
#round lat
te.max$lat.lab= round(te.max$lat,2)
#mean daily maximum by month
te.month = te.max %>% group_by(lat, month, lat.lab) %>% summarise( max=max(MaxTemp_C), mean.max=mean(MaxTemp_C), q75= quantile(MaxTemp_C, 0.75), q95= quantile(MaxTemp_C, 0.95) )
fig2<- ggplot(te.month) +
aes(x = month, y = as.factor(lat.lab) ) +
geom_tile(aes(fill = mean.max)) +
coord_equal()+
scale_fill_gradientn(colours = rev(heat.colors(10)), name="temperature (°C)" )+
#scale_fill_distiller(palette="Spectral", na.value="white", name="max temperature (°C)") +
theme_bw(base_size = 18)+xlab("month")+ylab("latitude (°)")+ theme(legend.position="bottom") #+ coord_fixed(ratio = 4)
fig2
```
#Extremes Analysis
Load libraries
```{r}
library(ismev) #for gev
library(reshape)
library(maptools) #for mapping
library(evd) #for extremes value distributions
library(extRemes)
library(fExtremes) # generate gev
```
Housekeeping for Extremes
```{r}
sites= levels(te.max$site) #c("SD","BB","PD")
subsites= levels(te.max$subsite)
gev.out= array(NA, dim=c(length(sites),length(subsites),13 ) )
for(site.k in 1:length(sites))
{
te.dat= te.max[which(te.max$site==sites[site.k]),]
subsites1= levels(te.dat$subsite)
for(subsite.k in 1:length(subsites)) {
te.dat1= te.dat[which(te.dat$subsite==subsites1[subsite.k]),]
#add site data
gev.out[site.k, subsite.k,12]= te.dat1$lat[1]
#gev.out[site.k, subsite.k,13]= te.dat1$height[1]
#Generalized extreme value distribution
dat1= na.omit(te.dat1$MaxTemp_C) ##CHECK na.omit appropraite?
if(length(dat1)>365){
mod.gev<- try(gev.fit(dat1, show=FALSE) ) #stationary
if(class(mod.gev)!="try-error") gev.out[site.k, subsite.k,1]<-mod.gev$nllh
if(class(mod.gev)!="try-error") gev.out[site.k, subsite.k,2:4]<-mod.gev$mle #add another for non-stat
if(class(mod.gev)!="try-error") gev.out[site.k, subsite.k,5]<-mod.gev$conv #add another for non-stat
#Generalized pareto distribution, for number of times exceeds threshold
thresh= 35
#stationary
mod.gpd <- try(gpd.fit(dat1, thresh, npy=365)) #stationary
if(class(mod.gpd)!="try-error") gev.out[site.k, subsite.k,6]<-mod.gpd$rate
## nonstationary
# try(mod.gpd<-gpd.fit(dat1, 40, npy=92, ydat=as.matrix(te.dat1$year), sigl=1),silent = FALSE)
#RETURN LEVELS: MLE Fitting of GPD - package extRemes
mpers= c(10,20,50,100)
for(m in 1:length(mpers)){
pot.day<- try( fpot(dat1, threshold=35, npp=365.25, mper=mpers[m], std.err = FALSE) )
if(class(pot.day)!="try-error") gev.out[site.k, subsite.k,6+m]=pot.day$estimate[1]
}
#proportion above threshold
if(class(pot.day)!="try-error") gev.out[site.k, subsite.k,11]=pot.day$pat
} #end check time series
} #end subsites
} #end sites
#-------------------------
```
Plot
```{r}
pow.out=gev.out
dimnames(pow.out)[[1]]<- sites
dimnames(pow.out)[[2]]<- 1:19
dimnames(pow.out)[[3]]<- c("gev.nllh", "gev.loc", "gev.scale", "gev.shape", "conv", "rate", "return10", "return20", "return50", "return100","pat","lat","height")
#to long format
for(site.k in 1:length(sites)){
pow1= pow.out[site.k,,]
#pow1= na.omit(pow1)
pow1m= melt(pow1)
pow1m$site= sites[site.k]
if(site.k==1)pow=pow1m
if(site.k>1)pow=rbind(pow,pow1m)
}
#--------------------
# ADD SITE INFO
names(pow)[1:2]=c("subsite","var")
pow$ssite= paste(pow$site,pow$subsite, sep=".")
pow.site= subset(pow, pow$var=="lat")
pow.site= pow.site[!duplicated(pow.site$ssite),]
match1= match(pow$ssite, pow.site$ssite)
pow$lat= pow.site$value[match1]
#====================
## PLOT
#dimnames(pow.out)[[3]]<- c("gev.nllh", "gev.loc", "gev.scale", "gev.shape", "conv", "rate", "return10", "return20", "return50", "return100","pat", "lat","height")
pow1= pow[pow$var %in% c("gev.loc", "gev.scale", "gev.shape", "pat", "return100"),]
#get rid of return100 outlier for ploting purposes
pow1= subset(pow1, pow1$value<300)
#revise labels
pow1$var <- factor(pow1$var, labels = c("location", "scale", "shape", "percent above threshold", "100 year return"))
#ggplot(data=pow1, aes(x=site, y = value, color=subsite))+geom_point()+theme_bw()+facet_wrap(~var, scales="free_y")
fig4= ggplot(data=pow1, aes(x=as.factor(lat), y = value, color=subsite))+geom_point()+
theme_bw()+theme(axis.text.x=element_blank())+facet_wrap(~var, scales="free_y")+ guides(color=FALSE)+xlab("latitude (°C)")
#as factor not latitude
fig4
```
```{r}
```
```{r}
```
```{r}
```