forked from mttinker/SOFA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOFA_format_MBA_data.R
235 lines (186 loc) · 6.81 KB
/
SOFA_format_MBA_data.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
#Joshua G. Smith
#jossmith@mbayaq.org
#Script initiated March 29, 2023
rm(list=ls())
#load packages
librarian::shelf(tidyverse, readxl, here, janitor)
#set directories
datin <- "/Volumes/seaotterdb$/kelp_recovery/data/foraging_data/raw"
datout <- "/Volumes/seaotterdb$/kelp_recovery/data/foraging_data/processed/SOFA_explore"
#get metadata
meta_raw <- read_xlsx(file.path(datin, "ForageKey_lookup_table.xlsx"))
#get foraging data
for_dat <- read_xlsx(file.path(datin, "Forage_data_2016tocurrent.xlsx"))
dives <- read_xlsx(file.path(datin, "Forage_dives_2016tocurrent.xlsx"))
index <- read_xlsx(file.path(datin, "Forage_indexi_2016tocurrent.xlsx"))
################################################################################
#step 1 - tidy data
meta_build1 <- meta_raw %>%
janitor::clean_names() %>%
#Sentence case
mutate(category_code = ifelse(data_type == "Prey_vars",
str_to_sentence(category_code), category_code),
category_long = str_to_sentence(category_long),
description = str_to_sentence(description))
################################################################################
################################################################################
#step 2 - process foraging data --- clean up names and check
for_dat_build1 <- for_dat %>% janitor::clean_names() %>%
#correct inconsistencies
mutate(qualifier = factor(tolower(qualifier)),
pup_behav = factor(tolower(pup_behav)),
mom_resp = factor(tolower(mom_resp)),
outcome = factor(tolower(outcome)),
prey = factor(prey),
tooltype = factor(tooltype),
pupsh = factor(pupsh),
pup_behav = factor(pup_behav),
stolenfr = factor(stolenfr),
steal = factor(steal),
sizecm = factor(sizecm)
)
str(for_dat_build1)
#check for inconsistencies
unique(for_dat_build1$preynum)
unique(for_dat_build1$prey)
unique(for_dat_build1$number)
unique(for_dat_build1$size)
unique(for_dat_build1$qualifier) #make lower
unique(for_dat_build1$ht)
unique(for_dat_build1$tooltype)
unique(for_dat_build1$tool_number)
unique(for_dat_build1$pupsh)
unique(for_dat_build1$pupshare_number)
unique(for_dat_build1$pup_behav) #make lower
unique(for_dat_build1$mom_resp) #make lower
unique(for_dat_build1$outcome) #make lower
unique(for_dat_build1$stolenfr)
unique(for_dat_build1$stolenfr_number)
unique(for_dat_build1$steal)
unique(for_dat_build1$steal_number)
unique(for_dat_build1$sizecm)
unique(for_dat_build1$gulls)
################################################################################
#process dives
dive_build1 <- dives %>% janitor::clean_names() %>%
#correct inconsistencies
mutate(where = tolower(where),
#fix incorrect sign
long_obs_deg = ifelse(long_obs_deg == 121,-121,long_obs_deg),
#fix kelp type
kelptype = ifelse(kelptype == "xx","x",kelptype),
kelptype = factor(tolower(kelptype)),
success = factor(tolower(success)),
sb_area = factor(toupper(sb_area))
)
colnames(dive_build1)
str(dive_build1)
#check for inconsistencies
unique(dive_build1$subbout)
unique(dive_build1$where)
unique(dive_build1$lat_obs_deg)
unique(dive_build1$long_obs_deg)
unique(dive_build1$long_obs_min)
unique(dive_build1$lat_obs)
unique(dive_build1$long_obs)
unique(dive_build1$long)
unique(dive_build1$shore)
unique(dive_build1$depth)
unique(dive_build1$canopy)
unique(dive_build1$kelptype)
unique(dive_build1$divenum)
unique(dive_build1$dt)
unique(dive_build1$st)
unique(dive_build1$success)
unique(dive_build1$sb_area)
################################################################################
#process index
index_build1 <- index %>% janitor::clean_names() %>%
mutate(visib = factor(tolower(visib)),
sex = factor(sex),
ageclass = factor(ageclass),
status = factor(status),
pupsz = factor(pupsz),
consort = factor(consort),
obsbeg = factor(obsbeg),
obsend = factor(obsend),
daynight = factor(daynight),
area = factor(area),
sky = factor(sky),
winddir = factor(winddir),
seaopen = factor(seaopen),
seafix = factor(seafix),
swell = factor(swell)
)
str(index_build1)
colnames(index_build1)
unique(index_build1$sex)
unique(index_build1$ageclass)
unique(index_build1$status)
unique(index_build1$pupsz)
unique(index_build1$ageweeks)
unique(index_build1$agequal)
unique(index_build1$consort)
unique(index_build1$gen_locat)
unique(index_build1$atos)
unique(index_build1$area)
unique(index_build1$fixtype)
unique(index_build1$sky)
unique(index_build1$rain)
unique(index_build1$windspeed)
unique(index_build1$winddir)
unique(index_build1$temperature)
unique(index_build1$seaopen)
unique(index_build1$seafix)
unique(index_build1$swell)
unique(index_build1$visib)
################################################################################
#merge data
forage_join <- for_dat_build1 %>%
#filter(steal == "No")%>%
#select variables of interest
# dplyr::select(foragdiv_id, foragdata_id, preynum, prey, number,
# size, qualifier)%>%
mutate_if(is.character, str_trim)
dive_join <- dive_build1 %>%
# dplyr::select(foragdiv_id, bout, subbout, lat, long, canopy,
# kelptype, divenum, dt, success) %>%
mutate_if(is.character, str_trim)
#filter successful dives only
#filter(success == "y")
index_join <- index_build1 %>%
# dplyr::select(bout, date, otterno, sex, ageclass, status)%>%
mutate_if(is.character, str_trim)
#merge forage data and bout info
data_build1 <- left_join(dive_join, forage_join, by="foragdiv_id")
anti_build <- anti_join(dive_join, forage_join, by="foragdiv_id")
#merge with index
data_build2 <- left_join(data_build1, index_join, by="bout")
anti_build2 <- anti_join(data_build1, index_join, by="bout")
################################################################################
#format for SOFA
data_build3 <- data_build2 %>%
#set user-defined fields
mutate(Region = "CALI",
#Area = "MONT",
Site = "MONT",
Period = "2016-2020",
Date = format(date, "%m/%d/%Y"),
Season = case_when(
month(date) %in% 5:10 ~ "summer",
TRUE ~ "winter"
),
Prop_Lost = 0, #set 0 for now, but requires formuls
How_Lost = NA, #requires mod ... create new column "discarded" and if proportion lost > 0, Or maybe create new dummy var
Est_kg = NA
)%>%
#THESE FIELDS ARE REQUIRED FOR SOFA TO RUN
dplyr::select(Region, Area = area, Site, Period, Date, Season, Sex = sex, AgeClass = ageclass,
Pup = status, Ottername = otterno, Bout = bout, Subbout = subbout,
TimeStart = timestart.y, TimeEnd = timeend.y, Divenum = divenum,
DT = dt, ST = st, Success = success, Prey = prey, N_Items = preynum,
Size = size, Qualifier = qualifier, HT = ht, Prop_Lost,
How_Lost, Est_kg, Est_cm = sizecm
)
xlsx_file <- file.path(datout, "SOFA_explore.xlsx")
write.xlsx(data_build3, xlsx_file, rowNames = FALSE)