Packages

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(umap)
library(seewave)
library(tuneR)
library(phonTools)
## 
## Attaching package: 'phonTools'
## The following objects are masked from 'package:tuneR':
## 
##     normalize, play
## The following object is masked from 'package:seewave':
## 
##     preemphasis
## The following object is masked from 'package:dplyr':
## 
##     slice
library(signal)
## 
## Attaching package: 'signal'
## The following object is masked from 'package:phonTools':
## 
##     resample
## The following object is masked from 'package:seewave':
## 
##     unwrap
## The following object is masked from 'package:dplyr':
## 
##     filter
## The following objects are masked from 'package:stats':
## 
##     filter, poly
library(warbleR)
## Loading required package: NatureSounds
## Loading required package: knitr
library(voice)
## 
## Attaching package: 'voice'
## The following object is masked from 'package:signal':
## 
##     interp
## The following object is masked from 'package:seewave':
## 
##     duration
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
## The following object is masked from 'package:dplyr':
## 
##     combine
library(datasets)
library(caret)
## Loading required package: lattice

Part 1: Waveform Analysis

read in files and convert into Wave class type

filenames <- list.files("150data", pattern="*.wav", full.names=TRUE)
ldf <- lapply(filenames, tuneR::readWave)

Spectrogram of a closed hi-hat.

hat <- ldf[[3]]
seewave::spectro(hat, f=48000, flog=TRUE, flim=c(0,20), tlim=c(0,0.2), main='hat')

### Spectrogram of a snare drum.

snare <- ldf[[115]]
seewave::spectro(snare, f=48000, flog=TRUE, flim=c(0,20), tlim=c(0,0.2), main='snare')

### Spectrogram of a kick drum.

kick <- ldf[[51]]
seewave::spectro(kick, f=48000, flog=TRUE, flim=c(0,20), tlim=c(0,0.2), main='kick')

### above are just three examples. Different samples end up getting very hard to tell apart. In this case, the snare spectrogram looks very similar to this kick drum’s spectrogram:

snare2 <- ldf[[101]]
kick2 <- ldf[[52]]
seewave::spectro(snare2, f=48000, flog=TRUE, flim=c(0,20), tlim=c(0,0.2), main='snare2')

seewave::spectro(kick2, f=48000, flog=TRUE, flim=c(0,20), tlim=c(0,0.2), main='kick2')

Compare dominant frequencies over time of three instrument samples

hatDfreq <- data.frame(seewave::dfreq(ldf[[2]], plot=FALSE, tlim=c(0,0.2), flim=c(0,20)))
snareDfreq <- data.frame(seewave::dfreq(ldf[[115]], plot=FALSE, tlim=c(0,0.2), flim=c(0,20)))
kickDfreq <- data.frame(seewave::dfreq(ldf[[51]], plot=FALSE, tlim=c(0,0.2), flim=c(0,20)))

hatDfreq$instrument <- "Hat"
snareDfreq$instrument <- "Snare"
kickDfreq$instrument <- "Kick"
combined_df <- rbind(hatDfreq, snareDfreq, kickDfreq)

ggplot(data = combined_df, aes(x = x, y = y, color = instrument)) +
  geom_line() +
  scale_y_log10() +
  labs(title = "Dominant frequencies Hat, Snare, and Kick") +
  xlab("Time (s)") +
  ylab("Frequency (kHz)")

# Part 2: Organizing/Tidying Data

go through training/testing data and add each file to a class based on its filename

# Function to trim strings based on substrings
trim_to_instrument <- function(string) {
  if (grepl("snare", string, ignore.case = TRUE)) {
    return("snare")
  } else if (grepl("hihat", string, ignore.case = TRUE)) {
    return("hat")
  } else if (grepl("kick", string, ignore.case = TRUE)) {
    return("kick")
  } else {
    stop("Error: No match found for instrument in string '", string, "'")
  }
}

