library("terra")
## terra 1.7.55
hym = rast("Data/HyMap_125bands_Karlsruhe2.tif")
hym = hym/100

tree = vect("Data/tree_species_KA.shp")

Task 1

plotRGB(hym,22,14,8,stretch = "lin",range=c(0,100))

plot(tree,col=tree$id,add = TRUE)

Task 2

The Carniferous forest are the darker forest here marked by pink markers for the test samples while the broad leafed forest brighter and marked by the light green points.

Task 3

hym.extract = extract(hym, tree)
xHelp = 1:126
plot(1:126,hym.extract[1,])
for(i in 1:250){
  points(1:126,hym.extract[i,])
}

Task 4

hym.extract$species = tree$id

hym.extract.mean <- aggregate(hym.extract, hym.extract[,"species", drop=FALSE], mean)
hym.extract.sd <- aggregate(hym.extract, hym.extract[,"species", drop=FALSE], sd)


mycolors= c("forest green", "blue", "red", "orange", "black")
# plot the spectra for the first tree id and add the standard deviations

for (i in 1:5){
  plot(1:125, hym.extract.mean[i, 2:126], type="l", col=mycolors[i], ylim=c(0,100), lwd=2, xlab= "band", ylab="reflectance [%]")
  # plot mean spectral signature + sd values
  lines(1:125, hym.extract.mean[i, 2:126]+hym.extract.sd[1,2:126], col=adjustcolor(mycolors[i], alpha.f=0.5), lty=2, lwd=2)
  # plot mean spectral signature - sd values
  lines(1:125, hym.extract.mean[i, 2:126]-hym.extract.sd[1, 2:126],  col=adjustcolor(mycolors[i], alpha.f=0.5), lty=2, lwd=2)
  par(new=T)
}

# add a legend indicating the species names
legend("topright", legend=c("Quercus robur", "Pinus sylvestris", "Quercus rubra", "Fagus sylvatica", "Pseudotsuga menziesii"), col=mycolors, lty=c(1,1,1,1,1)) 

Task 5

This shows us that separating the tree species is not going to be easy as the all have a very similar spectrum differentiated only by the strength of different features.

library("e1071")
## Warning: Paket 'e1071' wurde unter R Version 4.3.2 erstellt
## 
## Attache Paket: 'e1071'
## Das folgende Objekt ist maskiert 'package:terra':
## 
##     interpolate
gammas <-   2^(-8:5)
costs <- 2^(-5:8)
class.svm <- e1071::tune(svm,
              train.x = hym.extract[2:126],
              train.y = as.factor(hym.extract$species),
              type = "C-classification",
              kernel = "radial", 
              scale = TRUE,
              ranges = list(gamma = gammas, cost = costs),
              tunecontrol = tune.control(cross = 5)
              )
