-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCGmodel_EVJ_06232016.R
353 lines (304 loc) · 20.3 KB
/
BCGmodel_EVJ_06232016.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
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
library(plyr)
library(dplyr)
sampleList <- read.csv('C:/HardDriveBackup/R/BCG/FiguringItOut/taxaLists/sampleList.csv')%>%
select(-X)%>%
plyr::rename(c('BCGAttribute'='attLevel'))
#----------------------------------Taxa metric functions---------------------------------------------------------
# Number of Total Taxa
totTax <- function(sample){nrow(unique(sample))}
# Number of Total Individuals (for later calculations)
totInd <- function(sample){sum(sample$Count)}
# Number of Attribute I&II Taxa
T_12 <- function(sample){sum(sample$attLevel %in% c(1,2))}
# % Attribute 1,2,&3 Taxa
P_123 <- function(sample){(sum(sample$attLevel %in% c(1,2,3))/totTax(sample))*100}
# % Attribute 1,2,&3 Individuals
PI_123 <- function(sample){
df <- filter(sample,attLevel %in% c(1,2,3))
(sum(df$Count)/totInd(sample))*100}
# % Atribute 5 Individuals
PI_5 <- function(sample){
df <- filter(sample,attLevel %in% c(5))
(sum(df$Count)/totInd(sample))*100}
# % Atribute 5&6t Individuals
PI_56t <- function(sample){
df <- filter(sample,attLevel %in% c('5','6t'))
(sum(df$Count)/totInd(sample))*100}
# Number of Attribute 6 Taxa
T_6 <- function(sample){sum(sample$attLevel %in% c('6i','6m','6t'))}
# Number of Attribute 6t Taxa
T_6t <- function(sample){sum(sample$attLevel %in% c('6t'))}
# Number of Darter Taxa
T_darter <- function(sample){sum(filter(sample, grepl('darter',CommonName))$Count)}
# Number of native Brook Trout individuals
N_bTrout <- function(sample){sum(filter(sample, grepl('brook trout',CommonName))$Count)}
# % Most Dominant Attribute 5&6t individuals
P_Dom56t <- function(sample){(max(filter(sample,attLevel %in% c('5','6t'))$Count)/totInd(sample))*100}
# Number of Attribute 1,2,&3 taxa
T_123 <- function(sample){sum(sample$attLevel %in% c(1,2,3))}
# % Attribute 5&6 taxa
P_56 <- function(sample){(sum(sample$attLevel %in% c('5','6i','6m','6t'))/totTax(sample))*100}
## ---------------------------------------------------------------------------------------------------------
# Master metric that outputs dataframe of all results
masterMetric <- function(sampleName,sample){
data.frame(SampleName=sampleName,totTax=totTax(sample),T_12=T_12(sample),P_123=P_123(sample)
,PI_123=PI_123(sample),PI_5=PI_5(sample),PI_56t=PI_56t(sample),T_6=T_6(sample)
,T_6t=T_6t(sample),T_darter=T_darter(sample),N_bTrout=N_bTrout(sample),P_Dom56t=P_Dom56t(sample)
,T_123=T_123(sample),P_56=P_56(sample))}
# BCG Fuzzy Membership logic
fuzzyMembership <- function(metric,low,high){metric/(high-low)-low/(high-low)}
fmFinal <- function(x){
if(x>=1){return(1)}else(y=x)
if(y<=0){return(0)}else(return(x))
}
## ---------------------------- Level Rules ---------------------------------------------------------------------
# BCG level logic, Other Medium/Large
otherMedLarge_BCGlevel2 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,10,20)),fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1))
,fm_P_123=fmFinal(fuzzyMembership(test$P_123,5,15)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25))
,fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,55,65)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,3)))}
otherMedLarge_BCGlevel3 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,9,19)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,4,10))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,3,7)),fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,65,75))
,fm_P_Dom56t=fmFinal(1-fuzzyMembership(test$P_Dom56t,25,35)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,5)))}
otherMedLarge_BCGlevel4alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,7,15)),fm_T_123=fmFinal(fuzzyMembership(test$T_123,0,1))
,fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,75,85)))}
otherMedLarge_BCGlevel4alt2 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,16,26)),fm_P_Dom56t=fmFinal(1-fuzzyMembership(test$P_Dom56t,25,35))
,fm_T_darter=fmFinal(fuzzyMembership(test$T_darter,2,5)))}
otherMedLarge_BCGlevel5 <- function(test){data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,3,6)))}
# BCG level logic, Other Small
otherSmall_BCGlevel2alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,6,16)),fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1))
,fm_P_123=fmFinal(fuzzyMembership(test$P_123,10,20)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25))
,fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,55,65)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,3))
,fm_T_darter=fmFinal(fuzzyMembership(test$T_darter,2,5)))}
otherSmall_BCGlevel2alt2 <- function(test){
data.frame(fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,10,20))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,3))
,fm_T_darter=fmFinal(fuzzyMembership(test$T_darter,2,5)),fm_N_bTrout=fmFinal(fuzzyMembership(test$N_bTrout,0,3)))}
otherSmall_BCGlevel3alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,5,13)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,4,10))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,5,15)),fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,65,75))
,fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,5)),fm_T_darter=fmFinal(fuzzyMembership(test$T_darter,1,4)))}
otherSmall_BCGlevel3alt2 <- function(test){
data.frame(fm_P_123=fmFinal(fuzzyMembership(test$P_123,4,10)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,5,15))
,fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,1,5)),fm_N_bTrout=fmFinal(fuzzyMembership(test$N_bTrout,0,3)))}
otherSmall_BCGlevel4alt1 <- function(test){
data.frame(fm_T_123=fmFinal(fuzzyMembership(test$T_123,0,1)),fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,80,90)))}
otherSmall_BCGlevel4alt2 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,13,23)),fm_P_Dom56t=fmFinal(1-fuzzyMembership(test$P_Dom56t,25,35))
,fm_T_darter=fmFinal(fuzzyMembership(test$T_darter,1,4)))}
otherSmall_BCGlevel5 <- function(test){data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,3,6)))}
# BCG level logic, Above Falls Medium/Large
aboveFallsMedLarge_BCGlevel2 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,5,15)),fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1))
,fm_P_123=fmFinal(fuzzyMembership(test$P_123,20,30)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,25,35))
,fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,30,40)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,2,5))
,fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,0,1)))}
aboveFallsMedLarge_BCGlevel3 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,4,14)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,15,25))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25)),fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,35,45))
,fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,1,4)))}
aboveFallsMedLarge_BCGlevel4alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,6,10)),fm_T_123=fmFinal(fuzzyMembership(test$T_123,1,2))
,fm_P_123=fmFinal(fuzzyMembership(test$P_123,3,7)),fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,65,75)))}
aboveFallsMedLarge_BCGlevel4alt2 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,6,10)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,3,7))
,fm_P_56=fmFinal(1-fuzzyMembership(test$P_56,60,70)),fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,65,75)))}
aboveFallsMedLarge_BCGlevel5 <- function(test){data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,2,5)))}
# BCG level logic, starting at Above Falls Small
aboveFallsSmall_BCGlevel2alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,8,12)),fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1))
,fm_P_123=fmFinal(fuzzyMembership(test$P_123,20,30)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,25,35))
,fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,30,40)),fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,2,5))
,fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,0,1)))}
aboveFallsSmall_BCGlevel2alt2 <- function(test){
data.frame(fm_T_12=fmFinal(fuzzyMembership(test$T_12,0,1)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,20,30))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,25,35)),fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,30,40))
,fm_T_6=fmFinal(1-fuzzyMembership(test$T_6,2,5)),fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,0,1))
,fm_N_bTrout=fmFinal(fuzzyMembership(test$N_bTrout,0,3)))}
aboveFallsSmall_BCGlevel3alt1 <- function(test){
data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,4,11)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,5,15))
,fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25)),fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,50,60))
,fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,1,4)))}
aboveFallsSmall_BCGlevel3alt2 <- function(test){
data.frame(fm_P_123=fmFinal(fuzzyMembership(test$P_123,5,15)),fm_PI_123=fmFinal(fuzzyMembership(test$PI_123,15,25))
,fm_PI_5=fmFinal(1-fuzzyMembership(test$PI_5,50,60)),fm_T_6t=fmFinal(1-fuzzyMembership(test$T_6t,1,4))
,fm_N_bTrout=fmFinal(fuzzyMembership(test$N_bTrout,0,3)))}
aboveFallsSmall_BCGlevel4alt1 <- function(test){
data.frame(fm_T_123=fmFinal(fuzzyMembership(test$T_123,1,2)),fm_P_123=fmFinal(fuzzyMembership(test$P_123,3,7))
,fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,70,80)))}
aboveFallsSmall_BCGlevel4alt2 <- function(test){
data.frame(fm_P_123=fmFinal(fuzzyMembership(test$P_123,3,7)),fm_P_56=fmFinal(1-fuzzyMembership(test$P_56,60,70))
,fm_PI_56t=fmFinal(1-fuzzyMembership(test$PI_56t,70,80)))}
aboveFallsSmall_BCGlevel5 <- function(test){data.frame(fm_totTax=fmFinal(fuzzyMembership(test$totTax,2,5)))}
### Other Medium/Large BCG Model
OtherMedLargeModel <- function(sampleName,taxaListFromOneSite){
metricResults <- masterMetric(sampleName,taxaListFromOneSite)
levelresult <- data.frame(SampleName=metricResults[1]
,BCGlevel5=min(otherMedLarge_BCGlevel5(metricResults))
,BCGlevel4=max(min(otherMedLarge_BCGlevel4alt1(metricResults)),min(otherMedLarge_BCGlevel4alt2(metricResults)))
,BCGlevel3=min(otherMedLarge_BCGlevel3(metricResults))
,BCGlevel2=min(otherMedLarge_BCGlevel2(metricResults)))%>%
mutate(Level1=0,Level2=min(1-Level1,BCGlevel2),Level3=min(1-sum(Level1,Level2),BCGlevel3)
,Level4=min(1-sum(Level1,Level2,Level3),BCGlevel4)
,Level5=min(1-sum(Level1,Level2,Level3,Level4),BCGlevel5)
,Level6=1-sum(Level1,Level2,Level3,Level4,Level5))
placehold<-sort(levelresult[6:11],TRUE)[2] # pick second highest BCG level, can't be done in mutate statement
final <- data.frame(SampleName=metricResults[1]
,nominalTier=colnames(levelresult[,6:11])[apply(levelresult[6:11],1,which.max)]
,nominalMembership=apply(levelresult[,6:11],1,max)
,secondMembership=placehold[1,]
,runnerupTier=ifelse(placehold[1,]==0,"",colnames(placehold)))%>%
mutate(close=ifelse(nominalMembership-secondMembership<0.1,"tie"
,ifelse(nominalMembership-secondMembership<0.2,"yes","")))
return(final)
}
### Other Small BCG Model
OtherSmallModel <- function(sampleName,taxaListFromOneSite){
metricResults <- masterMetric(sampleName,taxaListFromOneSite)
levelresult <- data.frame(SampleName=metricResults[1]
,BCGlevel5=min(otherSmall_BCGlevel5(metricResults))
,BCGlevel4=max(min(otherSmall_BCGlevel4alt1(metricResults)),min(otherSmall_BCGlevel4alt2(metricResults)))
,BCGlevel3=max(min(otherSmall_BCGlevel3alt1(metricResults)),min(otherSmall_BCGlevel3alt2(metricResults)))
,BCGlevel2=max(min(otherSmall_BCGlevel2alt1(metricResults)),min(otherSmall_BCGlevel2alt2(metricResults))))%>%
mutate(Level1=0,Level2=min(1-Level1,BCGlevel2),Level3=min(1-sum(Level1,Level2),BCGlevel3)
,Level4=min(1-sum(Level1,Level2,Level3),BCGlevel4)
,Level5=min(1-sum(Level1,Level2,Level3,Level4),BCGlevel5)
,Level6=1-sum(Level1,Level2,Level3,Level4,Level5))
placehold<-sort(levelresult[6:11],TRUE)[2] # pick second highest BCG level, can't be done in mutate statement
final <- data.frame(SampleName=metricResults[1]
,nominalTier=colnames(levelresult[,6:11])[apply(levelresult[6:11],1,which.max)]
,nominalMembership=apply(levelresult[,6:11],1,max)
,secondMembership=placehold[1,]
,runnerupTier=ifelse(placehold[1,]==0,"",colnames(placehold)))%>%
mutate(close=ifelse(nominalMembership-secondMembership<0.1,"tie"
,ifelse(nominalMembership-secondMembership<0.2,"yes","")))
return(final)
}
### AboveFalls Medium/Large BCG Model
AboveFallsMedLargeModel <- function(sampleName,taxaListFromOneSite){
metricResults <- masterMetric(sampleName,taxaListFromOneSite)
levelresult <- data.frame(SampleName=metricResults[1]
,BCGlevel5=min(aboveFallsMedLarge_BCGlevel5(metricResults))
,BCGlevel4=max(min(aboveFallsMedLarge_BCGlevel4alt1(metricResults)),min(aboveFallsMedLarge_BCGlevel4alt2(metricResults)))
,BCGlevel3=min(aboveFallsMedLarge_BCGlevel3(metricResults))
,BCGlevel2=min(aboveFallsMedLarge_BCGlevel2(metricResults)))%>%
mutate(Level1=0,Level2=min(1-Level1,BCGlevel2),Level3=min(1-sum(Level1,Level2),BCGlevel3)
,Level4=min(1-sum(Level1,Level2,Level3),BCGlevel4)
,Level5=min(1-sum(Level1,Level2,Level3,Level4),BCGlevel5)
,Level6=1-sum(Level1,Level2,Level3,Level4,Level5))
placehold<-sort(levelresult[6:11],TRUE)[2] # pick second highest BCG level, can't be done in mutate statement
final <- data.frame(SampleName=metricResults[1]
,nominalTier=colnames(levelresult[,6:11])[apply(levelresult[6:11],1,which.max)]
,nominalMembership=apply(levelresult[,6:11],1,max)
,secondMembership=placehold[1,]
,runnerupTier=ifelse(placehold[1,]==0,"",colnames(placehold)))%>%
mutate(close=ifelse(nominalMembership-secondMembership<0.1,"tie"
,ifelse(nominalMembership-secondMembership<0.2,"yes","")))
return(final)
}
### Above Falls Small BCG Model
AboveFallsSmallModel <- function(sampleName,taxaListFromOneSite){
metricResults <- masterMetric(sampleName,taxaListFromOneSite)
levelresult <- data.frame(SampleName=metricResults[1]
,BCGlevel5=min(aboveFallsSmall_BCGlevel5(metricResults))
,BCGlevel4=max(min(aboveFallsSmall_BCGlevel4alt1(metricResults)),min(aboveFallsSmall_BCGlevel4alt2(metricResults)))
,BCGlevel3=max(min(aboveFallsSmall_BCGlevel3alt1(metricResults)),min(aboveFallsSmall_BCGlevel3alt2(metricResults)))
,BCGlevel2=max(min(aboveFallsSmall_BCGlevel2alt1(metricResults)),min(aboveFallsSmall_BCGlevel2alt2(metricResults))))%>%
mutate(Level1=0,Level2=min(1-Level1,BCGlevel2),Level3=min(1-sum(Level1,Level2),BCGlevel3)
,Level4=min(1-sum(Level1,Level2,Level3),BCGlevel4)
,Level5=min(1-sum(Level1,Level2,Level3,Level4),BCGlevel5)
,Level6=1-sum(Level1,Level2,Level3,Level4,Level5))
placehold<-sort(levelresult[6:11],TRUE)[2] # pick second highest BCG level, can't be done in mutate statement
final <- data.frame(SampleName=metricResults[1]
,nominalTier=colnames(levelresult[,6:11])[apply(levelresult[6:11],1,which.max)]
,nominalMembership=apply(levelresult[,6:11],1,max)
,secondMembership=placehold[1,]
,runnerupTier=ifelse(placehold[1,]==0,"",colnames(placehold)))%>%
mutate(close=ifelse(nominalMembership-secondMembership<0.1,"tie"
,ifelse(nominalMembership-secondMembership<0.2,"yes","")))
return(final)
}
#-------------------------------------------------------------------------------------------------------
### Put actual data in
splits <- split(sampleList,sampleList$SampleName,drop=T)%>% #Split dataframe into multiple df's in a list
lapply(function(x) x[!(names(x) %in% c('SampleName'))])
options(digits = 3)
result <- data.frame(matrix(NA, ncol = 7, nrow = 1))
names(result) <- c('SampleName','nominalTier','nominalMembership','secondMembership','runnerupTier'
,'close','Model')
for(i in 1:length(splits)){
print(i)
samplename <- names(splits)[i]
sampleathand <- data.frame(splits[[i]])
if(splits[[i]]$SubBasin[1] %in% c('New','UNew','MNew','Green')){
if(splits[[i]]$Catchment[1] < 10){
result<- rbind(result,cbind(AboveFallsSmallModel(samplename,sampleathand),Model='AboveFalls,Small'))
}else{
result<- rbind(result,cbind(AboveFallsMedLargeModel(samplename,sampleathand),Model='AboveFalls,MediumLarge'))
}
}else{
if(splits[[i]]$Catchment[1] < 10){
result<- rbind(result,cbind(OtherSmallModel(samplename,sampleathand),Model='Other,Small'))
}else{
result<- rbind(result,cbind(OtherMedLargeModel(samplename,sampleathand),Model='Other,MediumLarge'))
}
}
result<-filter(result,!(SampleName=='NA'))
}
# Bring in real results
realresults <- read.csv('C:/HardDriveBackup/R/BCG/FiguringItOut/AboveOtherModelResults.csv')%>%
plyr::rename(c('ExerciseID'='SampleName'))
realresults$SampleName <- as.character(realresults$SampleName)
realresults<- as.data.frame(sapply(realresults, function(x) gsub('Samp0','Sample',x)))
together3 <- join(result,realresults,by='SampleName')
#write.csv(together,'together.csv')
# --------------------------------- DIFFERENT TEST DATASET ------------------------------------
# run Lou's hw1 taxa list through code
library(readxl)
# Step1 in excel, take out all spaces in column names
hw1 <- read_excel('C:/HardDriveBackup/R/BCG/Lou/abovefalls_hw1.xlsx',sheet='Sheet1')%>%
select(c(ExerciseID,Basin,DrArea_mi2,CommonName,ScientificName,Count,BCGAttribute)) %>% #get just the variables I need
plyr::rename(c("ExerciseID"="SampleName","DrArea_mi2"="Catchment"
,"BCGAttribute"="attLevel")) %>% #rename to script variables
mutate(SubBasin=revalue(Basin,c('Upper New'='UNew','Middle New'='MNew','Lower New'='LNew'
,'Greenbrier'='Green')))
#louHW1 <- split(hw1,hw1$SampleName,drop=T)%>% #Split dataframe into multiple df's in a list
# lapply(function(x) x[!(names(x) %in% c('SampleName'))])
testfunction <- function(dfInCorrectFormat){
splits <-split(dfInCorrectFormat,dfInCorrectFormat$SampleName,drop=T)%>% #Split dataframe into multiple df's in a list
lapply(function(x) x[!(names(x) %in% c('SampleName'))])
result <- data.frame(matrix(NA, ncol = 7, nrow = 1))
names(result) <- c('SampleName','nominalTier','nominalMembership','secondMembership','runnerupTier'
,'close','Model')
for(i in 1:length(splits)){
print(i)
samplename <- names(splits)[i]
sampleathand <- data.frame(splits[[i]])
if(splits[[i]]$SubBasin[1] %in% c('New','UNew','MNew','LNew','Green')){
if(splits[[i]]$Catchment[1] < 10){
result<- rbind(result,cbind(AboveFallsSmallModel(samplename,sampleathand),Model='AboveFalls,Small'))
}else{
result<- rbind(result,cbind(AboveFallsMedLargeModel(samplename,sampleathand),Model='AboveFalls,MediumLarge'))
}
}else{
if(splits[[i]]$Catchment[1] < 10){
result<- rbind(result,cbind(OtherSmallModel(samplename,sampleathand),Model='Other,Small'))
}else{
result<- rbind(result,cbind(OtherMedLargeModel(samplename,sampleathand),Model='Other,MediumLarge'))
}
}
result<-filter(result,!(SampleName=='NA'))
}
return(result)
}
result <- testfunction(hw1)
result<- as.data.frame(sapply(result, function(x) gsub('Samp0','Sample',x)))
together <- join(result,realresults,by='SampleName')
# for later playing with making a function to detect column class
hw1<- as.data.frame(sapply(hw1, function(x) gsub('Samp0','Sample',x)))
hw1$SampleName <- as.character(hw1$SampleName)
hw2 <- hw1 %>% mutate_each(funs(type.convert(as.character(.))))