class_list <- sapply(filenames, trim_to_instrument)
class_list <- factor(class_list)
class_list

hand-crafted features

#medians of amplitude envelopes
medians <- sapply(ldf, seewave::M, simplify = TRUE)

#dominant frequencies each wave
Dfreq <- lapply(ldf, seewave::dfreq, plot=FALSE)
maxes <- as.data.frame(Dfreq) %>% select(-starts_with("x"))
maxes <-sapply(maxes, function(x) max(x, na.rm = TRUE), simplify = TRUE)

#spectral flatnesses
specs <- lapply(ldf, spec, plot=FALSE)
sfms <- sapply(specs, sfm, simplify = TRUE)

#mfccs
M1 <- extract_features(filenames, 
  features = c("mfcc"), 
  check.mono=FALSE, 
  windowShift = 20,
  numcep = 4)
mfccs <- subset(M1, section_seq_file == 1)
mfccdf <- mfccs[, -c(1:3)]

combine into single dataframe

data <- data.frame(medians, maxes, sfms, class_list)
rownames(data) <- NULL
df <- cbind(data, mfccdf)
df
##          medians    maxes        sfms class_list     mfcc1       mfcc2
## 1   2.689808e-05  4.68750 0.204609636        hat 179.13176  -8.4186252
## 2   1.146886e-05  7.78125 0.332010126        hat 183.18767 -11.4214664
## 3   5.199873e-07 16.78125 0.374170458        hat 172.16686 -23.3354828
## 4   1.891459e-06  9.37500 0.401794313        hat 178.16386 -14.8884762
## 5   1.624056e-06 13.59375 0.316058994        hat 168.37095 -21.5579093
## 6   1.441962e-06 11.25000 0.350822215        hat 179.17786 -16.5245074
## 7   1.634273e-05 11.62500 0.270590288        hat 177.23093 -16.6687942
## 8   8.952057e-06  9.28125 0.208957330        hat 179.00688 -14.6181504
## 9   1.556757e-05 15.75000 0.371195821        hat 163.77835 -28.3969917
## 10  5.354960e-06  8.06250 0.613808716        hat 185.83359 -14.4250497
## 11  1.473575e-06  9.75000 0.377004882        hat 179.04478 -20.7076699
## 12  1.504905e-06 12.00000 0.489114714        hat 172.61765 -22.0449068
## 13  1.852330e-06 11.71875 0.511710975        hat 173.98228 -19.9100880
## 14  3.831130e-06  8.06250 0.389379558        hat 178.12046 -14.8544594
## 15  6.757226e-06 13.03125 0.548991098        hat 183.30619 -13.9183048
## 16  3.147135e-05  9.93750 0.633846896        hat 184.36135 -15.1086920
## 17  4.252192e-06 14.06250 0.524943734        hat 183.94280 -13.2929628
## 18  1.119627e-07  0.75000 0.360201324        hat 180.19873  -5.3928232
## 19  8.157962e-06  9.00000 0.462939010        hat 184.73543 -16.0771623
## 20  2.937231e-05  5.15625 0.384998770        hat 173.62530  -6.8263906
## 21  6.021016e-06  7.31250 0.337686742        hat 178.45422  -9.7380050
## 22  1.335820e-07 11.34375 0.254739940        hat 182.43873 -14.0990301
## 23  9.898379e-06 11.43750 0.603516666        hat 173.14518 -24.5168624
## 24  1.351084e-06 14.25000 0.233537795        hat 175.43558 -19.9303916
## 25  5.634163e-07 11.43750 0.245426113        hat 161.05774 -31.5444937
## 26  1.192777e-04  1.96875 0.510714357        hat 182.53586 -18.5400257
## 27  8.861115e-06  6.75000 0.564857756        hat 187.79381  -6.8814404
## 28  5.223584e-06  7.31250 0.190131747        hat 178.67598  -9.9941976
## 29  3.365414e-07  8.53125 0.368618928        hat 170.62732 -23.5899544
## 30  1.033127e-06 12.28125 0.455991279        hat 170.09807 -14.4813754
## 31  1.082676e-04  2.62500 0.078853930        hat 179.69730   1.5428021
## 32  1.920318e-07  7.21875 0.249662105        hat 167.60414 -25.1434222
## 33  7.136607e-07  9.09375 0.306253019        hat 184.50257  -9.6611125
## 34  3.691089e-05  4.59375 0.085779730        hat 181.87840  -9.1703514
## 35  2.888238e-06 12.18750 0.474958645        hat 180.49182 -16.3576595
## 36  3.720352e-04  7.96875 0.114538539        hat 164.00627 -25.9433411
## 37  2.566214e-06  0.84375 0.453368444        hat 186.29589  -4.4846892
## 38  7.932772e-06  0.84375 0.548146300        hat 185.23941  -4.7672963
## 39  1.683517e-04 14.25000 0.627535305        hat 174.46764 -20.3421474
## 40  4.926012e-06  7.31250 0.495003509        hat 177.23701  -9.9900238
## 41  1.440345e-06  6.18750 0.500357828        hat 180.22335 -11.8839351
## 42  1.117089e-04 10.40625 0.418998740        hat 178.47703 -20.1238613
## 43  1.473671e-06 10.68750 0.255596307        hat 171.60734 -21.0752801
## 44  5.685891e-07 10.31250 0.229650011        hat 168.86423 -19.4409817
## 45  8.737135e-06  9.84375 0.592358444        hat 182.38106 -15.8901450
## 46  5.052177e-05 11.15625 0.612433193        hat 183.01935 -14.2381211
## 47  2.409363e-05  7.21875 0.470644207        hat 183.84542 -16.8333737
## 48  2.434209e-06  6.09375 0.351552237        hat 185.30403 -13.8566946
## 49  1.560678e-06  0.65625 0.589903132        hat 185.64166  -9.9161559
## 50  5.395438e-06  3.65625 0.601378268        hat 182.68407 -17.8365918
## 51  8.407019e-05  0.09375 0.057142562       kick 161.51651   4.7673882
## 52  5.291000e-04  0.18750 0.024344738       kick 142.53651  17.0964511
## 53  4.457660e-05  0.46875 0.026614052       kick 141.56901  20.3901404
## 54  1.326085e-04  0.18750 0.291048613       kick 152.85266  19.0358667
## 55  5.237076e-05  0.28125 0.125885813       kick 175.25998   3.7429051
## 56  1.055505e-04  0.09375 0.080569046       kick 157.75299   8.9947794
## 57  5.184660e-05  0.09375 0.172993821       kick 166.94362   9.7833854
## 58  2.577285e-04  0.18750 0.060536994       kick 155.66043  13.7528528
## 59  2.152496e-04  0.18750 0.030535781       kick 157.58159  11.6234210
## 60  9.166327e-05  0.09375 0.370810526       kick 170.45926  16.1937310
## 61  1.344033e-05  0.56250 0.131962211       kick 177.00501  -2.5516652
## 62  1.762508e-04  0.18750 0.022893848       kick 151.44987  12.8886139
## 63  1.713291e-05 20.53125 0.103452213       kick 151.12373  26.9114396
## 64  4.193571e-05  0.18750 0.003782977       kick 136.63747  33.7044183
## 65  5.277706e-04  0.46875 0.054743096       kick 167.18728   4.4635062
## 66  2.571393e-04  0.18750 0.094357419       kick 170.93574   8.1687742
## 67  1.294538e-04  0.09375 0.014036870       kick 142.85994  16.1353831
## 68  4.148206e-04  0.09375 0.018002310       kick 126.32164  20.0518934
## 69  8.107040e-05  0.09375 0.011346204       kick 147.76110  15.1611760
## 70  3.817227e-05  0.18750 0.012162666       kick 134.47140  27.9742188
## 71  9.052531e-06  0.18750 0.062030514       kick 164.88101  10.5918608
## 72  4.418969e-05  0.28125 0.009187091       kick 146.68970  29.4229147
## 73  2.320087e-02  0.18750 0.203656220       kick 173.81105  12.8734649
## 74  1.251280e-04  0.18750 0.237988990       kick 158.97455  14.4092694
## 75  3.055273e-02  0.93750 0.007411004       kick 154.68397  20.1766675
## 76  1.369901e-04  0.09375 0.097692966       kick 159.01601  11.4967982
## 77  7.368968e-06  0.37500 0.012102547       kick 136.17771  27.7117063
## 78  7.730536e-05  0.56250 0.030288648       kick 177.43632   6.2760312
## 79  2.239873e-06 18.28125 0.020515445       kick 155.23720  21.1993482
## 80  2.383305e-04  0.09375 0.093970147       kick 155.99331  15.1787798
## 81  4.219157e-05 18.18750 0.027821534       kick 145.87191  21.4673245
## 82  1.175613e-02  0.18750 0.034373083       kick 151.13485  16.0076403
## 83  1.113747e-04  0.18750 0.001355992       kick  95.88751  35.2781636
## 84  1.823330e-04  0.09375 0.097934232       kick 142.05526  11.7680383
## 85  2.002760e-04  0.09375 0.136842276       kick 142.00318  12.8765586
## 86  3.431754e-05  0.28125 0.006041619       kick 134.52313  31.5292740
## 87  3.692497e-04 17.25000 0.276856330       kick 139.97684  21.8515364
## 88  2.350660e-05  0.18750 0.032402699       kick 158.35052   4.0577390
## 89  1.122703e-05  0.18750 0.049876647       kick 162.08072  14.1314438
## 90  1.020991e-05  0.18750 0.172297825       kick 168.86059   9.0104140
## 91  1.562879e-04  0.00000 0.005905879       kick 140.07089  25.7198919
## 92  1.152145e-04  0.28125 0.072869592       kick 161.36616   7.6372015
## 93  9.101402e-05  0.18750 0.051601038       kick 159.12711   7.1974033
## 94  1.727802e-02 22.03125 0.111895926       kick 149.22000   2.9568758
## 95  1.823137e-02  0.28125 0.438407337       kick 165.13194  11.3653763
## 96  2.333464e-04  0.09375 0.111866619       kick 166.53563   7.2131403
## 97  6.769791e-06  5.53125 0.075174343       kick 147.87955  -1.4661615
## 98  5.074300e-05  0.09375 0.049103251       kick 149.01507  18.5185363
## 99  5.279081e-04  0.28125 0.441115736       kick 165.51721  16.0537789
## 100 1.400839e-06  0.46875 0.080649764       kick 172.23041  13.5007293
## 101 1.528673e-05  0.18750 0.103848503      snare 172.91244  -0.4095897
## 102 2.540404e-05  8.43750 0.202207793      snare 174.17290   7.6647496
## 103 7.108802e-06  0.18750 0.311904275      snare 170.43778   9.4053058
## 104 4.334505e-05  4.03125 0.224389603      snare 187.73820  -2.3863878
## 105 8.672130e-06  0.28125 0.562496755      snare 182.70515  -0.5330093
## 106 3.203001e-05  0.18750 0.165788719      snare 180.29698   5.4070671
## 107 3.363245e-05  6.65625 0.166583540      snare 177.45030   5.8556149
## 108 2.985106e-05  1.40625 0.214977059      snare 184.35739   7.0425564
## 109 4.765681e-05  0.56250 0.533183656      snare 191.13263  -0.4223163
## 110 2.702805e-05  3.56250 0.212092730      snare 178.34488   5.0190441
## 111 6.680592e-05  1.40625 0.204702160      snare 176.87073   6.3482302
## 112 5.905738e-04  4.21875 0.250768090      snare 167.34591  11.9822175
## 113 8.817453e-05  0.18750 0.438753213      snare 180.91046  -4.6364954
## 114 4.455403e-04  1.03125 0.222594792      snare 194.51535   1.0809124
## 115 2.514580e-05 16.31250 0.372380482      snare 193.01986  -5.0952279
## 116 8.199202e-05  0.37500 0.167899940      snare 181.73177   3.9410395
## 117 5.972964e-07  1.78125 0.152938480      snare 174.63080   4.7147041
## 118 5.816549e-05 12.65625 0.505344186      snare 192.19724  -2.2318089
## 119 3.019170e-06  1.87500 0.392507857      snare 186.59025  -0.1826460
## 120 1.466286e-03  2.25000 0.135961134      snare 187.34641   2.0747612
## 121 1.393724e-05 17.81250 0.390488101      snare 173.20535  -2.7609877
## 122 3.465540e-06  1.03125 0.086901874      snare 178.05244  14.1895037
## 123 6.545849e-06  1.21875 0.105498052      snare 176.04196  12.1244793
## 124 1.218990e-05 19.21875 0.663095839      snare 183.89248  -1.4887017
## 125 2.418153e-04  6.46875 0.303779875      snare 176.82878 -15.0344748
## 126 2.177171e-06  0.37500 0.261860384      snare 170.99443   8.7250004
## 127 5.607254e-05  4.31250 0.185445415      snare 186.32630   4.7875696
## 128 6.806349e-05  0.28125 0.308343195      snare 173.95145   7.4783151
## 129 2.581208e-05  0.28125 0.354685651      snare 169.83100   6.3200342
## 130 6.062868e-06  3.28125 0.270300918      snare 162.53387   4.5514542
## 131 1.125138e-05  0.28125 0.345136805      snare 176.29271   6.1878243
## 132 1.356422e-04  5.34375 0.148947970      snare 183.04525   7.5239666
## 133 2.407483e-04  0.09375 0.427849714      snare 183.30619 -11.4563491
## 134 2.909831e-05 10.78125 0.596531542      snare 179.05788 -15.1999677
## 135 1.539608e-04  0.18750 0.618347358      snare 179.22177 -17.5669965
## 136 6.455536e-06  8.25000 0.109153746      snare 184.60320   6.7688051
## 137 3.522766e-07  3.93750 0.130465809      snare 172.15718   1.8311727
## 138 4.773910e-05  0.28125 0.133568867      snare 176.52417   0.7682257
## 139 1.145767e-05  2.62500 0.270061477      snare 178.43966   1.5946750
## 140 6.397396e-06  2.62500 0.068455294      snare 183.30186   4.6716860
## 141 2.285158e-05  1.59375 0.200413753      snare 182.35789   3.3827218
## 142 2.157369e-05  0.37500 0.241202598      snare 179.05752   0.7194225
## 143 2.009124e-05  0.56250 0.234461463      snare 161.32226   1.2427679
## 144 2.358965e-06 10.03125 0.195675454      snare 181.22278   3.2475291
## 145 7.407383e-05  0.65625 0.219563810      snare 189.98599   1.6208675
## 146 3.160992e-06 12.93750 0.317142973      snare 185.49079   2.1532158
## 147 3.596121e-05 22.03125 0.693519539      snare 183.20340  -6.5147809
## 148 4.087562e-05 22.03125 0.747349813      snare 183.13749  -5.3873135
## 149 3.694842e-06  0.75000 0.136718674      snare 182.85419   3.8994445
## 150 1.094794e-05  0.93750 0.070344086      snare 170.91342  13.7381390
##           mfcc3      mfcc4
## 1   -22.2266497  5.4983729
## 2   -10.2173828 10.9199130
## 3   -17.0900428  8.8959059
## 4    -2.4628270 10.6916431
## 5    -5.5933576 -3.3462257
## 6    -9.3727077  7.2097609
## 7    -8.1429035 10.1847197
## 8    -8.8472196 10.1344239
## 9     9.0947728  6.3065136
## 10   -3.6443965  7.1433724
## 11  -11.1722927  5.5566139
## 12   -6.2609100  7.2653590
## 13   -6.2359357  2.9033833
## 14  -21.5949905  5.2475098
## 15   -6.8435270 -3.5483814
## 16   -8.7167288  0.4392875
## 17   -1.4685915 -2.2987207
## 18   -9.5498280 11.8003222
## 19   -6.4742157  7.4232270
## 20  -21.6432298  4.5835707
## 21  -18.5444434 13.4651215
## 22   -4.9210053  7.2452475
## 23   -3.0723502  2.2103164
## 24   -5.9328622  7.9336039
## 25  -11.7994496  1.0893452
## 26   -9.2621990 -1.1619709
## 27   -4.7386791  4.4770220
## 28  -18.1038554 12.8495306
## 29  -15.6154963 12.7553672
## 30  -11.6899067 10.7248676
## 31  -22.9884041  3.6982331
## 32  -11.1222380 15.8549724
## 33   -6.5425083 10.9950550
## 34  -14.4323975  4.3003312
## 35  -12.6062424 -1.3578396
## 36  -13.3572082 14.5540571
## 37   -1.8118386  5.2001825
## 38   -1.2092119  6.1024916
## 39   -6.5343994  7.3590477
## 40  -18.5949217 13.6261572
## 41  -15.6396394  9.1907323
## 42   -8.9015626  3.3612390
## 43  -15.6784021  5.1031629
## 44  -19.9485736  7.5666933
## 45   -3.1764772  3.1697844
## 46   -2.1782362  3.3429362
## 47   -9.0067124 -2.1119588
## 48  -17.3591962  0.6043887
## 49   -4.4612607  4.6746380
## 50  -10.4394127 -3.9981515
## 51   -3.4100344  9.9637937
## 52  -12.6209638 19.6730449
## 53    8.4046262  2.1887415
## 54    1.8373191 10.8962592
## 55   -4.9588128 18.8004488
## 56   -7.9114989 15.3201896
## 57   -3.5620316 12.9844903
## 58    2.6696999  5.1950539
## 59   -1.0481709 15.9054842
## 60   -6.3762451  6.8466676
## 61    2.7616489  2.3393825
## 62    5.5918683  9.8262825
## 63   -2.4033003  6.0380387
## 64    3.1928141  3.8697912
## 65    9.8352587  5.3042973
## 66  -16.1580704 17.6257356
## 67   -4.7792632 18.2758004
## 68    5.2596512  9.8110658
## 69  -15.6189140 19.2764216
## 70    6.6522885  5.2977546
## 71  -11.4104282 17.5374381
## 72   -3.3255250  5.6113734
## 73   -8.8260564  4.8307718
## 74  -10.4904537 10.3356783
## 75  -15.9480287  4.9772580
## 76   -8.6423947 15.2312660
## 77    4.6676404 10.5653823
## 78   -2.6456542  3.5187983
## 79   -3.7716696 10.5790510
## 80  -16.9711362 17.1360156
## 81   -4.5398614 11.2576129
## 82   -2.8477993 10.6798383
## 83   14.7044891 12.2616238
## 84   -0.0698215 17.5824286
## 85   -0.0753951 16.2584388
## 86    1.7760388  6.6894834
## 87    0.5896923 10.1248785
## 88   -4.2453708 16.4714535
## 89  -10.6671218 11.5033529
## 90   -8.2381613 12.7384228
## 91    2.6894392  8.8823248
## 92   -6.3458429 17.8577149
## 93   -6.1912325 14.6654075
## 94   12.2910900 14.3431856
## 95    5.1099907  8.3290034
## 96   -0.2844363  2.9140864
## 97  -24.5061126 24.0664267
## 98   -2.6489804  2.2634454
## 99   -3.0587087 11.1428467
## 100 -17.3217805 21.2009676
## 101  -1.1403637 15.5037088
## 102  -5.1624171 17.1905847
## 103  -6.2059259  9.0416620
## 104 -14.0258292  9.6712709
## 105   2.6782809  5.0003767
## 106 -12.9218803 14.3728291
## 107 -10.3656917 12.5808927
## 108 -15.1725845 -1.3123136
## 109  -2.0416413  4.1797838
## 110  -7.3721832 15.9157013
## 111  -4.0645482 12.7691133
## 112  -7.4337065  9.9801683
## 113  -6.8005308 12.7963674
## 114  -9.8180964  0.3378207
## 115  -6.8233056  6.0154933
## 116  -9.7855686 10.0624400
## 117 -15.2068961  9.4997140
## 118  -9.6178682 -1.8371937
## 119  -2.6327708  1.9417207
## 120 -11.1593874  6.3810069
## 121  -2.9728552 10.4572877
## 122 -19.1392327  8.5744510
## 123 -22.7988934 11.5024686
## 124  -2.3419208  4.8644868
## 125  -7.0812997  9.9788337
## 126  -9.4846551 11.6504252
## 127 -12.1606480  4.3219996
## 128  -0.5336350  7.9173719
## 129  -1.7490994  9.0260530
## 130  -2.0317069 -1.0434656
## 131 -15.2937853 14.1150659
## 132 -16.2975156  1.3036426
## 133  -3.2786682  9.4530662
## 134  -1.2539340 12.6340527
## 135  -1.6881597 12.7991626
## 136  -6.1249054 12.6215970
## 137  -4.0551442 14.7351238
## 138  -5.5837700 14.8450918
## 139 -14.7924421  3.1231936
## 140 -16.5789251  8.3109233
## 141  -5.3577890  2.3254065
## 142 -10.7966357  9.2839551
## 143  -8.4668109  9.7020077
## 144  -3.6318699  8.0684473
## 145  -7.6611927  7.3208005
## 146  -1.3709559  4.9421209
## 147  -9.2769118  8.1330580
## 148  -8.9384303  7.2204205
## 149  -9.7939161 10.0719736
## 150 -22.5597919 15.1649295

