-
Notifications
You must be signed in to change notification settings - Fork 0
/
bleaching_analysis.R
290 lines (206 loc) · 8.02 KB
/
bleaching_analysis.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
##############################################################################################################
## CAP 6737 - Final Project - Coral Data Visualization
## Name: Luis A. Rivera Morales
##############################################################################################################
# set working dir
setwd("C:/Users/groot/Desktop/gitluis/coralviz")
## MISSING PACKAGES
## ----------------
## Use install.packages("pkg_name") to install any missing packages.
## CORAL DATA ANALYSIS
## -------------------
## The idea is to obtain a coral dataset containing coral information such as location, coral bleaching, or
## coral diseases with enough different corals and/or metrics to generate multiple visualizations of the
## data set for further analysis. A dashboard containing different graphs showing the information in
## different forms which could even lead to detecting any similarities in between the coral samples.
# read Coral dataset
ds = read.csv("data/CoralBleachingRaw.csv")
ds.raw = ds
# separate numerical variables from categorical variables
ds.numeric = ds[ , sapply(ds, is.numeric)]
ds.categorical = ds[ , sapply(ds, is.factor)]
## -----------------------------------------------------------------------------------------------------------
## Data Analysis
## -----------------------------------------------------------------------------------------------------------
#install.packages("ggplot2")
require(ggplot2, quietly=TRUE, warn.conflicts=FALSE)
# get_column_index: Retrieves column index of a data frame
get_column_index = function(column, data){
return( grep(column, colnames(data))[1] )
}
# Histogram & Bar chart functions
histogram = function(i){
qplot(ds.numeric[,i], geom="histogram", bins=50, main=names(ds.numeric)[i], xlab=names(ds.numeric)[i])
}
barchart = function(i){
qplot(ds.categorical[,i], geom="bar", main=names(ds.categorical)[i], xlab=names(ds.categorical)[i])
}
# check structure & analyze variables
str(ds)
# plot histograms of numerical variables
lapply(1:3, histogram)
lapply(4:6, histogram)
lapply(7:9, histogram)
# plot bar charts of categorical variables
barchart(get_column_index("region", ds.categorical))
barchart(get_column_index("subregion", ds.categorical))
barchart(get_column_index("bleaching_severity", ds.categorical))
# mortality is numeric in reality, need to replace with numbers
barchart(get_column_index("mortality", ds.categorical))
## -----------------------------------------------------------------------------------------------------------
## Numerical/Categorical Variables Recoding for Coral Bleaching
## -----------------------------------------------------------------------------------------------------------
require(stringr, quietly=TRUE, warn.conflicts=FALSE)
# lowercase bleaching severity levels & show graph
levels(ds$bleaching_severity) = c("High", "Low", "Medium", "No Bleaching", "Unknown")
i = get_column_index("bleaching_severity", ds)
qplot(ds[,i], geom="bar", main=names(ds)[i], xlab=names(ds)[i])
rm(i)
# replace empty levels with no data (NA)
levels(ds$region)[1] = "NA"
levels(ds$subregion)[1] = "NA"
# replace -1 with NAs (o mortality level)
mcode = ds$mortality_code
mlevel = ds$mortality
mrows = which(ds$mortality_code == -1)
mcode[mrows] = NA
mlevel[mrows] = NA
# re-code NAs to Unknown
mrows = which(is.na(mcode))
mcode[mrows] = "Unknown"
# save and convert to categorical
mcode = as.factor(mcode)
summary(mcode)
# re-code levels
levels(mcode) = c("No Mortality", "Low", "Medium", "High", "Unknown")
# save data
ds$mortality_code = mcode
ds$mortality = mlevel
# re-code coral families
# ----------------------
str(ds$coral_family)
families = ds$coral_family
comma_in_string = function(x){
return( grepl(",", x) )
}
word_and_in_string = function(x){
return( grepl("and", x) )
}
# retrieve which families can be converted
rows = comma_in_string( levels(families) )
levels(families)[rows] = "Mixed"
rows = word_and_in_string( levels(families) )
levels(families)[rows] = "Mixed"
# save variable
ds$coral_family = families
# show graph
ds.categorical = ds[ , sapply(ds, is.factor)]
barchart(get_column_index("coral_family", ds.categorical))
# regex for float or int numbers only
p_num = "\\d+\\.*\\d*"
# verify coral mortality level (%)
# --------------------------------
# 92% of 'mortalitly' levels is missing data
length(which(ds$mortality == ""))
length(which(ds$mortality == "")) / nrow(ds) * 100
# take sample (non-NAs)
m = ds$mortality
sample_rows = which(m != "")
# extract numbers from strings
sample = m[sample_rows]
sample = str_extract(sample, p_num)
# convert to numeric
sample = as.numeric(format(round(as.numeric(sample), 2), nsmall=2))
# prepare cleaned data
m = rep(NA, nrow(ds))
m[sample_rows] = sample
# conver any mortality % over 100 to NA
m[which(m > 100)] = NA
# check stats
summary(m)
nrow(ds) - 5857 == length(sample_rows)
# save
ds$mortality = m
# re-calculate coral mortality level proportions
length(which(is.na(m)))
length(which(is.na(m))) / nrow(ds) * 100
# so we ended up with 94% of 'mortality' levels missing
# and 6% of data being good...
# verify coral affected by bleaching (%)
# --------------------------------------
# 74% of 'percentage_affected' is missing data
length(which(ds$percentage_affected == ""))
length(which(ds$percentage_affected == "")) / nrow(ds) * 100
# take sample (non-NAs)
perc_affected = ds$percentage_affected
sample_rows = which(perc_affected != "")
# extract numbers from strings
sample = perc_affected[sample_rows]
sample = str_extract(sample, p_num)
# convert to numeric
sample = as.numeric(format(round(as.numeric(sample), 2), nsmall=2))
# prepare cleaned data
perc_affected = rep(NA, nrow(ds))
perc_affected[sample_rows] = sample
# check stats
summary(perc_affected)
nrow(ds) - 4591 == length(sample_rows)
# save
ds$percentage_affected = perc_affected
# how much data from mortality and percentage affected
# variables can we use in reality for graphs?
m_rows = which(!is.na(m))
tmp_data = cbind(mortality=m[m_rows], percentage_affected=perc_affected[m_rows])
nrow(na.omit(tmp_data))
# view data
View(na.omit(tmp_data))
# Answer: There are 103 observations that we can use.
# verify sea water temperature for observations
# ---------------------------------------------
# 83% of 'water_temperature' is missing data
length(which(ds$water_temperature == ""))
length(which(ds$water_temperature == "")) / nrow(ds) * 100
# take sample (non-NAs)
water_temp = ds$water_temperature
sample_rows = which(water_temp != "")
# extract numbers from strings
sample = water_temp[sample_rows]
sample = str_extract(sample, p_num)
# convert to numeric
sample = as.numeric(format(round(as.numeric(sample), 2), nsmall=2))
# Function: Converts emperature from Fahrenheit to Celsius
F_to_C = function(f){
temp = (f - 32) / 1.8
temp = as.numeric(format(round(temp, 2), nsmall=2))
return(temp)
}
# any temp > 90 --- set to 90 F
sample[sample > 90] = 90
summary(sample)
# any temp > 50 --- to Celsius
over_50_rows = sample > 50
sample[over_50_rows] = sapply(sample[over_50_rows], F_to_C)
sample[sample > 32.22] = 32.22
summary(sample)
# prepare cleaned data
water_temp = rep(NA, nrow(ds))
water_temp[sample_rows] = sample
# check stats
summary(water_temp)
nrow(ds) - 5124 == length(sample_rows)
# save
ds$water_temperature = water_temp
# drop any variables that will not be used
ds = ds[, -which(colnames(ds) == "month")]
ds = ds[, -which(colnames(ds) == "bleaching_duration")]
ds = ds[, -which(colnames(ds) == "recovery_code")]
ds = ds[, -which(colnames(ds) == "reference_code")]
ds = ds[, -which(colnames(ds) == "remarks")]
ds = ds[, -which(colnames(ds) == "other_factors")]
ds = ds[, -which(colnames(ds) == "survey_area")]
ds = ds[, -which(colnames(ds) == "survey_type")]
ds = ds[, -which(colnames(ds) == "recovery")]
## -----------------------------------------------------------------------------------------------------------
# Export prepared dataset(s)
## -----------------------------------------------------------------------------------------------------------
write.csv(ds, file="data/CoralBleaching.csv", na="", row.names=FALSE, col.names=TRUE)