# Have a look at the information of the svm models:
summary(class.svm)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 5-fold cross validation 
## 
## - best parameters:
##       gamma cost
##  0.00390625  128
## 
## - best performance: 0.196 
## 
## - Detailed performance results:
##           gamma      cost error dispersion
## 1    0.00390625   0.03125 0.632 0.15401299
## 2    0.00781250   0.03125 0.620 0.14142136
## 3    0.01562500   0.03125 0.652 0.14939880
## 4    0.03125000   0.03125 0.684 0.14926487
## 5    0.06250000   0.03125 0.720 0.17720045
## 6    0.12500000   0.03125 0.732 0.16223440
## 7    0.25000000   0.03125 0.752 0.13827509
## 8    0.50000000   0.03125 0.804 0.07797435
## 9    1.00000000   0.03125 0.812 0.07563068
## 10   2.00000000   0.03125 0.836 0.04335897
## 11   4.00000000   0.03125 0.844 0.03286335
## 12   8.00000000   0.03125 0.844 0.03286335
## 13  16.00000000   0.03125 0.748 0.17469974
## 14  32.00000000   0.03125 0.720 0.16309506
## 15   0.00390625   0.06250 0.536 0.10334409
## 16   0.00781250   0.06250 0.472 0.12214745
## 17   0.01562500   0.06250 0.432 0.11189281
## 18   0.03125000   0.06250 0.456 0.08648699
## 19   0.06250000   0.06250 0.676 0.13594116
## 20   0.12500000   0.06250 0.728 0.15786070
## 21   0.25000000   0.06250 0.752 0.13827509
## 22   0.50000000   0.06250 0.804 0.07797435
## 23   1.00000000   0.06250 0.812 0.07563068
## 24   2.00000000   0.06250 0.836 0.04335897
## 25   4.00000000   0.06250 0.844 0.03286335
## 26   8.00000000   0.06250 0.844 0.03286335
## 27  16.00000000   0.06250 0.748 0.17469974
## 28  32.00000000   0.06250 0.720 0.16309506
## 29   0.00390625   0.12500 0.412 0.11454257
## 30   0.00781250   0.12500 0.388 0.10256705
## 31   0.01562500   0.12500 0.384 0.09528903
## 32   0.03125000   0.12500 0.364 0.09423375
## 33   0.06250000   0.12500 0.400 0.09695360
## 34   0.12500000   0.12500 0.656 0.10334409
## 35   0.25000000   0.12500 0.752 0.13827509
## 36   0.50000000   0.12500 0.804 0.07797435
## 37   1.00000000   0.12500 0.812 0.07563068
## 38   2.00000000   0.12500 0.836 0.04335897
## 39   4.00000000   0.12500 0.844 0.03286335
## 40   8.00000000   0.12500 0.844 0.03286335
## 41  16.00000000   0.12500 0.748 0.17469974
## 42  32.00000000   0.12500 0.720 0.16309506
## 43   0.00390625   0.25000 0.376 0.10526158
## 44   0.00781250   0.25000 0.364 0.10899541
## 45   0.01562500   0.25000 0.356 0.07924645
## 46   0.03125000   0.25000 0.352 0.07014271
## 47   0.06250000   0.25000 0.356 0.07797435
## 48   0.12500000   0.25000 0.392 0.06572671
## 49   0.25000000   0.25000 0.692 0.10256705
## 50   0.50000000   0.25000 0.804 0.07797435
## 51   1.00000000   0.25000 0.812 0.07563068
## 52   2.00000000   0.25000 0.836 0.04335897
## 53   4.00000000   0.25000 0.844 0.03286335
## 54   8.00000000   0.25000 0.844 0.03286335
## 55  16.00000000   0.25000 0.748 0.17469974
## 56  32.00000000   0.25000 0.720 0.16309506
## 57   0.00390625   0.50000 0.356 0.10334409
## 58   0.00781250   0.50000 0.344 0.06985700
## 59   0.01562500   0.50000 0.340 0.08000000
## 60   0.03125000   0.50000 0.328 0.07694154
## 61   0.06250000   0.50000 0.344 0.08876936
## 62   0.12500000   0.50000 0.324 0.10990905
## 63   0.25000000   0.50000 0.500 0.06164414
## 64   0.50000000   0.50000 0.752 0.05403702
## 65   1.00000000   0.50000 0.812 0.07563068
## 66   2.00000000   0.50000 0.836 0.04335897
## 67   4.00000000   0.50000 0.844 0.03286335
## 68   8.00000000   0.50000 0.844 0.03286335
## 69  16.00000000   0.50000 0.748 0.17469974
## 70  32.00000000   0.50000 0.720 0.16309506
## 71   0.00390625   1.00000 0.340 0.07071068
## 72   0.00781250   1.00000 0.324 0.07536577
## 73   0.01562500   1.00000 0.320 0.07071068
## 74   0.03125000   1.00000 0.324 0.06841053
## 75   0.06250000   1.00000 0.308 0.07014271
## 76   0.12500000   1.00000 0.300 0.06633250
## 77   0.25000000   1.00000 0.352 0.06418723
## 78   0.50000000   1.00000 0.508 0.03346640
## 79   1.00000000   1.00000 0.716 0.07668116
## 80   2.00000000   1.00000 0.800 0.07483315
## 81   4.00000000   1.00000 0.836 0.04335897
## 82   8.00000000   1.00000 0.844 0.03286335
## 83  16.00000000   1.00000 0.844 0.03286335
## 84  32.00000000   1.00000 0.844 0.03286335
## 85   0.00390625   2.00000 0.320 0.07071068
## 86   0.00781250   2.00000 0.304 0.06066300
## 87   0.01562500   2.00000 0.308 0.06723095
## 88   0.03125000   2.00000 0.296 0.08648699
## 89   0.06250000   2.00000 0.268 0.05932959
## 90   0.12500000   2.00000 0.288 0.05019960
## 91   0.25000000   2.00000 0.328 0.06099180
## 92   0.50000000   2.00000 0.492 0.03633180
## 93   1.00000000   2.00000 0.664 0.09316652
## 94   2.00000000   2.00000 0.792 0.07563068
## 95   4.00000000   2.00000 0.828 0.05403702
## 96   8.00000000   2.00000 0.844 0.03286335
## 97  16.00000000   2.00000 0.844 0.03286335
## 98  32.00000000   2.00000 0.844 0.03286335
## 99   0.00390625   4.00000 0.292 0.06260990
## 100  0.00781250   4.00000 0.292 0.06260990
## 101  0.01562500   4.00000 0.264 0.06985700
## 102  0.03125000   4.00000 0.252 0.08197561
## 103  0.06250000   4.00000 0.256 0.06228965
## 104  0.12500000   4.00000 0.272 0.05215362
## 105  0.25000000   4.00000 0.328 0.06099180
## 106  0.50000000   4.00000 0.492 0.03633180
## 107  1.00000000   4.00000 0.664 0.09316652
## 108  2.00000000   4.00000 0.792 0.07563068
## 109  4.00000000   4.00000 0.828 0.05403702
## 110  8.00000000   4.00000 0.844 0.03286335
## 111 16.00000000   4.00000 0.844 0.03286335
## 112 32.00000000   4.00000 0.844 0.03286335
## 113  0.00390625   8.00000 0.268 0.05019960
## 114  0.00781250   8.00000 0.260 0.06164414
## 115  0.01562500   8.00000 0.232 0.05932959
## 116  0.03125000   8.00000 0.220 0.03162278
## 117  0.06250000   8.00000 0.244 0.04979960
## 118  0.12500000   8.00000 0.268 0.04381780
## 119  0.25000000   8.00000 0.328 0.06099180
## 120  0.50000000   8.00000 0.492 0.03633180
## 121  1.00000000   8.00000 0.664 0.09316652
## 122  2.00000000   8.00000 0.792 0.07563068
## 123  4.00000000   8.00000 0.828 0.05403702
## 124  8.00000000   8.00000 0.844 0.03286335
## 125 16.00000000   8.00000 0.844 0.03286335
## 126 32.00000000   8.00000 0.844 0.03286335
## 127  0.00390625  16.00000 0.252 0.03033150
## 128  0.00781250  16.00000 0.236 0.07536577
## 129  0.01562500  16.00000 0.216 0.06066300
## 130  0.03125000  16.00000 0.228 0.04604346
## 131  0.06250000  16.00000 0.236 0.03577709
## 132  0.12500000  16.00000 0.268 0.04381780
## 133  0.25000000  16.00000 0.328 0.06099180
## 134  0.50000000  16.00000 0.492 0.03633180
## 135  1.00000000  16.00000 0.664 0.09316652
## 136  2.00000000  16.00000 0.792 0.07563068
## 137  4.00000000  16.00000 0.828 0.05403702
## 138  8.00000000  16.00000 0.844 0.03286335
## 139 16.00000000  16.00000 0.844 0.03286335
## 140 32.00000000  16.00000 0.844 0.03286335
## 141  0.00390625  32.00000 0.224 0.05176872
## 142  0.00781250  32.00000 0.224 0.02966479
## 143  0.01562500  32.00000 0.224 0.05176872
## 144  0.03125000  32.00000 0.228 0.03898718
## 145  0.06250000  32.00000 0.236 0.03577709
## 146  0.12500000  32.00000 0.268 0.04381780
## 147  0.25000000  32.00000 0.328 0.06099180
## 148  0.50000000  32.00000 0.492 0.03633180
## 149  1.00000000  32.00000 0.664 0.09316652
## 150  2.00000000  32.00000 0.792 0.07563068
## 151  4.00000000  32.00000 0.828 0.05403702
## 152  8.00000000  32.00000 0.844 0.03286335
## 153 16.00000000  32.00000 0.844 0.03286335
## 154 32.00000000  32.00000 0.844 0.03286335
## 155  0.00390625  64.00000 0.200 0.03741657
## 156  0.00781250  64.00000 0.232 0.03033150
## 157  0.01562500  64.00000 0.240 0.05477226
## 158  0.03125000  64.00000 0.228 0.03898718
## 159  0.06250000  64.00000 0.236 0.03577709
## 160  0.12500000  64.00000 0.268 0.04381780
## 161  0.25000000  64.00000 0.328 0.06099180
## 162  0.50000000  64.00000 0.492 0.03633180
## 163  1.00000000  64.00000 0.664 0.09316652
## 164  2.00000000  64.00000 0.792 0.07563068
## 165  4.00000000  64.00000 0.828 0.05403702
## 166  8.00000000  64.00000 0.844 0.03286335
## 167 16.00000000  64.00000 0.844 0.03286335
## 168 32.00000000  64.00000 0.844 0.03286335
## 169  0.00390625 128.00000 0.196 0.03847077
## 170  0.00781250 128.00000 0.224 0.02607681
## 171  0.01562500 128.00000 0.240 0.05477226
## 172  0.03125000 128.00000 0.228 0.03898718
## 173  0.06250000 128.00000 0.236 0.03577709
## 174  0.12500000 128.00000 0.268 0.04381780
## 175  0.25000000 128.00000 0.328 0.06099180
## 176  0.50000000 128.00000 0.492 0.03633180
## 177  1.00000000 128.00000 0.664 0.09316652
## 178  2.00000000 128.00000 0.792 0.07563068
## 179  4.00000000 128.00000 0.828 0.05403702
## 180  8.00000000 128.00000 0.844 0.03286335
## 181 16.00000000 128.00000 0.844 0.03286335
## 182 32.00000000 128.00000 0.844 0.03286335
## 183  0.00390625 256.00000 0.200 0.02449490
## 184  0.00781250 256.00000 0.224 0.02607681
## 185  0.01562500 256.00000 0.240 0.05477226
## 186  0.03125000 256.00000 0.228 0.03898718
## 187  0.06250000 256.00000 0.236 0.03577709
## 188  0.12500000 256.00000 0.268 0.04381780
## 189  0.25000000 256.00000 0.328 0.06099180
## 190  0.50000000 256.00000 0.492 0.03633180
## 191  1.00000000 256.00000 0.664 0.09316652
## 192  2.00000000 256.00000 0.792 0.07563068
## 193  4.00000000 256.00000 0.828 0.05403702
## 194  8.00000000 256.00000 0.844 0.03286335
## 195 16.00000000 256.00000 0.844 0.03286335
## 196 32.00000000 256.00000 0.844 0.03286335
plot(class.svm)