convert class list into factor variable, check with frequency table, separate into test and training data

df$class_list <- as.factor(df$class_list)
table(df$class_list)
## 
##   hat  kick snare 
##    50    50    50
#set.seed(222)
ind <- sample(2, nrow(df), replace = TRUE, prob = c(0.7, 0.3))
train <- df[ind==1,]
test <- df[ind==2,]

Part 3: Training Random Forest Classifier

create the random forest using the R randomForest package:

rf <- randomForest(class_list~., data=train, proximity=TRUE, ntree=150)
print(rf)
## 
## Call:
##  randomForest(formula = class_list ~ ., data = train, proximity = TRUE,      ntree = 150) 
##                Type of random forest: classification
##                      Number of trees: 150
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 11.65%
## Confusion matrix:
##       hat kick snare class.error
## hat    33    0     2  0.05714286
## kick    0   33     4  0.10810811
## snare   2    4    25  0.19354839

Use newly created random forest to classify its own training data. Resulting accuracy should be 100%

p1 <- predict(rf, train)
confusionMatrix(p1, train$ class_list)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction hat kick snare
##      hat    35    0     0
##      kick    0   37     0
##      snare   0    0    31
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9648, 1)
##     No Information Rate : 0.3592     
##     P-Value [Acc > NIR] : < 2.2e-16  
##                                      
##                   Kappa : 1          
##                                      
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: hat Class: kick Class: snare
## Sensitivity              1.0000      1.0000        1.000
## Specificity              1.0000      1.0000        1.000
## Pos Pred Value           1.0000      1.0000        1.000
## Neg Pred Value           1.0000      1.0000        1.000
## Prevalence               0.3398      0.3592        0.301
## Detection Rate           0.3398      0.3592        0.301
## Detection Prevalence     0.3398      0.3592        0.301
## Balanced Accuracy        1.0000      1.0000        1.000

