-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMIT_aggregated_med.R
176 lines (107 loc) · 4.08 KB
/
MIT_aggregated_med.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
# Author: Shannon Hubler
# Initial Date: 11.10.21
# Project: Macroinvertebrate Inferred Temperature
# summary: Building on thermal traits work, as part of the Western OR/WA BCG workgroup.
# Develop Weighted Averaging models to infer seasonal 7-d maximum temperatures,
# derived from NorWeSTs MWMT metric.
# AGGREGATED taxonomic resolution models
# packages used
library(tidyverse)
library(rioja)
#####
#####
# bring in taxonomy lookup table -- with OTUs for different models
#####
#####
taxa <- read.csv('Taxa_translator_FINAL.csv')
# OTU = taxa translator to bug data files -- use this for aggregation
# OTU_high.res = highest taxonomic resolution (genus/species as much as possible)
# OTU_med.res = medium taxa resolution--compromises for groups where majority of info is at the next level up from 'high.res'
# OTU_low.res = lowest taxa resolution--many groups moved to tribe/subfamily/family/order (mostly midges, oligochaetes, etc.)
# '666' = used to filter out and drop taxa not included in model,
# modify to make easier to join/link with bug data files
taxa.otu <- taxa %>%
select(TaxaID_v2, OTU)
@@@@ dont need this because Sean included it in the agg data files?
#####
#####
# bring in bug data
#####
#####
# calibration AGG data set, used to build the models (n = 3875)
cal <- read.csv('FINAL_AGG_DATA/CAL_3875_AGG_W_META.csv')
cal <- cal %>%
rename(sample.id = UniqueID_v2)
# independent validation AGG data set, used to get better error estimates of model performance (n = 629)
val <- read.csv('FINAL_AGG_DATA/VAL_629_AGG_W_META.csv')
val <- val %>%
rename(sample.id = UniqueID_v2)
#####
#
# CALIBRATION
#
#####
## calculate total abundance from raw data
tot.abund<-aggregate(cal$Count, list(sample.id=cal$sample.id), sum)
colnames(tot.abund)[colnames(tot.abund)=="x"] <- "tot.abund"
# join back into CAL
cal.agg <- cal %>%
left_join(tot.abund, by = c('sample.id'))
# calculate Relative Abundance
rel.abund<-cal.agg[, c("sample.id", "OTU_med.res", "Count")]
rel.abund <- rel.abund %>%
left_join(tot.abund, by = c('sample.id'))
# calculate RA
rel.abund<-plyr::ddply(.data = rel.abund, c('sample.id', 'OTU_med.res'),
plyr::summarize, rel.abund=Count/tot.abund)
# sum RA's across all OTUs--should see a reduction in rows
rel.abund<-plyr::ddply(.data = rel.abund, c('sample.id', 'OTU_med.res'),
plyr::summarize, rel.abund=sum(rel.abund))
# check for duplicates, across sample.id and OTU
rel.abund[duplicated(rel.abund[,1:2]),]
# filter for MED RES OTUs and remove unwanted taxa
cal.agg_RA <- rel.abund %>%
select(sample.id, OTU_med.res, rel.abund) %>%
filter(OTU_med.res != 666)
# need to crosstab the bug data (turn into a wide format) so that OTUs are columns
# high res
bug.cal.agg_med_wide <- cal.agg_RA %>%
pivot_wider(id_cols = sample.id, names_from = OTU_med.res, values_from = rel.abund,
values_fn = sum)
bug.cal.agg_med_wide[is.na(bug.cal.agg_med_wide)] <- 0
bug.cal.agg_med_wide <- column_to_rownames(bug.cal.agg_med_wide, 'sample.id')
####
# repeat for VAL
####
@@@@@@
@@@@@@@@@
@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@
#####
#####
# create ENV data frame (MWMT)
#####
#####
env <- read.csv('CAL_3875_JS.csv')
env.cal <- env %>%
select(UniqueID_v2, MWMT_final) %>%
rename(sample.id = UniqueID_v2) %>%
distinct_all()
env.cal <- column_to_rownames(env.cal, 'sample.id')
####
# repeat for VAL
####
#####
#####
# Create inference models with CAL data
#####
#####
# high res
spec <- bug.cal.agg_med_wide
env <- env.cal
wa_med.res <- WA(y=spec, x=env, mono=TRUE, tolDW = TRUE, use.N2=TRUE, tol.cut=.01,
check.data=TRUE, lean=FALSE)
wa_med.res # 280 taxa, RMSE (inv/cla) = 2.3/2.7, r2 = 0.682 (both), max bias 6.2/3.2
crossval(wa_med.res, cv.method="loo", verbose=TRUE, ngroups=10,
nboot=100, h.cutoff=0, h.dist=NULL)
# crossval results almost exactly the same as original