gamma <- class.svm$best.model$gamma
cost <- class.svm$best.model$cost

# train model
svm.best <- svm(hym.extract[2:126], as.factor(hym.extract$species), gamma = gamma, cost = cost, probability = TRUE, cross=5)

Task 6

The parameter cross stands for the partitions for cross-validation. Cross-validation is a technique in machine learning where the data is divided into subsets, and the model is trained and tested multiple times on different combinations of these subsets to obtain a more reliable performance evaluation. It helps assess a model’s ability to generalize to new data by simulating various training-test splits.

hymap.class.svm <- terra::predict(hym, svm.best,
                  filename = "hymap_svm.tif",
                  overwrite = TRUE,type="prob"
                  #na.rm = T
)

Task 7

colors = c('#b3e2cd','#fdcdac','#cbd5e8','#f4cae4','#e6f5c9')

plot(hymap.class.svm,col = colors)
legend(x="bottomleft", c("A","B","C","D","E"),inset=0.18,fill = colors,bg="white",cex=1.0)

library("randomForest")
## Warning: Paket 'randomForest' wurde unter R Version 4.3.2 erstellt
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
class.tunerf <- tuneRF(hym.extract[,2:116],
                  as.factor(hym.extract$species),
                  mtryStart = 5,
                  ntree = 1000,
                  importance=T,
                  doBest = T
                  )