classify test data and generate confusion matrix

p2 <- predict(rf, test)
confusionMatrix(p2, test$ class_list)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction hat kick snare
##      hat    14    0     3
##      kick    0   13     2
##      snare   1    0    14
## 
## Overall Statistics
##                                           
##                Accuracy : 0.8723          
##                  95% CI : (0.7426, 0.9517)
##     No Information Rate : 0.4043          
##     P-Value [Acc > NIR] : 3.959e-11       
##                                           
##                   Kappa : 0.8087          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: hat Class: kick Class: snare
## Sensitivity              0.9333      1.0000       0.7368
## Specificity              0.9062      0.9412       0.9643
## Pos Pred Value           0.8235      0.8667       0.9333
## Neg Pred Value           0.9667      1.0000       0.8437
## Prevalence               0.3191      0.2766       0.4043
## Detection Rate           0.2979      0.2766       0.2979
## Detection Prevalence     0.3617      0.3191       0.3191
## Balanced Accuracy        0.9198      0.9706       0.8506

randomForest’s tuneRF function to explore different ntree values

#### played around with this function a little bit, it didn't seem to be very helpful given that the tuning method has no patience for anything besides an decrease in error: 
t <- tuneRF(train[,-4], train[,4],
       stepFactor = 5,
       plot = TRUE,
       ntreeTry = 50,
       trace = TRUE,
       doBest=TRUE,
       improve = 0.01)
