-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample.Rmd
1508 lines (1036 loc) · 48.1 KB
/
Sample.Rmd
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ETC5250 Assignment 2"
author: "Dewi Lestari Amaliah - 31251587"
date: "16/04/2021"
output:
bookdown::html_document2:
toc: true
toc_float : true
after_body: tutorial-footer.html
css: assignment.css
number_sections: no
---
```{r echo = FALSE, message = FALSE, warning = FALSE}
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
error = FALSE,
collapse = TRUE,
echo = FALSE,
comment = "#",
fig.height = 4,
fig.width = 8,
fig.align = "center",
cache = FALSE
)
```
```{r packages}
library(tidyverse)
library(tidymodels)
library(discrim)
library(MASS)
library(broom)
library(rpart)
library(patchwork)
library(caret)
library(palmerpenguins)
library(GGally)
library(tourr)
library(matlib)
library(kableExtra)
library(spinifex)
library(boot)
library(plotly)
```
# Question 1
The lecture notes provide code to plot the QDA boundary between regions 2 and 3 of the olive oils data, using `linoleic` and `arachidic` acid. Use the same split of training and testing sets as used in lecture notes.
```{r q1}
# make a standardization function
std <- function(x) (x-mean(x, na.rm=TRUE))/sd(x, na.rm=TRUE)
# olive <- read_csv("http://ggobi.org/book/data/olive.csv") %>%
# dplyr::filter(region != 1) %>%
# dplyr::select(region, arachidic, linoleic, oleic) %>%
# dplyr::mutate(region = factor(region)) %>%
# #standardize the variable
# dplyr::mutate(arachidic_std = (arachidic - mean(arachidic))/ sd(arachidic),
# linoleic_std = (linoleic - mean(linoleic))/ sd(linoleic),
# oleic_std = (oleic - mean(oleic))/ sd(oleic))
# read the data set
olive <- read_csv("http://ggobi.org/book/data/olive.csv")
# create a standardized variable data frame
olive_std <- olive %>%
dplyr::filter(region != 1) %>%
dplyr::select(region, arachidic, linoleic, oleic) %>%
dplyr::mutate(region = factor(region)) %>%
#standardize the variable
mutate_if(is.numeric, std)
# split the data into train and test
set.seed(775)
olive_split <- initial_split(olive_std, 2/3, strata = region)
olive_train <- analysis(olive_split)
olive_test <- assessment(olive_split)
```
## Question 1a
**Write down the equation for discriminant function for region 2 as specified by QDA, using values computed from the data. (Use standardised variables.) Also, explain what assumption is not satisfied for QDA to be correctly applied for this data.**
**Answer**
The discriminant function of QDA for each class could be written as follow (James, Witten, Hastie, & Tibshirani, 2013):
$$\delta_k(x) = -\frac12x^T\Sigma_k^{-1}x + x^T\Sigma_k^{-1}\mu_k - \frac12\mu_k^T\Sigma_k^{-1}\mu_k - \frac12 \log{|\Sigma_k|} + \log(\pi_k)$$
Where:
- $\Sigma_k$ is the variance-covariance matrix of class k.
- $\mu_k$ is the means for class k
- $x$ is the array of the predictors
- $\pi_k$ is the prior probability of class k
Using `qda` function from `MASS` (Venables & Ripley, 2002) we are able to get the $\mu_k$ and $\pi_k$ values. However, this function does not produce the value of $\Sigma_k$. Hence, we have to calculate it separately using the following formula (Lauer, 2014; Jones & Del Fiore, 2020):
$$\hat\Sigma_k = \frac1{n_k -1}\sum_{y_i = k}(x_i - \hat{\mu})(x_i - \hat{\mu})^T$$
Where :
- $n_c$ is the number of observation in class k
- $x_i$ is the array of the predictors for ith observation
- $\hat{\mu}$ is the array of estimate means of class k
**Prior probabilities and group means of region 1 and 2**
```{r q1a}
# perform the QDA
olive_qda_fit <- qda(region ~ arachidic + linoleic,
data = olive_train)
olive_qda_fit
```
**Covariance matrix of Region 2**
```{r sigma-matrix}
# get the length of region 2 data
nrow_reg2 <- olive_train %>%
filter(region == 2) %>%
nrow()
# filter the train data to be only have region 2
reg2 <- olive_train %>%
filter(region == 2)
# create the matrix of mu_hat for region 2
mu_hat_2 <- as.matrix(c(olive_qda_fit[["means"]][1], olive_qda_fit[["means"]][3]))
# calculate the covariance matrix for region 2 using the formula specified above.
sum_matrix <- 0
for(i in 1:nrow_reg2){
elem <- (as.matrix(c(reg2$arachidic[i], reg2$linoleic[i])) -
mu_hat_2) %*% t(as.matrix(c(reg2$arachidic[i], reg2$linoleic[i])) -
mu_hat_2)
sum_matrix <- sum_matrix + elem
}
sigma_2 <- (1/(nrow_reg2-1))*sum_matrix
sigma_2
```
**Hence, the discriminant function for Region 2 based on QDA result is:**
$$\delta_{region 2}(x) = -\frac12x^T\begin{bmatrix}0.1786 & -0.0013\\
-0.0013 & 0.1695
\end{bmatrix}^{-1}x + x^T\begin{bmatrix}0.1786 & -0.0013\\
-0.0013 & 0.1695
\end{bmatrix}^{-1}\begin{bmatrix}0.7483 \\
1.0746
\end{bmatrix}- \frac12\begin{bmatrix}0.7483 &
1.0746
\end{bmatrix}\\\begin{bmatrix}0.1786 & -0.0013\\
-0.0013 & 0.1695
\end{bmatrix}^{-1}\begin{bmatrix}0.7483 \\
1.0746
\end{bmatrix} - \frac12 \log{|\begin{bmatrix}0.1786 & -0.0013\\
-0.0013 & 0.1695
\end{bmatrix}|} + \log(0.3952)$$
Or it could be written as follows:
```{r}
sigma2_inv <- Inverse(sigma_2)
sigma2_inv_prod_mu_hat2 <- sigma2_inv %*% mu_hat_2
mu_hat2_trans_prod_sigma2_inv_prod_mu_hat2 <- t(mu_hat_2) %*% sigma2_inv_prod_mu_hat2
log_det_sigma2 <- log(det(sigma_2))
```
$$\delta_{region 2}(x) = -\frac12x^T\begin{bmatrix}5.5980 & 0.0430\\
0.0430 & 5.8992
\end{bmatrix}x + x^T\begin{bmatrix}4.24\\
6.37
\end{bmatrix} - \frac12(10) - \frac12 (-3.4972) - 0.9283$$
or equal to:
$$\delta_{region 2}(x) = -\frac12x^T\begin{bmatrix}5.5980 & 0.0430\\
0.0430 & 5.8992
\end{bmatrix}x + x^T\begin{bmatrix}4.24\\
6.37
\end{bmatrix} - 4.1797 $$
*According to James, Witten, Hastie, and Tibshirani (2013), the QDA could be applied if the assumption that covariance matrix is equal for all of K classes is not satisfied. In other words, unlike Linear Discriminant Analysis (LDA), QDA is more flexible in assuming that covariance matrix for each class is different.*
## Question 1b
**Make similar plots that shows the boundary between the two regions, that would be produced by (i) linear discriminant analysis, (ii) classification tree (using `minsplit` of 10) using the `rpart` engine.**
**Answer**
The plots of boundary could be created using `ggplot` from `ggplot2` (Wickham, 2016). To make the boundary obviously seen, we have to create a grid first and predict the region over that grid.
```{r q1b}
# fit the model of lda
olive_lda_fit <- lda(region ~ arachidic + linoleic, data=olive_train)
# create the grid to show boundary
olive_grid <- expand_grid(arachidic = seq(-2.2, 2.2, 0.05),
linoleic = seq(-2.2, 2.2, 0.05))
# predict region using lda
olive_grid <- olive_grid %>%
mutate(pred = predict(olive_lda_fit, olive_grid)$class)
# fit the classification tree
olive_fit_rp <- rpart(region ~ arachidic + linoleic, data=olive_train,
parms = list(split = "information"),
control = rpart.control(minsplit=10))
# predict region sing classification tree
olive_grid$pred_tree <- predict(olive_fit_rp, newdata=olive_grid, type="class")
```
```{r q1b-plot, fig.cap="Plot A shows the boundary between Region 2 and 3 based on LDA. Plot B shows the boundary between Region 2 and 3 based on classification tree. We can see that some observations are misclassified in both plots. We can also see that there is a slope in LDA boundary, whereas the boundary with classification tree is flat. Hence, we could imply that the decision boundary in LDA still take the two variables into account. Differently, the decision boundary in classification tree only takes the linoleic value into account."}
# create the plot for LDA fit
olive_lda_plot <- ggplot() +
geom_point(data=olive_grid,
aes(x=arachidic, y=linoleic, colour=pred),
size=0.5) +
geom_point(data=olive_train,
aes(x=arachidic, y=linoleic, shape=region),
size = 2, alpha=0.7) +
scale_colour_brewer(palette="Dark2") +
ylab("standardized linoleic") +
xlab("standardized arachidic") +
theme_bw() + theme(aspect.ratio=1,
legend.position = "bottom",
plot.title = element_text(size = 10)) +
ggtitle("A) LDA fit")
# create the plot for rpart fit
olive_rpart_plot <- ggplot() +
geom_point(data=olive_grid,
aes(x=arachidic, y=linoleic, colour=pred_tree),
size=0.5) +
geom_point(data=olive_train,
aes(x=arachidic, y=linoleic, shape=region),
size = 2, alpha=0.7) +
scale_colour_brewer(palette="Dark2") +
ylab("standardized linoleic") +
xlab("standardized arachidic") +
theme_bw() + theme(aspect.ratio=1,
legend.position = "none",
plot.title = element_text(size = 10)) +
ggtitle("B) Classification tree fit")
# combine the twi plots
olive_lda_plot + olive_rpart_plot
```
## Question 1c
**Compute the test balanced accuracy for the three models (i) LDA, (ii) QDA, (iii) classification tree. Are they equally as accurate?**
**Answer**
Balanced accuracy could be calculated with the following formula (Cook, 2021a):
$$balanced\:accuracy = \frac12(sensitivity + specificity)$$
where:
$$sensitivity = a/(a+c)\:(true\: positive)$$
$$specificity = D/(b+d)\:(true\: negative)$$
Hence, based on the formula above, we could first generate the confusion matrix of each model fit and calculate their balanced accuracy.
```{r 1c}
# compute the prediction in test data for the three models an save it into a new data frame.
# compute prediction of lda and qda
olive_test_pred <- olive_test %>%
mutate(pred_lda = predict(olive_lda_fit, olive_test)$class,
pred_qda = predict(olive_qda_fit, olive_test)$class)
# compute prediction for classification tree
olive_test_pred$pred_tree <- predict(olive_fit_rp, newdata=olive_test, type="class")
```
**Balance accuracy for LDA fit**
```{r q1c-lda}
# get the confusion matrix
conf_mat(olive_test_pred, region, pred_lda)
# calculate the sensitivity and specificity
sensi_LDA <- 32/(32+0)
speci_LDA <- 50/(50+0)
# compute the balance accuracy
ba_LDA <- (sensi_LDA + speci_LDA)/2
```
$$sensitivity = 32/(32+0)$$
$$specificity = 50/(50+0)$$
$$balanced\:accuracy = (1+1)/2$$
The balance accuracy for LDA fit is `r ba_LDA`
**Balance accuracy for QDA fit**
```{r q1c-qda}
# get the confusion matrix
conf_mat(olive_test_pred, region, pred_qda)
# calculate the sensitivity and specificity
sensi_qda <- 32/(32+0)
speci_qda <- 50/(50+0)
#calculate balances accuracy
ba_qda <- (sensi_qda + speci_qda)/2
```
$$sensitivity = 32/(32+0)$$
$$specificity = 50/(50+0)$$
$$balanced\:accuracy = (1+1)/2$$
The balance accuracy for QDA fit is `r ba_qda`
**Balance accuracy for Classification Tree fit**
```{r q1c-tree}
# get the confusion matrix
conf_mat(olive_test_pred, region, pred_tree)
#calculate sensitivity and specificity
sensi_tree <- 32/(32+0)
speci_tree <- 50/(50+0)
#calculate balanced accuracy
ba_tree <- (sensi_tree + speci_tree)/2
```
$$sensitivity = 32/(32+0)$$
$$specificity = 50/(50+0)$$
$$balance\:accuracy = (1+1)/2$$
The balance accuracy for classification tree fit is `r ba_tree`
**Since the balance accuracy values for the three models are equal, we can conclude that they are equally as accurate.**
## Question 1d
**Re-fit the three models with the additional variable `oleic` (so you now have three predictors). Write a paragraph discussing how the models change, and why this might be.**
**Answer**
We can see how the model changes based on their predictors. The change of the model could be observed in the following tables.
```{r q1d}
# fit the new lda
olive_lda_fit_new <- lda(region ~ arachidic + linoleic + oleic, data=olive_train)
# fit the new lda
olive_qda_fit_new <- qda(region ~ arachidic + linoleic + oleic, data=olive_train)
# fit the new classification tree
olive_fit_rp_new <- rpart(region ~ arachidic + linoleic + oleic , data=olive_train,
parms = list(split = "information"),
control = rpart.control(minsplit=10))
# compute the new prediction values of the new model.
olive_test_pred <- olive_test %>%
mutate(pred_lda2 = predict(olive_lda_fit_new, olive_test)$class,
pred_qda2 = predict(olive_qda_fit_new, olive_test)$class)
olive_test_pred$pred_tree2 <- predict(olive_fit_rp_new, newdata=olive_test, type="class")
```
**LDA: Old vs New**
```{r results='hide'}
olive_lda_fit
olive_lda_fit_new
```
```{r results='hide'}
# calculate baance accuracy for lda model
confusionMatrix(olive_test_pred$pred_lda2, olive_test_pred$region)
```
```{r q1d-comp-lda}
# create a data frame to show the models' properties
# the LDA1 equation is produced from the model fit
lda_com <- data.frame(model = c("lda(region = arachidic + linoleic)", "lda(region = arachidic + linoleic + oleic)"),
accuracy = c(1, 1),
coeffients = c("LD1 = -0.9183*arachidic - 2.0375*linoleic",
"LD1 = -1.0739*arachidic - 2.8248*linoleic - 0.8577*oleic"))
kable(lda_com, caption = "Comparison between 2 LDA model") %>%
kable_styling()
```
**QDA: Old vs New**
```{r results='hide'}
# calculate balance accuracy for qda
confusionMatrix(olive_test_pred$pred_qda2, olive_test_pred$region)
```
```{r q1d-comp-qda}
qda_com <- data.frame(model = c("qda(region = arachidic + linoleic)", "qda(region = arachidic + linoleic + oleic)"),
accuracy = c(1, 0.99))
kable(qda_com, caption = "Comparison between 2 QDA model") %>%
kable_styling()
```
**Classification Tree: Old vs New**
```{r results='hide'}
olive_fit_rp
olive_fit_rp_new
```
```{r}
tree_com <- data.frame(model = c("rpart(region = arachidic + linoleic)", "rpart(region = arachidic + linoleic + oleic)"),
accuracy = c(1, 1),
rule = c("linoleic>=0.537 classified to region 2",
"linoleic>=0.537 classified to region 2"))
kable(tree_com, caption = "Comparison between 2 classification tree") %>%
kable_styling()
```
**Discussion**
From the tables above, we learn that the coefficients of discriminant space of the LDA model have changed when `oleic` included in the model. However, the contribution of this variable is minor compared to the other variable (only 0.86). Hence the accuracy of the model does not change, i.e., still perfectly accurate to predict the test data. Regarding the classification tree, the model's accuracy also does not change (the accuracy is 1 for the two models). It is because the classification rule does not change either. In these models, `linoleic` is used to decide whether an observation belongs to region 2 or 3. The accuracy of the model only changes in QDA, where adding `oleic` decreases the accuracy by about 1 percent. It is probably because the relationship between `linoleic` and `oleic` is linear, as seen in Figure 2. Hence, QDA does not work pretty well.
```{r q1d-plot, fig.cap="The scatter plot of three variables in the new model. We could see that there is a linear relationship between linoleic and oleic."}
ggscatmat(olive_train, c(2:4), color = "region") +
scale_colour_brewer(palette="Dark2")
```
# Question 2
The `palmerpenguins` is a new R data package, with interesting measurements on penguins of three different species. Subset the data to contain just the Adelie and Gentoo species, and only the variables species and the four physical size measurement variables.
```{r pp-data}
# load the penguin data, filter out Chinstrap and select only 4 variables
penguin <- penguins %>%
dplyr::filter(species != "Chinstrap") %>%
dplyr::select(species, bill_length_mm:body_mass_g)
# drop chinstrap level
penguin <- droplevels(penguin)
# standardize the data
penguins_std <- penguin %>%
mutate_if(is.numeric, std) %>%
drop_na() %>%
rename(bl = bill_length_mm,
bd = bill_depth_mm,
fl = flipper_length_mm,
bm = body_mass_g)
```
## Question 2a
**Make a scatterplot matrix of the data, with species mapped to colour. Which variables would you expect to be the most important for distinguishing between the species? Is it fair to assume homogeneous variance-covariances?**
**Answer**
```{r q2a, fig.cap="The scatterplot matrix of 4 variables used in distinguish Adelie and Gentoo species. The green colour indicates Adelie species, while orange indicates Gentoo. We can see that the relationship between variables pretty linear. Some outliers in each species also could be observed in this plot."}
ggscatmat(penguins_std, columns = 2:5, color="species") +
scale_colour_brewer(palette="Dark2")
```
Figure \@ref(fig:q2a) indicates that bill depth (`bd`) could be the most important variables to distinguish Adelie and Gentoo as this variable could separate the species well. We can see that any variables that are paired with `bd` will show a good separation between the two species.
Yes, it is. If the data have the same shape, means that we can assume that they share a common variance-covariance matrix. Figure \@ref(fig:q2a) shows that the two species have relatively same shape. Thus, we could assume that they have the same variance-covariance.
## Question 2b
**Break the data into training and test sets. Fit an LDA model to the data, assuming equal prior probabilities for the two groups. Report confusion table and the misclassification error for the test set.**
**Answer**
```{r peng-split}
#set the seed
set.seed(775)
# make the split rule
penguin_split <- initial_split(penguins_std, 2/3, strata = species)
# make train data
penguin_train <- analysis(penguin_split)
# make test data
penguin_test <- assessment(penguin_split)
```
**The LDA fit model**
```{r peng-lda}
# set the nengine of lda
lda_mod <- discrim_linear() %>%
set_engine("MASS", prior = c(0.5, 0.5)) %>%
translate()
# fitting the model
penguin_lda_fit <-
lda_mod %>%
fit(species ~ .,
data = penguin_train)
penguin_lda_fit
```
**The confusion matrix**
```{r peng-accuracy}
# create prediction data
penguin_test_pred <- penguin_test %>%
mutate(pred = predict(penguin_lda_fit, penguin_test)$.pred_class)
# generate confusion matrix for test data
conf_mat(penguin_test_pred, species, pred)
```
From the confusion table above, we could see that all of the observations in the test data are correctly classified to Adelie and Gentoo species. Hence, the missclasification error in this case is 0.
## Question 2c
**Write down the LDA rule, for classifying the two species, explicitly being clear about which species is class 1 and which is class 2.**
**Answer**
```{r pooled-cov-var}
# create a matrix of adelie group means
mu_hat_adelie <- as.matrix(c(penguin_lda_fit[["fit"]][["means"]][1],
penguin_lda_fit[["fit"]][["means"]][3],
penguin_lda_fit[["fit"]][["means"]][5],
penguin_lda_fit[["fit"]][["means"]][7]))
# create a matrix of gentoo group means
mu_hat_gentoo <- as.matrix(c(penguin_lda_fit[["fit"]][["means"]][2],
penguin_lda_fit[["fit"]][["means"]][4],
penguin_lda_fit[["fit"]][["means"]][6],
penguin_lda_fit[["fit"]][["means"]][8]))
# filter the data to only have adelie species
adelie <- penguin_train %>%
dplyr::filter(species == "Adelie")
# filter the data to only have gentoo species
gentoo <- penguin_train %>%
dplyr::filter(species == "Gentoo")
# get the variance covariance matrix of adelie class
sum_adelie <- 0
for(i in 1:nrow(adelie)){
elem_adelie <- (as.matrix(c(adelie$bl[i], adelie$bd[i], adelie$fl[i], adelie$bm[i])) -
mu_hat_adelie) %*%
t(as.matrix(c(adelie$bl[i], adelie$bd[i], adelie$fl[i], adelie$bm[i])) -
mu_hat_adelie)
sum_adelie <- sum_adelie + elem_adelie
}
sigma_hat_adelie <- 1/(nrow(adelie) - 1)*sum_adelie
# get the variance covariance matrix of gentoo class
sum_gentoo <- 0
for(i in 1:nrow(gentoo)){
elem_gentoo <- (as.matrix(c(gentoo$bl[i], gentoo$bd[i], gentoo$fl[i], gentoo$bm[i])) -
mu_hat_gentoo) %*%
t(as.matrix(c(gentoo$bl[i], gentoo$bd[i], gentoo$fl[i], gentoo$bm[i])) -
mu_hat_gentoo)
sum_gentoo <- sum_gentoo + elem_gentoo
}
sigma_hat_gentoo <- 1/(nrow(gentoo) - 1)*sum_gentoo
# create the numerator to be fed to pooled variance formula
n_sigma_hat_adelie <- (nrow(adelie)-1)*sigma_hat_adelie
n_sigma_hat_gentoo <- (nrow(gentoo)-1)*sigma_hat_gentoo
# calculate the pooled variance covariance matrix
peng_pooled_cov_var <- (n_sigma_hat_adelie+n_sigma_hat_gentoo)/(nrow(adelie)+nrow(gentoo)-2)
peng_pooled_cov_var_inv <- Inverse(peng_pooled_cov_var)
```
```{r }
# left hand side of the equation
comp_a <- peng_pooled_cov_var_inv %*% (mu_hat_gentoo - mu_hat_adelie)
# right hand side of the equation
comp_b <- 0.5*t(mu_hat_gentoo+mu_hat_adelie)%*%comp_a
```
When a case has 2 classes and the prior probability of each class is equal, the observation is assigned to class 1 according to the equation as follows (Cook, 2021b):
$$x_0^T\Sigma^{-1}(\mu_1-\mu_2) > \frac{1}{2}(\mu_1+\mu_2)^T\Sigma^{-1}(\mu_1-\mu_2)$$
or
$$x_0^T\Sigma^{-1}(\mu_1-\mu_2) - \frac{1}{2}(\mu_1+\mu_2)^T\Sigma^{-1}(\mu_1-\mu_2) > 0$$
Where:
- $\mu_1$ is the group means for class 1
- $\mu_2$ is the group means for class 2
- $\Sigma^{-1}$ is the inverse of the pooled variance-covariance matrix
The formula of pooled variance-covariance matrix is :
$$\hat\Sigma_p = \frac{(n_1-1)\hat\Sigma_1 + (n_2-1)\hat\Sigma_2 + ... + (n_k-1)\hat\Sigma_k}{n_1 + n_2 + ... + n_k -k}$$
For this data set, the class 1 is `gentoo` species and class 2 is `adelie` species. Hence, the LDA rule could be written as follow:
**An observation would be classified as Gentoo if:**
$$x_0^T\begin{bmatrix}5.7659\\
-16.3360\\
11.7811\\
7.5679
\end{bmatrix} - 3.44 > 0$$
Or could be written as:
$$5.7659bl - 16.3360bd + 11.7811fl + 7.5679bm - 3.44 > 0$$
**An observation would be classified as Adelie if:**
$$x_0^T\begin{bmatrix}5.7659\\
-16.336\\
11.7811\\
7.568
\end{bmatrix} - 3.44 <= 0$$
Or:
$$5.7659bl - 16.3360bd + 11.7811fl + 7.5679bm - 3.44 <= 0$$
## Question 2d
**Report the group means and the pooled variance-covariance matrix, and show the computations to obtain the linear discriminant space is computed from these.**
**Answer**
The group means could be obtained from the output of `lda` fit. For this case, the group means is:
```{r}
penguin_lda_fit$fit[3]
```
While the pooled variance-covariance matrix is:
$$\hat\Sigma_p = \begin{bmatrix}0.2776 & 0.1497 & 0.1010 & 0.1760\\
0.1497 & 0.3233 & 0.1057 & 0.2031\\
0.1010 & 0.1057 & 0.1680 & 0.1299\\
0.1760 & 0.2031 & 0.1299 & 0.3170
\end{bmatrix}$$
Further, the computation of discriminant space for this data could be performed by following this formula (Cook, 2021b):
$$\Sigma^{-1}(\mu_1-\mu_2) $$
In this case, the inverse of pooled variance-covariance matrix is:
$$\hat\Sigma^{-1} = \begin{bmatrix}5.9716 & -0.9908 & -1.3115 & -2.1428\\
-0.9908 & 5.4608 & -0.8209 & -2.6125\\
-1.3115 & -0.8209 & 9.2059 & -2.5189\\
-2.1428 & -2.6125 & -2.5189 & 7.0509
\end{bmatrix}$$
And
$$\hat\mu_1-\hat\mu_2 = \begin{bmatrix}1.6768 \\
-1.6361 \\
1.8176 \\
1.6260
\end{bmatrix}$$
In R, the code to obtain this discriminant space is:
```{r q2d, echo = TRUE}
# Discriminant space
disc_space <- peng_pooled_cov_var_inv %*% (mu_hat_gentoo - mu_hat_adelie)
```
Hence, the discriminant space is :
$$\hat\Sigma^{-1}(\hat\mu_1-\hat\mu_2) = \begin{bmatrix}5.7659 \\
-16.3360 \\
11.7811 \\
7.5679
\end{bmatrix}$$
Further, I normalized discriminant vector as shown above to obtain spherical within group covariance matrix as is done in MASS's `lda` function. The code to perform this is displayed as follows:
```{r q2d-normlaized, echo=TRUE}
# in order to have a spherical within groups covariance matirx, the discriminant space should be normalized
v_scalar = sqrt(t(disc_space) %*% peng_pooled_cov_var %*% disc_space)
LD <- disc_space/drop(v_scalar)
```
Finally the result of disctiminant coefficient is:
$$LD = \begin{bmatrix}0.6886 \\
-1.9509 \\
1.4069 \\
0.9038
\end{bmatrix}$$
## Question 2e
**Make a plot showing the data in the discriminant space, to examine how well the species are separated.**
**Answer**
Since we only have 2 classes, we will only have 1 discriminant space. Hence, we could make a dotplot with LD on the x-axis and species on the y-axis then color it by the species.
We can see in Figure \@ref(fig:q2e) that Adelie and Gentoo are well separated. We learn that when LD is negative, the penguins are classified as Adelie. In contrast, when LD is positive, the Penguins are classified as Gentoo.
```{r q2e, fig.cap="Adelie and Gentoo distributed in discriminant space. We can see that the two species are well-separated."}
# compute the value of observations in discriminant space
penguin_tr_dic <- penguin_train %>%
mutate(LD = (LD[1]*bl) +
(LD[2]*bd) +
(LD[3]*fl) +
(LD[4]*bm))
# make the plot showing the data in discriminant space
ggplot(penguin_tr_dic) +
geom_point(aes(x = LD, y = species, color = species)) +
theme(aspect.ratio=1) +
scale_color_brewer(palette="Dark2")
```
## Question 2f
**Return to the data with the original three species of penguins. Conduct LDA for the three groups, and obtain the 2D discriminant space, using just the same four physical measurements. Using the tour, determine which of the variables are most important for distinguishing Chinstrap penguins from Adelie. Provide a plot or two to support your decision.**
**Answer**
**LDA for the three species and its discriminant space**
```{r q2f}
# put back Chinstrap to data set
penguin_new <- penguins %>%
dplyr::select(species, bill_length_mm:body_mass_g)
penguin_new$species <- factor(penguin_new$species, levels = c("Adelie", "Gentoo", "Chinstrap"))
# standardize the data
penguins_std_new <- penguin_new %>%
mutate_if(is.numeric, std) %>%
drop_na() %>%
rename(bl = bill_length_mm,
bd = bill_depth_mm,
fl = flipper_length_mm,
bm = body_mass_g)
## break the data into train and test
#set the seed
set.seed(775)
# make the split rule
penguin_split_new <- initial_split(penguins_std_new, 2/3, strata = species)
# make train data
penguin_train_new <- analysis(penguin_split_new)
# make test data
penguin_test_new <- assessment(penguin_split_new)
## doing lda
penguin_lda_new_fit <-
lda(species ~ .,
data = penguin_train_new,
prior = c(1,1,1)/3)
## create the plot of 2D LDA
penguin_lda_space <- bind_cols(penguin_train_new,
as_tibble(predict(penguin_lda_new_fit, penguin_train_new)$x))
penguin_lda_new_fit
```
From the output above, we obtain the discriminant vector of LD1 and LD2 shown by the coefficients of linear discriminant. Hence, we can make the 2D plot of the discriminant space as follows:
```{r q2f-plot, fig.cap="Linear discriminant space plotted in 2D dimension. Gentoo onservations are more distinguishable compared to the other species."}
ggplot(penguin_lda_space) +
geom_point(aes(x = LD1,
y = LD2,
color = species)) +
theme(aspect.ratio=1) +
scale_color_brewer(palette="Dark2")
```
We can learn from Figure \@ref(fig:q2f-plot) that LD1 separates the Gentoo with the two species very well. Adelie and Chinstrap are mixed up in LD1 space, while we can see they are separated in LD2 space.
**Play the manual tour**
In order to know what variable that separate Adelie and Chinstrap very well, we can play the manual tour as is seen in Figure \@ref(fig:manual-tour).
```{r manual-tour, fig.cap="Visualisation of four predictor variables. By clicking the play button, we can see how the observations move in the space. In this plot, Gentoo, Adelie, and Chinstrap are displayed in the Orang, Green, and Purple dots respectively. We learn that 'bd' separates Gentoo from Adelie and Chinstrap. On the other axis, 'bl' separates Adelie and Chinstrap."}
bas <- penguin_lda_new_fit[["scaling"]]
play_manual_tour(bas, penguins_std_new[,2:5], 2,
aes_args = list(color = penguins_std_new$species))
```
> From Figure \@ref(fig:manual-tour) we learn that Bill Length (BL) is the most important variable that distinguish Chinstrap from Adelie.
# Question 3
Impurity metrics can be other than Gini or entropy. This is a metric proposed by [Buja and Lee (2001)](https://repository.upenn.edu/cgi/viewcontent.cgi?article=1406&context=statistics_papers) is called a one-sided extreme:
$$OSE = 1-max(\hat{p}_L^1, \hat{p}_R^1)$$
## Question 3a
**Read in the spam data, set the levels for day of the week, and filter to the five most common domains, "com", "edu", "net", "org", "gov". Drop variable spampct, because there are many missing values. (This variable was one that some of the mail software provided as a probability that the email was spam. Most mail software didn't provide this.) Class 1 for this data would be `spam=no`. Explain why.**
**Answer**
```{r load-spam-data}
spam <- read_csv("http://ggobi.org/book/data/spam.csv") %>%
mutate(`day of week` = factor(`day of week`,
levels=c("Mon", "Tue", "Wed", "Thu",
"Fri", "Sat", "Sun"))) %>%
filter(domain %in% c("com", "edu", "net", "org", "gov")) %>%
dplyr::select(-spampct)
```
**Class 1 for this data would be `spam=no` because we want to build a spam filter. Hence our group of interest is the emails that are not spam.**
## Question 3b
**Considering only the domain variable, what would be all of the possible splits? Compute OSE for each possible split, and report the best split, by hand.**
**Answer**
```{r q3b}
# select only domain and spam column
dom <- spam %>%
dplyr::select(domain, spam) %>%
mutate(domain = factor(domain, levels = c("com", "edu", "net", "org", "gov")),
spam = ifelse(spam == "no", 1, 0)) %>%
mutate(spam = factor(spam, levels = c(1, 0)))
# group the data based on domain and spam
dom_cl <- dom %>%
group_by(domain, spam) %>%
count()
```
The number of possible splits of classification tree with categorical predictors are $2^{k-1} -1$ (Hoare, n.d.). Hence, if we plug our data into this criteria, since we have 5 class, the number of possible split would be $2^{4} -1$ = 15. They are:
1. split 1: com -- edu, net, org, gov
2. split 2: edu -- com, net, org, gov
3. split 3: net -- com, edu, org, gov
4. split 4: org -- com, edu, net, gov
5. split 5: gov -- com, edu, net, org
6. split 6: com, edu -- net, org, gov
7. split 7: com, net -- edu, org, gov
8. split 8: com, org -- edu, net, gov
9. split 9: com, gov -- edu, net, org
10. split 10: edu, net -- com, org, gov
11. split 11: edu, org -- com, net, gov
12. split 12: edu, gov -- com, net, org
13. split 13: net, org -- com, edu, gov
14. split 14: net, gov -- com, edu, org
15. split 15: org, gov -- com, edu, net
Before creating the possible splits and computing the OSE, I split the data into each class using `split` function (R Core Team, 2020). After that, I made a function to create the left and right bucket of the data as displayed as follows:
```{r q3b-1, echo = TRUE}
# split the data
dom_split <- split(dom_cl, dom_cl$domain)
# make a function to make data frame for every split
Y <- lapply(seq_along(dom_split), function(x) as.data.frame(dom_split[[x]])[, 1:3])
# create the data frame
com <- Y[[1]]
edu <- Y[[2]]
net <- Y[[3]]
org <- Y[[4]]
gov <- Y[[5]]
```
```{r echo = TRUE}
# function to bind the data into each bucket
get_bind <- function(...){
rbind(...) %>%
mutate(sum_email = sum(n)) %>%
filter(spam == 1)
}
```
Next, the OSE for each split could be calculated by hand as shown as follows. Note that for the lengthy of the report, I will just show some sample code of the calculation. The rest code is available in the Rmd file of this report.
```{r echo = TRUE}
# **Split 1 : com vs edu, net, org, gov**
# left bucket is com
# right bucket is
rb_1 <- get_bind(edu, net, org, gov)
# probability in left and right bucket
pl_s1 <- com$n[1]/sum(com$n)
pr_s1 <- sum(rb_1$n)/rb_1$sum_email[1]
# compute OSE
OSE1 <- 1 - max(pl_s1, pr_s1)
```
```{r echo=TRUE}
# **Split 2 : edu vs com, net, org, gov**
# left bucket is edu
# right bucket is
rb_2 <- get_bind(com, net, org, gov)
# probability in left and right bucket
pl_s2 <- edu$n[1]/sum(edu$n)
pr_s2 <- sum(rb_2$n)/rb_2$sum_email[1]
# compute OSE
OSE2 <- 1 - max(pl_s2, pr_s2)
```
```{r}
# **Split 3: net vs com, edu, org, gov**
# left bucket is net
# right bucket is
rb_3 <- get_bind(com, edu, org, gov)