This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGolshan is glimmering.Rmd
303 lines (237 loc) · 8.07 KB
/
Golshan is glimmering.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
---
title: "lol final project"
output: pdf_document
date: "2024-11-14"
---
```{r, message=F, warning=F}
require(tidyverse)
require(deSolve)
require(vegan)
require(car)
require(rgl)
require(ggfortify)
options(rgl.useNULL = TRUE)
rgl::setupKnitr(autoprint = TRUE)
```
```{r}
install.packages("ggfortify")
```
```{r}
install.packages("rgl")
```
```{r}
install.packages("fitdistrplus")
require(fitdistrplus)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
read_csv("Arthropod_List.csv") -> Arthropod_List
read_csv("Plant_Traits.csv") -> Plant_Traits
read_csv("Soil_Traits.csv") -> Soil_Traits
read_csv("Stable_Isotopes.csv") -> Stable_Isotopes
read_csv("final_arthro.csv") -> Arthropod_List_Updated
```
```{r}
Arthropod_list[1,]
```
#Shannon Diversity Index
```{r}
# Sample data: abundance of species
abundance_comm1 <- c()
abundance_comm2 <- c()
abundance_comm3 <- c()
abundance_comm4 <- c()
abundance_comm5 <- c()
shannon_comm1 <- NULL
shannon_comm2 <- NULL
shannon_comm3 <- NULL
shannon_comm4 <- NULL
shannon_comm5 <- NULL
# For loop, First separate by community, and then sum the vector of the row of that species and put into an abundance vector
for (community in 1:5){
if (community == 1){ #in OP
for (species in 1:349){
abundance_comm1[species] = sum(Arthropod_List[species,5:19])
}
shannon_comm1 <- diversity(abundance_comm1, index = "shannon")
}
if (community == 2){ #in OP
for (species in 1:349){
abundance_comm2[species] = sum(Arthropod_List[species,20:34])
}
shannon_comm2 <- diversity(abundance_comm2, index = "shannon")
}
if (community == 3){ #in OP
for (species in 1:349){
abundance_comm3[species] = sum(Arthropod_List[species,35:49])
}
shannon_comm3 <- diversity(abundance_comm3, index = "shannon")
}
if (community == 4){ #in OP
for (species in 1:349){
abundance_comm4[species] = sum(Arthropod_List[species,50:64])
}
shannon_comm4 <- diversity(abundance_comm4, index = "shannon")
}
if (community == 5){ #in OP
for (species in 1:349){
abundance_comm5[species] = sum(Arthropod_List[species,65:79])
}
shannon_comm5 <- diversity(abundance_comm5, index = "shannon")
}
}
```
```{r}
shannon_frame = data.frame(Arthropod_List_Updated$Plot, "Shannon_Diversity")
for (i in 1:75){
abundance_vec = Arthropod_List_Updated[i,2:250]
diversity = diversity(abundance_vec, index = "shannon")
shannon_frame[i,2] = diversity
}
```
```{r}
fixed_arthro = Arthropod_List_Updated[,2:350]
shannon_indices = diversity(fixed_arthro, index = "shannon")
view(shannon_indices)
```
```{r}
final_shannon = data.frame(Treatment = Arthropod_List_Updated$Plot,
Shannon = shannon_indices)
```
```{r}
ggplot(final_shannon, aes(x = Treatment, y = Shannon)) +
geom_bar(stat = "identity", fill = "skyblue", color = "black") +
theme_minimal() +
labs(title = "Shannon Diversity for each Treatment", x = "Treatment", y = "Shannon Diversity")
```
```{r}
install.packages("lme4") # For mixed effects models
install.packages("lmerTest")
```
```{r}
library(lme4)
library(lmerTest)
```
```{r}
soil_C = Soil_Traits$Soil_C
soil_N = Soil_Traits$Soil_N
soil_P = Soil_Traits$Soil_P
soil_pH = Soil_Traits$Soil_pH
soil_sal = Soil_Traits$Soil_Salinity
soil_wat = Soil_Traits$Soil_Water
biomass_above = Plant_Traits$Aboveground_Biomass
plant_dens = Plant_Traits$Plant_Density
leaf_N = Plant_Traits$Leaf_N
leaf_C = Plant_Traits$Leaf_C
leaf_P = Plant_Traits$Leaf_P
```
```{r}
final_shannon$soil_C <- soil_C
final_shannon$soil_N <- soil_N
final_shannon$soil_P <- soil_P
final_shannon$soil_pH <- soil_pH
final_shannon$soil_sal <- soil_sal
final_shannon$soil_wat <- soil_wat
final_shannon$biomass_above <- biomass_above
final_shannon$plant_dens <- plant_dens
final_shannon$leaf_N <- leaf_N
final_shannon$leaf_C <- leaf_C
final_shannon$leaf_P <- leaf_P
```
```{r}
write.csv(final_shannon, "final_shannon.csv")
```
```{r}
# Example: Group treatments into broader categories
final_shannon$Treatment_grouped <- ifelse(startsWith(final_shannon$Treatment, "OP"), "OP",
ifelse(startsWith(final_shannon$Treatment, "RP"), "RP",
ifelse(startsWith(final_shannon$Treatment, "TP"), "TP",
ifelse(startsWith(final_shannon$Treatment, "IS"), "IS",
ifelse(startsWith(final_shannon$Treatment, "PS"), "PS", "Other")))))
# Fit the model with grouped Treatment
model2 <- lmer(Shannon ~ Treatment_grouped, data = final_shannon, family = Gamma(link = "log"))
summary(model2)
```
```{r}
model <- glmer(Shannon ~ soil_sal + biomass_above + plant_dens + leaf_N + (1 | Treatment), data = final_shannon)
summary(model)
run a PCA on all the variables -
```
```{r}
final_shannon$soil_C <- as.numeric(as.character(final_shannon$soil_C))
final_shannon$soil_N <- as.numeric(as.character(final_shannon$soil_N))
final_shannon$soil_P <- as.numeric(as.character(final_shannon$soil_P))
final_shannon$soil_sal <- as.numeric(as.character(final_shannon$soil_sal))
final_shannon$soil_wat <- as.numeric(as.character(final_shannon$soil_wat))
final_shannon$biomass_above <- as.numeric(as.character(final_shannon$biomass_above))
final_shannon$plant_dens <- as.numeric(as.character(final_shannon$plant_dens))
final_shannon$leaf_N <- as.numeric(as.character(final_shannon$leaf_N))
final_shannon$leaf_C <- as.numeric(as.character(final_shannon$leaf_C))
final_shannon$leaf_P <- as.numeric(as.character(final_shannon$leaf_P))
```
```{r}
is.na(final_shannon$soil_C)
is.na(final_shannon$soil_N)
is.na(final_shannon$soil_P)
is.na(final_shannon$soil_sal)
is.na(final_shannon$soil_wat)
is.na(final_shannon$biomass_above)
is.na(final_shannon$plant_dens)
is.na(final_shannon$leaf_N)
is.na(final_shannon$leaf_C)
is.na(final_shannon$leaf_P)
```
```{r}
noNA_final_shannon <- filter(final_shannon, !is.na(final_shannon$leaf_N))
```
```{r}
mean(noNA_final_shannon$leaf_N)
```
```{r}
read.csv("final_shannon_noNA.csv") -> final_shannon_UPDATED
```
```{r}
par(mfrow=c(1,1))
# can look at all variables together
# can see that all morphological traits are highly linearly and positively correlated
# ie species with large masses ALSO have high wing length and high tarsus length (lower leg bone, the bone below what looks like a backwards facing knee)
pairs(final_shannon_UPDATED[, c("soil_C", "soil_N", "soil_P", "soil_pH",
"soil_sal", "soil_wat", "biomass_above",
"plant_dens", "leaf_N", "leaf_C", "leaf_P")])
```
```{r}
pca.out <- prcomp(final_shannon_UPDATED[, c("soil_C", "soil_N", "soil_P", "soil_pH",
"soil_sal", "soil_wat", "biomass_above",
"plant_dens", "leaf_N", "leaf_C", "leaf_P")], scale.=T)
```
```{r}
pca.out
```
```{r}
summary(pca.out)
```
```{r}
head(pca.out$rotation)
```
```{r}
#soil_n, water, ph, c, aboveground biomass
#
```
```{r}
```
```{r}
autoplot(pca.out, x=1,y=2,
data=final_shannon_UPDATED,
colour="Treatment_grouped",
frame=TRUE) +
theme_classic()
```
```{r}
cor_matrix <- cor(final_shannon[, c("soil_C", "soil_N", "soil_P", "soil_pH",
"soil_sal", "soil_wat", "biomass_above",
"plant_dens", "leaf_N", "leaf_C", "leaf_P")],
use = "pairwise.complete.obs")
print(cor_matrix)
```