## mtry = 5  OOB error = 28.8% 
## Searching left ...
## mtry = 3     OOB error = 28.4% 
## 0.01388889 0.05 
## Searching right ...
## mtry = 10    OOB error = 28.4% 
## 0.01388889 0.05

# Let's have a look at the performance of the best model:
class.tunerf
## 
## Call:
##  randomForest(x = x, y = y, mtry = res[which.min(res[, 2]), 1],      importance = ..1) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 30.4%
## Confusion matrix:
##    1  2  3  4  5 class.error
## 1 27  2 16  4  1        0.46
## 2  3 38  0  0  9        0.24
## 3 11  1 35  3  0        0.30
## 4  5  0  3 42  0        0.16
## 5  2 13  1  2 32        0.36

Task 8

The Confusion Matrix shows that the first class is the least accurate predicted class while as the 4th class is the most accurately predicted.

hymap.class.rf.call1 <- terra::predict(hym, class.tunerf,
                  filename = "hymap_svm_call1.tif",
                  overwrite = TRUE,type="prob"
                  #na.rm = T
)

# Call2:                  
hymap.class.rf.call2 <- terra::predict(hym, class.tunerf,
                  filename = "hymap_svm2_call2.tif",
                  overwrite = TRUE
                  #na.rm =T
)

The Difference betweent the two function calls is the argument type = “prob” , with type=“prob” we will obtain 5 different Rasters with the probability of the pixel being classified as the corresponding category. While on the other function call we obtain one raster with pixels being assigned one of 5 discrete values it is classified as.

Task 9

probability = app(hymap.class.rf.call1,"max")

plot(probability)

Task 10

majorityvote = app(hymap.class.rf.call1,"which.max")

plot(majorityvote == hymap.class.rf.call2)

Task 11

To potentially compare the classification we would need to have a large enough subset where we correctly know the classes so that we could compare the correct classes to the predicted classes and then by the law of large numbers say which one is more accurate. Because of the way Machine Learning works the better algorithm would heavily depend on the dataset though. So a generalized statement cannot be made. The better statements can be made about the Calculation Speed and how it scales in Big-O notation.