-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThomasP.R
334 lines (219 loc) · 9.15 KB
/
ThomasP.R
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
327
328
329
330
331
332
333
334
#install.packages("gitcreds")
library(gitcreds)
#gitcreds_set()
library(tidyverse)
library(dplyr)
library(plotrix)
library(nlme)
library(MASS)
library(gridExtra)
options(contrasts = c("contr.helmert","contr.poly"))
#Litter over time
phragmain <- read.csv("PhragSurvey2017to2022.csv", stringsAsFactors = TRUE)
phragx <- phragmain %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Litter))
litterOT <- phragx %>%
ggplot(aes(x = Year, y = Litter, fill = Site)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragx$Transect)
litterOT
litterOT2 <- phragx %>%
ggplot(aes(x = Year, y = Litter, fill = Transect)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragx$Site)
litterOT2
grid.arrange(litterOT,litterOT2, ncol = 2)
#Diversity over Time
phragy <- phragmain %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Richness))
diversityOT <- phragy %>%
ggplot(aes(x = Year, y = Richness, fill = Site)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragy$Transect)
diversityOT2 <- phragy %>%
ggplot(aes(x = Year, y = Richness, fill = Transect)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragy$Site)
grid.arrange(diversityOT,diversityOT2, ncol = 2)
diversityOT <- phragy %>%
ggplot(aes(x = Year, y = Richness, fill = Site)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragy$Transect)
diversityOT2 <- phragy %>%
ggplot(aes(x = Year, y = Richness, fill = Transect)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(phragy$Site)
grid.arrange(diversityOT,diversityOT2, ncol = 2)
#Line Graphs: LitterOT and DiversityOT
#Litter
phragxtransect <- phragx %>%
group_by(Site, Year, Transect) %>%
summarise(means = mean(Litter), se = std.error(Litter))
litterOT <- phragxtransect %>%
ggplot(aes(x = Year, y = means, color = Site)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymax = means + se, ymin = means - se), color = "black", width = 0.1) +
facet_wrap(phragxtransect$Transect)
litterOT
phragxsite <- phragx %>%
group_by(Site, Year, Transect) %>%
summarise(means = mean(Litter), se = std.error(Litter))
litterOT2 <- phragxsite %>%
ggplot(aes(x = Year, y = means, color = Transect)) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymax = means + se, ymin = means - se), color = "black", width = 0.1) +
facet_wrap(phragxsite$Site)
litterOT2
grid.arrange(litterOT,litterOT2, ncol = 2)
#Diversity
phragy <- phragmain %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Richness))
phragy <- phragy %>%
group_by(Site, Year, Transect) %>%
summarise(means = mean(Richness), se = std.error(Richness))
diversityOT <- phragy %>%
ggplot(aes(x = Year, y = means, color = Site)) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymax = means + se, ymin = means - se), color = "black", width = 0.1) +
facet_wrap(phragy$Transect)
diversityOT2 <- phragy %>%
ggplot(aes(x = Year, y = means, color = Transect)) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymax = means + se, ymin = means - se), color = "black", width = 0.1) +
facet_wrap(phragy$Site)
grid.arrange(diversityOT,diversityOT2, ncol = 2)
#Model Work
#Run models testing the effect of year (factor) and transect and year*transect on phrag abundance. AND a model with the effect of year (linear) and transect and year*transect on phrag abundance
#Run models testing the effect of year (factor) and transect (and phrag abundance) and year*transect on phrag abundance. AND a model with the effect of year (linear) and transect (and phrag abundance) and year*transect on phrag abundance
phragmain <- read.csv("PhragSurvey2017to2022.csv", stringsAsFactors = TRUE)
phragx <- phragmain %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Phragmites.australis))
#gls with year and transect
gls.syt <- gls(Phragmites.australis ~ Site + Year + Transect + Year*Transect, data = phragx)
summary(gls.syt)
#glm with year and transect
phragy <- read.csv("DeleteX.csv", stringsAsFactors = TRUE)
phragy <- phragy %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Phragmites.australis))
glm.syt <- glm(Phragmites.australis ~ Site + Year + Transect + Year*Transect,data = phragy)
summary(glm.syt)
#lm with year and transect
lm.syt <- lm(Phragmites.australis ~ Site + Year + Transect + Year*Transect,data = phragy)
summary(lm.syt)
anova(lm.syt)
# Random effects: Plot,
#lm with random
lme.sytr <- lme(Phragmites.australis ~ Site + Year + Transect + Year*Transect, random = ~1|Plot, data = phragy)
anova(lme.sytr, type = "marginal")
summary(lme.sytr)
anova(lme.sytr, lm.syt)
#lme.sytr is better
model1 <- lme.sytr
#ModelValidation Notes
#plot(data = phragy, phragy$Year, resid(phragy$Phragmites.australis))
#Diversity is the new model
#model validation needed
#Look at lecture on correlation on autoregressive correlation on temporal autocorrelation
#Lecture 8
#Look at spatial correlation (Never did that)
#look at la marsh repository (other one) that has the code for spatial autocorrelation
# -> LamarshGradient2 :Phragabunbiomasslitter (Corsphere latlong) (Spatial correlation)
#Example
#Final stats model for manuscript
#m1<-lme(phraus~MarshClassV*Transect2,random=~1|Site,correlation=corSpher(form = ~ Lat+Long),weights=varIdent(form=~1|MarshClassV.Transect),data=dat17phrag)
#m2<-lme(phraus~MarshClassV*Transect2,random=~1|Site,correlation=corSpher(form = ~ Lat+Long),data=dat17phrag)#
#anova(m1,m2) #var ident not significant
#anova(m2,type="margin")
#m1m<-as.data.frame(summary(emmeans(m2,~Transect2|MarshClassV)))
#ModelValidation
par(mfrow = c(1,2))
fm1 <- fitted(model1)
resid1 <- resid(model1, type = "normalized")
hist(resid(model1, type = "normalized"))
plot(fm1, resid1)
plot(phragy$Transect, resid1)
#non-normal distribution of residuals
model1res <- resid(model1)
model1res <- model1res[is.na(model1res)==F]
acf(model1res)
#High auto correlation?
model1TA <- lme(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
correlation = corAR1(form =~ Year),
random = ~1|Plot, data = phragy)
summary(model1TA)
#results still significant even factoring for temporal autocorrelation
#Testing spatial correlation
#model1SA <- lme(
# Phragmites.australis ~ Site + Year + Transect + Year*Transect,
# correlation=corSpher(form = ~ Lat+Long),
# random = ~1|Plot, data = phragy)
# is there latitude longitude data somewhere? Ask for specific location or use SITE?
model1TApois <- glm(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
family = poisson, data = phragy)
summary(model1TApois)
#residual deviance = ~5 (Bad, should be ~1)
#Do you have to analyze part of the data at once? (Not correct)
#ggplot(data = phragy, aes(x = Year, y = Phragmites.australis) +
# geom_point()+
# geom_smooth(method='glm',method.args=list(family='poisson'))
model1TApois <- glm(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
family = poisson, data = phragy)
drop1(model1TApois,.~., test = "Chi")
model1TAquasi<- glm(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
family = quasipoisson, data = phragy)
summary(model1TAquasi)
#Over dispersion still about 5
model1TAneg<- glm.nb(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
link = "log", data = phragy)
summary(model1TAneg)
#over dispersion is about ~1 now. This is the best one to use.
#use package lme4 package. Add random effects to negative distributions
glm.nb(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
link = "log", data = phragy)
negplot <- resid(model1TAneg, type = "deviance")
negplotpredict <- predict(model1TAneg, type = "link")
par(mfrow = c(1,3))
plot(negplot)
plot(negplotpredict)
#weird!
#Actual Test for Spatial Correlation
SpatialData <- read.csv("latlongPhragSurvey2017to2022.csv", stringsAsFactors = TRUE)
SpatialData2 <- SpatialData %>%
filter(!phragmain$Site %in% c('LUMCON 1', 'LUMCON 2', 'Fontainebleau', 'Turtle Cove'), !is.na(Phragmites.australis))
model1SA <- gls(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
correlation=corSpher(form = ~ Lat+Long|Year),
data = SpatialData2)
summary(model1SA)
anova(model1SA, model1TA)
model1resSA <- resid(model1SA)
model1resSA <- model1res[is.na(model1res)==F]
acf(model1resSA)
#lag = meters
#use package lme4 package. Add random effects to negative distributions
#figure out the negative bionimail distrbution
#compare regular, spatial model, temporal model together
#Reg Model
model1 <- lme(Phragmites.australis ~ Site + Year + Transect + Year*Transect, random = ~1|Plot, data = phragy)
#temporal model
model1TA <- lme(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
correlation = corAR1(form =~ Year),
random = ~1|Plot, data = phragy)
#spatial model
model1SA <- gls(
Phragmites.australis ~ Site + Year + Transect + Year*Transect,
correlation=corSpher(form = ~ Lat+Long|Year),
data = SpatialData2)
anova(model1, model1TA, model1SA)
#model1SA is the best model, making spatial correlation more important