## mtry = 2  OOB error = 11.65% 
## Searching left ...
## mtry = 1     OOB error = 11.65% 
## 0 0.01 
## Searching right ...
## mtry = 7     OOB error = 16.5% 
## -0.4166667 0.01

p3 <- predict(t, test)
confusionMatrix(p3, test$ class_list)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction hat kick snare
##      hat    14    0     2
##      kick    0   13     1
##      snare   1    0    16
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9149          
##                  95% CI : (0.7962, 0.9763)
##     No Information Rate : 0.4043          
##     P-Value [Acc > NIR] : 2.917e-13       
##                                           
##                   Kappa : 0.8716          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: hat Class: kick Class: snare
## Sensitivity              0.9333      1.0000       0.8421
## Specificity              0.9375      0.9706       0.9643
## Pos Pred Value           0.8750      0.9286       0.9412
## Neg Pred Value           0.9677      1.0000       0.9000
## Prevalence               0.3191      0.2766       0.4043
## Detection Rate           0.2979      0.2766       0.3404
## Detection Prevalence     0.3404      0.2979       0.3617
## Balanced Accuracy        0.9354      0.9853       0.9032

Part 4: Classifier Analysis

graph is pretty self explanatory

hist(treesize(rf),
     main = "No. of Nodes for the Trees",
     col = "green")

### error rate of random forest

plot(rf)

### mfcc2 is the most important attribute followed by mfcc1

varImpPlot(rf, n.var = 7,main = "Variable Importance")

importance(rf)
##         MeanDecreaseGini
## medians         5.800406
## maxes           7.771504
## sfms           11.844088
## mfcc1          12.951061
## mfcc2          22.435988
## mfcc3           4.083050
## mfcc4           2.870667

Partial dependence plot gives a graphical depiction of the marginal effect of a variable on the class probability (classification)

partialPlot(rf, train, sfms, "kick")

#### if the sfm is less than ~0.3, higher chances of classifying into kick class.

Plot the scaling coordinates of the proximity matrix from randomForest

MDSplot(rf, train$class_list)