This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_project_code.Rmd
389 lines (294 loc) · 10.2 KB
/
final_project_code.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
---
title: "Untitled"
author: "Yara Ghabra 1006336056, Grace Walker Mitchell 1005046326"
date: "2022-11-22"
output: html_document
---
```{r}
library(tidyverse)
library(lmerTest)
library(lme4)
library(ggalt)
library(MuMIn)
library(sjmisc)
library(car)
```
```{r}
#writing datasets into CSV for sharing in github
write.csv(pheno_data, "final_project_full_data.csv")
write.csv(spp_filt, "final_project_filtered_data.csv")
```
###Data Manipulation
```{r}
#read in the unaltered phenophase data set
pheno_data<-read.csv("/Users/gracewalkermitchell/Desktop/EEB313/project/CCIN13215_20220120_tundra_phenology_database.csv")
#filter the dataset by flowering phenophase, control treatment, forb functional group, and species of interest (species that occur over the same time span with a lot of observations)
pheno_data_filt<-pheno_data %>%
filter(phenophase == "flower") %>%
filter(treatment == "CTL") %>%
filter(functional_group == "forb") %>%
filter(spp == "CARBEL" | spp == "DRALAC" | spp == "PAPRAD" | spp == "POLVIV" | spp == "PEDHIR" | spp == "SAXOPP" | spp == "SILACA" | spp == "STECRA")
#seeing how many species occur in both wet and dry
numbers<-pheno_data_filt %>%
group_by(spp,soil_moisture) %>%
summarise(n=n()) %>%
ungroup() %>%
filter(soil_moisture == "wet" | soil_moisture =="dry" | soil_moisture == "moist")
#filtering by four species of interest - occur in both wet and dry, have a similar time range of observations, and have a lot of observations
spp_filt<-pheno_data_filt %>%
filter(spp == "DRALAC" | spp == "PAPRAD" | spp == "POLVIV" | spp == "PEDHIR") %>%
filter(soil_moisture =="wet" | soil_moisture =="moist" | soil_moisture =="dry") %>%
filter(study_area != "Val Bercla") #excluding this because it is an alpine site, not an arctic site
#only including wet and dry, not moist
spp_filt[spp_filt == "moist"] <- "wet"
```
### T tests
```{r}
#creating a separate dataset for each species for t tests
polviv_ttest<-spp_filt %>%
filter(spp == "POLVIV")
dralac_ttest<-spp_filt %>%
filter(spp == "DRALAC")
paprad_ttest<-spp_filt %>%
filter(spp == "PAPRAD")
pedhir_ttest<-spp_filt %>%
filter(spp == "PEDHIR")
#removing blank values from the POLVIV dataset
polviv_ttest<-polviv_ttest %>%
filter(soil_moisture == "dry" | soil_moisture == "wet")
```
T tests have two assumptions:
1. The dependent variable must be normally distributed in both samples
2. The variance of the dependent variable must be approximately equal between the two samples
Shapiro-wilk's test:
in this case if we get a non-significant p-value, our data is normally distributed
```{r initial test of t test assumptions}
#Shapiro-wilk's test to test normality
shapiro.test(paprad_ttest$DOY)
#not normally distributed
shapiro.test(polviv_ttest$DOY)
#not normally distributed
shapiro.test(pedhir_ttest$DOY)
#not normally distributed
shapiro.test(dralac_ttest$DOY)
#normally distributed
# F-test to test for homogeneity in variances
# H0: Ratio of variance is equal to 1, i.e., samples have equal variance
var.test(DOY ~ soil_moisture, data=paprad_ttest)
var.test(DOY ~ soil_moisture, data=polviv_ttest)
var.test(DOY ~ soil_moisture, data=pedhir_ttest)
var.test(DOY ~ soil_moisture, data=dralac_ttest)
```
```{r data transformations for normality}
#transforming data to achieve normal distribution
hist(spp_filt$DOY) #right skewed
#performing log base 10 transformation
spp_filt<- spp_filt %>%
mutate(inv_DOY= 1/DOY , log10_DOY= log10(DOY) , sqrt_DOY= sqrt(DOY) , log_DOY= log(DOY))
#---------------------------------------------------------------
#have to run this again to update
polviv_ttest<-spp_filt %>%
filter(spp == "POLVIV")
dralac_ttest<-spp_filt %>%
filter(spp == "DRALAC")
paprad_ttest<-spp_filt %>%
filter(spp == "PAPRAD")
pedhir_ttest<-spp_filt %>%
filter(spp == "PEDHIR")
#removing blank values from the POLVIV dataset
polviv_ttest<-polviv_ttest %>%
filter(soil_moisture == "dry" | soil_moisture == "wet")
#---------------------------------------------------------------
hist(polviv_ttest$inv_DOY) #normal now
hist(dralac_ttest$inv_DOY) #normalish -- doesn't need transformations
hist(pedhir_ttest$inv_DOY) #left skewed
hist(paprad_ttest$log10_DOY) #normal
#checking Shapiro-wilk's test again:
shapiro.test(paprad_ttest$log10_DOY)
#not normally distributed
shapiro.test(polviv_ttest$inv_DOY)
#not normally distributed
shapiro.test(pedhir_ttest$inv_DOY)
#not normally distributed
shapiro.test(dralac_ttest$DOY)
#normally distributed
#discuss that there are errors in our results cuz data is not significatly normal and dont have homogeniety of variances
```
```{r f test after transformations}
#F tests after data transformations
var.test(log10_DOY ~ soil_moisture, data=paprad_ttest)
var.test(inv_DOY ~ soil_moisture, data=polviv_ttest)
var.test(inv_DOY ~ soil_moisture, data=pedhir_ttest)
# var.test(DOY ~ soil_moisture, data=dralac_ttest)
```
```{r}
#performing t test for paprad
t.test(log10_DOY~soil_moisture, data = paprad_ttest)
```
```{r}
#t test for polviv
t.test(inv_DOY~soil_moisture, data = polviv_ttest)
```
```{r}
# t test for dralac
t.test(DOY~soil_moisture, data = dralac_ttest)
```
```{r}
#t test for pedhir
t.test(inv_DOY~soil_moisture, data = pedhir_ttest)
```
### Modelling
```{r}
#this is just for f-ing around, dont include
t.test(DOY~soil_moisture, data=spp_filt)
summary(aov(DOY~spp, data=spp_filt))
fixed_model<- lm(DOY~soil_moisture*spp, data=spp_filt)
summary(fixed_model)
```
```{r}
#mixed models
mix_model_int1<-lmer(DOY~soil_moisture * spp +
(1|subsite),
data = spp_filt)
mix_model_int2<-lmer(DOY~soil_moisture * spp +
(1|study_area),
data = spp_filt)
mix_model_int3<-lmer(DOY~soil_moisture * spp +
(1|study_area) + (1|subsite),
data = spp_filt)
mix_model_int4<-lmer(DOY~soil_moisture * spp + year +
(1|study_area) + (1|subsite),
data = spp_filt)
mix_model_int5<- lmer(DOY~soil_moisture * spp * year +
(1|subsite),
data = spp_filt)
mix_model_int6<-lmer(DOY~soil_moisture * spp * year +
(1|study_area),
data = spp_filt)
```
```{r}
#creating a fixed model to test the effects of soil moisture on flowering date
fixed_model2<-lm(DOY~soil_moisture * spp + subsite,
data = spp_filt)
summary(fixed_model2)
fixed_model3<-lm(DOY~soil_moisture * spp + study_area,
data = spp_filt)
summary(fixed_model2)
```
```{r}
#getting DF
Anova(mix_model_int3, p.adjust.methods = TRUE, type = 2) # in package car to get degrees of freedoms
```
```{r}
#Using AICc to compare all models
AICc(mix_model_int1, mix_model_int2, mix_model_int3, mix_model_int4, mix_model_int5, mix_model_int6)
```
```{r}
#re print the best model and interpret
mix_model_int3<-lmer(DOY~soil_moisture * spp +
(1|study_area) + (1|subsite),
data = spp_filt)
summary(mix_model_int3)
library(emmeans)
emmeans(mix_model_int3, pairwise ~ spp*soil_moisture)
```
###check assumptions for linear model!!!!!
1. Normality at each X value
2. Homogeneity of variances at each X
3. Independence of observations
```{r}
library(effects)
library(sjPlot)
library(glmmTMB)
plot_model(mix_model_int3, type='diag')
#our model meets all the assumptions
```
### Graphing
```{r}
#simple visualizations of our data
spp_filt %>%
ggplot(aes(x=soil_moisture, y=DOY, colour=spp))+
geom_boxplot()+
labs(x="Soil Moisture", y="Flowering Day")
spp_filt %>%
ggplot(aes(x=spp, y=DOY, colour=soil_moisture))+
geom_boxplot()+
labs(x="Species", y="Day of Flowering")+
theme_classic()#visualization of the t test
avg_doy<-spp_filt %>%
group_by(year, soil_moisture) %>%
mutate(avg_DOY= mean(DOY))
ggplot(data = avg_doy, aes(x=year, y=avg_DOY, colour=soil_moisture))+
geom_point()+
labs(x="Year", y="Day of Flowering")+
facet_wrap(~spp)+
geom_line()+
theme_classic()
#this shows that theres no change in trend through time^
```
```{r}
#calculating median day of flowering for each species to accompany the boxplot
spp_filt %>%
filter(spp == "DRALAC") %>%
filter(soil_moisture == "dry") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "DRALAC") %>%
filter(soil_moisture == "wet") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "PAPRAD") %>%
filter(soil_moisture == "dry") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "PAPRAD") %>%
filter(soil_moisture == "wet") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "PEDHIR") %>%
filter(soil_moisture == "dry") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "PEDHIR") %>%
filter(soil_moisture == "wet") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "POLVIV") %>%
filter(soil_moisture == "dry") %>%
summarize(median = median(DOY))
spp_filt %>%
filter(spp == "POLVIV") %>%
filter(soil_moisture == "wet") %>%
summarize(median = median(DOY))
```
```{r}
#finding flowering time length to compare between wet and dry soils
flower_end<-pheno_data %>%
filter(phenophase == "flowerend"| phenophase == "flower") %>%
filter(treatment == "CTL") %>%
filter(functional_group == "forb") %>%
filter(spp == "DRALAC" | spp == "PAPRAD" | spp == "POLVIV" | spp == "PEDHIR") %>%
pivot_wider(names_from = phenophase, values_from = DOY) %>%
filter(flower != "NA") %>%
filter(flowerend != "NA") %>%
mutate(flowering_length = flowerend - flower)
flower_end$soil_moisture <- gsub("moist","wet",flower_end$soil_moisture)
#visualizing the data
flower_end %>%
ggplot(aes(x=spp, y=flowering_length, colour=soil_moisture))+
geom_boxplot()+
labs(x="Species", y="Flowering Length")
#t.tests to compare flowering length in dry and wet soil
dralac<- flower_end %>%
filter(spp== "DRALAC")
t.test(flowering_length~soil_moisture, data=dralac)
pedhir<- flower_end %>%
filter(spp== "PEDHIR")
t.test(flowering_length~soil_moisture, data=pedhir)
paprad<- flower_end %>%
filter(spp== "PAPRAD")
t.test(flowering_length~soil_moisture, data=paprad)
polviv<- flower_end %>%
filter(spp== "POLVIV")
t.test(flowering_length~soil_moisture, data=polviv)
```