-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPAMPwrProject.Rmd
187 lines (118 loc) · 4.42 KB
/
PAMPwrProject.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
---
title: "Power Simulation Notes"
author: "Eiren Jacobson"
date: "April 18, 2017"
output: pdf_document
---
header-includes:
- \usepackage{caption}
output:
pdf_document:
latex_engine: xelatex
---
\begin{center}
\Large{Eiren Jacobson}
\end{center}
\begin{center}
\large{`r format(Sys.time(), '%d %B %Y')`}
\end{center}
```{r ChunkOpts, echo=FALSE}
knitr::opts_chunk$set(echo=TRUE, results="hide", message=FALSE, warning=FALSE)
```
# Variability in the 3-year passive acoustic dataset
* The mean PPS values measured across all stations and years is shown in the following table
```{r Setup, results="hide", echo=FALSE}
require(data.table)
require(ggplot2)
require(reshape2)
require(nlme)
require(plyr)
load("./Data/CpodPPS.RData") # PPS.Data created by cpodParse
load("./Data/CXY.RData") # CXY created by spDensity
load("./Data/predGrid.RData") #gridOut created by spDensity
```
```{r, results="markup"}
# Need to change names to match
names(CXY)[4]<- "DENSITY"
names(CXY)[1] <- "MOORING"
# Get annual mean PPS values for each station
total <- PPS.Data[,.(Total = sum(PPS)),
by = .(YEAR, MOORING)]
vartab <- total[,.(CV = sd(Total)/mean(Total)),
by = .(MOORING)]
m <- merge(vartab, CXY[,c(1,4)], by="MOORING")
plot(m$DENSITY, m$CV)
mod <- lm(CV ~ DENSITY, data=m)
# Attach the acoustic data to the aerial survey density estimates
total <- merge(CXY[,c(1,4)], total, by="MOORING")
all.pps <- merge(CXY[,c(1,4)], PPS.Data, by="MOORING")
# Create a spatial model of PPS as a function of year and underlying mean density
D.model <- glm(log(Total) ~ factor(YEAR, ordered=TRUE) + log(DENSITY),
data = total)
Dm <- glm(log(Total) ~ 0 + factor(YEAR, ordered=TRUE) + log(DENSITY),
family=quasi(link="identity", variance="mu"), data=total)
summary(Dm)
plot(Dm)
# glm(Total ~ factor(YEAR) + DENSITY, family=poisson(link="log"), data=total) AIC = 465658
#
# save(D.model, file="./Data/Dmodel.RData")
# Use model to predict expected PPS values as a function of density over the three years
pred.grid <- expand.grid("DENSITY"=seq(0.001, 3, by=0.001), "YEAR"=as.factor(2013:2015))
p.vals <- predict(Dm, pred.grid, se.fit=TRUE)
pred.grid <- cbind.data.frame(pred.grid, PredPPS=p.vals)
ggplot()+
geom_line(data=pred.grid,
aes(x=DENSITY, y=exp(PredPPS.fit), color=YEAR))+
geom_point(data=total,
aes(x=DENSITY, y=Total, color=as.factor(YEAR)))+
ylab("Total (91 day) PPS")+
theme_bw()
```
```{r}
```
```{r}
source("./Scripts/simSpatial")
n.moorings <- c(10, 25, 50, 75, 100)
p.change <- seq(-0.5, 0.5, by = 0.05)
nrep <- 1000
tab.res <- data.frame()
for (no in n.moorings){
for (pc in p.change){
r.est <- vector()
for (i in 1:nrep){
nd <- as.matrix(simSpatial(no, 10, pc, b = 0))
nd <- as.data.frame(nd, names=c("MOORING", "YEAR", "DENSITY", "PPS"))
nm <- lme(fixed = log(PPS) ~ YEAR,
random = ~ 1 | MOORING, data = nd)
ye <- ifelse(summary(nm)$tTable[10]<0.05 & sign(summary(nm)$tTable[2])==sign(pc), 1, 0)
r.est <- c(r.est, ye)
} # end i in n.rep
tab.res <- rbind.data.frame(tab.res,
cbind.data.frame("No.M" = no, "P.Change" = pc,
"P.Dect" = sum(r.est)/length(r.est)))
} # end pc in p.change
} # end nm in no. moorings
cbPalette <- c("#E69F00", "#56B4E9", "#009E73","#0072B2", "#CC79A7")
ggsave("./Figures/ChangeVPower.pdf",
ggplot()+
geom_line(data=data.frame("P.Change"=c(-0.5, 0.5), "P.Dect"=c(0.8, 0.8)),
aes(x=P.Change, y=P.Dect), linetype=1, size=1)+
geom_line(data=tab.res, aes(x=P.Change, y=P.Dect,
group=as.factor(No.M), color=as.factor(No.M)), size=1)+
scale_color_manual(name="No. Moorings", values=cbPalette)+
# geom_line(data=data.frame("P.Change"=c(-0.5, -0.5), "P.Dect"=c(0.14, 0.33)),
# aes(x=P.Change, y=P.Dect) ,size =3, color = "red")+
xlab("Percent Change in Population")+
ylab("Power")+
scale_x_continuous(labels=scales::percent)+
theme_bw()+
theme(legend.key=element_blank()),
height=4, width=5, units="in")
table <- dcast(tab.res, No.M ~ P.Change)
print(table)
```
```{r}
require(reshape2)
load("./Data/simSpatialb0Resultsx10000.RData")
table <- dcast(tab.res, No.M ~ P.Change, value.var = "P.Dect")
```