Skip to content

Instantly share code, notes, and snippets.

@darmitage
Created September 18, 2012 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darmitage/3744607 to your computer and use it in GitHub Desktop.
Save darmitage/3744607 to your computer and use it in GitHub Desktop.
Classification of zero-crossings bat calls using machine learning
require(kernlab)
require(caret)
require(e1071)
require(nnet)
require(randomForest)
set.seed(34873458)
#Open call library (needs name (eg. MYLU), and parameters)
setwd = ("Dropbox/Anabat files/Dave's Florida Bat Calls/")
callsamplesALLNNET=read.csv(file="Dropbox/Anabat files/FloridaBatsALL.csv",header=T,sep=",")
callsamplesALLNNET[,-1]=scale(callsamplesALLNNET[,-1], center = T, scale = T)
fitControl <- trainControl(method = "cv", number = 10, returnResamp = "all", verboseIter = FALSE)
#train ANN on voucher calls
nnet1 <- train(Name ~., data = callsamplesALLNNET, method = "nnet", tuneLength = 10,
trControl = fitControl)
nnet1
#open table of unknown calls (Filename = analook file with date and time) parameters must be identical to voucher calls above.
#test calls here are output of Analook scan of particular night's folder
testNNET = read.csv("Dropbox/Anabat files/test.csv", head = T, sep = ",")
testNNET$facName = factor(testNNET$Filename)
levels(testNNET$facName) = 1:1000
samp = testNNET$facName
testNNET[,2:12] = scale(testNNET[,2:12], center = TRUE, scale = TRUE)
#predict identity of unknown calls using NNET
nnetpred <- predict(nnet1, newdata = testNNET[,2:12])
species = data.frame(nnetpred)
res = cbind(samp, species)
#return a consensus (i.e. mode) of call assignments for each sequence
df.list <-vector("list",max(as.numeric(res$samp)))
for (i in 1:max(as.numeric(res$samp))) {
set = subset(res, samp==i)
consensus = names(sort(-table(set$nnetpred)))[1]
df.list[[i]] = consensus }
print(df.list)
results = cbind(levels(testNNET[,1]),as.matrix(df.list))
colnames(results) = c("sequence", "consensus ID")
write.csv(results, file = "Dropbox/Anabat files/results.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment