Homework 4 (Final project)

DATA INITIAL SUMMARY

DATA SOURCE: https://www.kaggle.com/datasets/rounakbanik/pokemon

LABELS

ARTS & Entertainment - Earth and Nature - Games - Video Games - Anime - Pop Culture

Data Source

Activity Overview activity stats Views 307518 Downloads 41644 Download per view ratio 0.14 Total unique contributors 157

Description

Context

This dataset contains information on all 802 Pokemon from all Seven Generations of Pokemon. The information contained in this dataset include Base Stats, Performance against Other Types, Height, Weight, Classification, Egg Steps, Experience Points, Abilities, etc. The information was scraped from http://serebii.net/

DATA EXPLORATION

pkm_df <- read.csv("https://raw.githubusercontent.com/johnm1990/DATA622/main/pokemon.csv")

head(pkm_df)
##                     abilities against_bug against_dark against_dragon
## 1 ['Overgrow', 'Chlorophyll']        1.00            1              1
## 2 ['Overgrow', 'Chlorophyll']        1.00            1              1
## 3 ['Overgrow', 'Chlorophyll']        1.00            1              1
## 4    ['Blaze', 'Solar Power']        0.50            1              1
## 5    ['Blaze', 'Solar Power']        0.50            1              1
## 6    ['Blaze', 'Solar Power']        0.25            1              1
##   against_electric against_fairy against_fight against_fire against_flying
## 1              0.5           0.5           0.5          2.0              2
## 2              0.5           0.5           0.5          2.0              2
## 3              0.5           0.5           0.5          2.0              2
## 4              1.0           0.5           1.0          0.5              1
## 5              1.0           0.5           1.0          0.5              1
## 6              2.0           0.5           0.5          0.5              1
##   against_ghost against_grass against_ground against_ice against_normal
## 1             1          0.25              1         2.0              1
## 2             1          0.25              1         2.0              1
## 3             1          0.25              1         2.0              1
## 4             1          0.50              2         0.5              1
## 5             1          0.50              2         0.5              1
## 6             1          0.25              0         1.0              1
##   against_poison against_psychic against_rock against_steel against_water
## 1              1               2            1           1.0           0.5
## 2              1               2            1           1.0           0.5
## 3              1               2            1           1.0           0.5
## 4              1               1            2           0.5           2.0
## 5              1               1            2           0.5           2.0
## 6              1               1            4           0.5           2.0
##   attack base_egg_steps base_happiness base_total capture_rate   classfication
## 1     49           5120             70        318           45   Seed Pokémon
## 2     62           5120             70        405           45   Seed Pokémon
## 3    100           5120             70        625           45   Seed Pokémon
## 4     52           5120             70        309           45 Lizard Pokémon
## 5     64           5120             70        405           45  Flame Pokémon
## 6    104           5120             70        634           45  Flame Pokémon
##   defense experience_growth height_m hp              japanese_name       name
## 1      49           1059860      0.7 45 Fushigidaneフシギãƒ\200ãƒ\215  Bulbasaur
## 2      63           1059860      1.0 60  Fushigisouフシギソウ    Ivysaur
## 3     123           1059860      2.0 80 Fushigibanaフシギãƒ\220ナ   Venusaur
## 4      43           1059860      0.6 39       Hitokageヒãƒ\210カゲ Charmander
## 5      58           1059860      1.1 58        Lizardoリザード Charmeleon
## 6      78           1059860      1.7 78    Lizardonリザードン  Charizard
##   percentage_male pokedex_number sp_attack sp_defense speed type1  type2
## 1            88.1              1        65         65    45 grass poison
## 2            88.1              2        80         80    60 grass poison
## 3            88.1              3       122        120    80 grass poison
## 4            88.1              4        60         50    65  fire       
## 5            88.1              5        80         65    80  fire       
## 6            88.1              6       159        115   100  fire flying
##   weight_kg generation is_legendary
## 1       6.9          1            0
## 2      13.0          1            0
## 3     100.0          1            0
## 4       8.5          1            0
## 5      19.0          1            0
## 6      90.5          1            0

DATA PREPARATION

glimpse(pkm_df)
## Rows: 801
## Columns: 41
## $ abilities         <chr> "['Overgrow', 'Chlorophyll']", "['Overgrow', 'Chloro~
## $ against_bug       <dbl> 1.00, 1.00, 1.00, 0.50, 0.50, 0.25, 1.00, 1.00, 1.00~
## $ against_dark      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ against_dragon    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ against_electric  <dbl> 0.5, 0.5, 0.5, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.~
## $ against_fairy     <dbl> 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.~
## $ against_fight     <dbl> 0.50, 0.50, 0.50, 1.00, 1.00, 0.50, 1.00, 1.00, 1.00~
## $ against_fire      <dbl> 2.0, 2.0, 2.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 2.0, 2.~
## $ against_flying    <dbl> 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.~
## $ against_ghost     <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0~
## $ against_grass     <dbl> 0.25, 0.25, 0.25, 0.50, 0.50, 0.25, 2.00, 2.00, 2.00~
## $ against_ground    <dbl> 1.0, 1.0, 1.0, 2.0, 2.0, 0.0, 1.0, 1.0, 1.0, 0.5, 0.~
## $ against_ice       <dbl> 2.0, 2.0, 2.0, 0.5, 0.5, 1.0, 0.5, 0.5, 0.5, 1.0, 1.~
## $ against_normal    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ against_poison    <dbl> 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.~
## $ against_psychic   <dbl> 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1~
## $ against_rock      <dbl> 1, 1, 1, 2, 2, 4, 1, 1, 1, 2, 2, 4, 2, 2, 2, 2, 2, 2~
## $ against_steel     <dbl> 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1.0, 1.~
## $ against_water     <dbl> 0.5, 0.5, 0.5, 2.0, 2.0, 2.0, 0.5, 0.5, 0.5, 1.0, 1.~
## $ attack            <int> 49, 62, 100, 52, 64, 104, 48, 63, 103, 30, 20, 45, 3~
## $ base_egg_steps    <int> 5120, 5120, 5120, 5120, 5120, 5120, 5120, 5120, 5120~
## $ base_happiness    <int> 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, ~
## $ base_total        <int> 318, 405, 625, 309, 405, 634, 314, 405, 630, 195, 20~
## $ capture_rate      <chr> "45", "45", "45", "45", "45", "45", "45", "45", "45"~
## $ classfication     <chr> "Seed Pokémon", "Seed Pokémon", "Seed Pokémon", "~
## $ defense           <int> 49, 63, 123, 43, 58, 78, 65, 80, 120, 35, 55, 50, 30~
## $ experience_growth <int> 1059860, 1059860, 1059860, 1059860, 1059860, 1059860~
## $ height_m          <dbl> 0.7, 1.0, 2.0, 0.6, 1.1, 1.7, 0.5, 1.0, 1.6, 0.3, 0.~
## $ hp                <int> 45, 60, 80, 39, 58, 78, 44, 59, 79, 45, 50, 60, 40, ~
## $ japanese_name     <chr> "Fushigidaneフシギãƒ\200ãƒ\215", "Fushigisouフã‚~
## $ name              <chr> "Bulbasaur", "Ivysaur", "Venusaur", "Charmander", "C~
## $ percentage_male   <dbl> 88.1, 88.1, 88.1, 88.1, 88.1, 88.1, 88.1, 88.1, 88.1~
## $ pokedex_number    <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1~
## $ sp_attack         <int> 65, 80, 122, 60, 80, 159, 50, 65, 135, 20, 25, 90, 2~
## $ sp_defense        <int> 65, 80, 120, 50, 65, 115, 64, 80, 115, 20, 25, 80, 2~
## $ speed             <int> 45, 60, 80, 65, 80, 100, 43, 58, 78, 45, 30, 70, 50,~
## $ type1             <chr> "grass", "grass", "grass", "fire", "fire", "fire", "~
## $ type2             <chr> "poison", "poison", "poison", "", "", "flying", "", ~
## $ weight_kg         <dbl> 6.9, 13.0, 100.0, 8.5, 19.0, 90.5, 9.0, 22.5, 85.5, ~
## $ generation        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ is_legendary      <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~

Pre-processing to identify legendary by logical

pkm_df <- pkm_df %>% mutate(is_legendary = as.factor(if_else(is_legendary == 1, 
    "yes", "no")))

We can now check the levels as needed We check for NA

levels(pkm_df$capture_rate)
## NULL
pkm_df <- pkm_df %>% mutate(capture_rate = if_else(capture_rate == "30 (Meteorite)255 (Core)",
    30, as.numeric(capture_rate)), generation = as.factor(generation))
## Warning in replace_with(out, !condition, false, fmt_args(~false), glue("length
## of {fmt_args(~condition)}")): NAs introduced by coercion
colSums(is.na(pkm_df))
##         abilities       against_bug      against_dark    against_dragon 
##                 0                 0                 0                 0 
##  against_electric     against_fairy     against_fight      against_fire 
##                 0                 0                 0                 0 
##    against_flying     against_ghost     against_grass    against_ground 
##                 0                 0                 0                 0 
##       against_ice    against_normal    against_poison   against_psychic 
##                 0                 0                 0                 0 
##      against_rock     against_steel     against_water            attack 
##                 0                 0                 0                 0 
##    base_egg_steps    base_happiness        base_total      capture_rate 
##                 0                 0                 0                 0 
##     classfication           defense experience_growth          height_m 
##                 0                 0                 0                20 
##                hp     japanese_name              name   percentage_male 
##                 0                 0                 0                98 
##    pokedex_number         sp_attack        sp_defense             speed 
##                 0                 0                 0                 0 
##             type1             type2         weight_kg        generation 
##                 0                 0                20                 0 
##      is_legendary 
##                 0

Since we see perce_male variable has a few missing value, we will remove them in order to conserver data.

pkm_df2 <- pkm_df %>% select_if(~is.numeric(.)) %>% select(-c(pokedex_number, percentage_male)) %>% 
    mutate(legendary = pkm_df$is_legendary, name = pkm_df$name) %>% na.omit()
pkm_df1 <- pkm_df2 %>% select(-c(name, legendary))
ggplot(pkm_df, aes(base_egg_steps, base_total, color = pkm_df$legendary, size = capture_rate)) + 
    geom_point(alpha = 0.5) + theme_minimal()
## Warning: Use of `pkm_df$legendary` is discouraged. Use `legendary` instead.

pkm_df %>% filter(is_legendary == "no") %>% group_by(type1, type2) %>% summarise(base = mean(base_total)) %>% 
    ggplot(aes(type1, type2, fill = base)) + scale_fill_viridis_c(option = "B") + 
    geom_tile(color = "yellow") + theme_minimal()
## `summarise()` has grouped output by 'type1'. You can override using the `.groups` argument.

p1 <- ggplot(pkm_df, aes(is_legendary, height_m, fill = is_legendary)) + geom_boxplot(show.legend = F) + 
    theme_minimal() + labs(title = "Height")

p2 <- ggplot(pkm_df, aes(is_legendary, weight_kg, fill = is_legendary)) + geom_boxplot(show.legend = F) + 
    theme_minimal() + labs(title = "Weight")

p3 <- ggplot(pkm_df, aes(is_legendary, speed, fill = is_legendary)) + geom_boxplot(show.legend = F) + 
    theme_minimal() + labs(title = "Speed")

p4 <- ggplot(pkm_df, aes(is_legendary, hp, fill = is_legendary)) + geom_boxplot() + 
    theme_minimal() + theme(legend.position = "bottom") + labs(title = "Health Point (HP)")

plot_grid(p1, p2, p3, p4)
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

K-MEANS CLUSTERING

using KMEANS method

set.seed(111)
km <- kmeans(pkm_df1, centers = 4)
head(km)
## $cluster
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  21  22 
##   1   1   1   1   1   1   1   1   1   3   3   3   3   3   3   1   1   1   3   3 
##  23  24  25  29  30  31  32  33  34  35  36  39  40  41  42  43  44  45  46  47 
##   3   3   3   1   1   1   1   1   1   2   2   2   2   3   3   1   1   1   3   3 
##  48  49  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71 
##   3   3   3   3   3   3   4   4   1   1   1   1   1   1   1   1   1   1   1   1 
##  72  73  77  78  79  80  81  82  83  84  85  86  87  90  91  92  93  94  95  96 
##   4   4   3   3   3   3   3   3   3   3   3   3   3   4   4   1   1   1   3   3 
##  97  98  99 100 101 102 104 106 107 108 109 110 111 112 113 114 115 116 117 118 
##   3   3   3   3   3   4   3   3   3   3   3   3   4   4   2   3   3   3   3   3 
## 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 
##   3   4   4   3   3   3   3   3   4   4   4   4   4   3   3   3   3   3   3   3 
## 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 
##   3   3   3   4   4   4   4   4   4   4   4   4   1   1   1   1   1   1   1   1 
## 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 
##   1   1   3   3   3   3   2   2   2   2   3   4   4   3   2   2   2   2   3   3 
## 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 
##   1   1   1   1   2   2   3   1   1   1   1   2   1   1   3   3   3   3   3   1 
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 
##   3   2   3   3   3   3   3   3   1   3   2   2   3   3   1   4   1   3   3   3 
## 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 
##   3   4   4   2   3   3   2   4   4   4   4   3   3   3   3   4   2   3   3   3 
## 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 
##   3   3   4   2   4   4   4   4   4   4   4   4   1   1   1   1   1   1   1   1 
## 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 
##   1   1   3   3   3   3   3   3   3   3   3   1   1   1   1   1   1   1   1   3 
## 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 
##   3   4   4   4   3   3   4   4   4   4   4   2   2   2   1   1   1   4   4   2 
## 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 
##   3   2   2   1   2   4   4   4   3   3   4   4   3   3   2   4   1   4   4   4 
## 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 
##   4   4   4   3   3   3   2   2   2   1   1   1   1   1   2   2   2   4   2   2 
## 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 
##   3   3   4   4   3   3   2   2   2   2   2   2   3   1   2   2   2   2   4   2 
## 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 
##   1   3   3   3   1   1   1   2   2   2   4   2   4   4   4   4   4   4   4   4 
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 
##   4   4   4   4   4   4   4   4   1   1   1   1   1   1   1   1   1   1   1   1 
## 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 
##   3   3   1   1   1   1   1   1   1   2   2   2   2   3   3   3   1   1   3   3 
## 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 
##   3   3   3   3   3   2   4   4   3   3   2   1   2   2   2   3   3   3   3   3 
## 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 
##   3   2   1   3   4   4   4   4   1   1   4   4   4   4   3   3   4   2   2   4 
## 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 
##   4   4   1   3   3   4   3   3   3   2   3   3   3   1   4   3   4   3   2   3 
## 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 
##   3   4   4   4   4   4   4   4   4   4   4   4   4   1   4   4   1   1   1   1 
## 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 
##   1   1   1   1   1   3   3   1   1   1   3   3   3   3   3   3   3   3   2   2 
## 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 
##   1   1   1   3   3   1   1   1   3   3   3   3   2   1   1   1   1   1   1   3 
## 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 
##   3   1   1   1   1   1   1   3   3   3   3   3   1   1   1   1   1   3   3   3 
## 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 
##   3   3   3   3   3   3   3   3   3   3   3   1   1   2   2   1   1   1   1   1 
## 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 
##   1   3   3   4   4   4   3   3   3   3   3   3   3   3   3   2   3   3   3   3 
## 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 
##   1   1   1   4   4   4   3   3   1   1   1   4   4   4   3   3   3   3   3   3 
## 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 
##   1   1   3   3   3   3   3   3   4   4   4   4   3   3   4   4   4   4   4   4 
## 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 
##   4   4   4   4   4   4   4   4   4   4   4   1   1   1   1   1   1   1   1   1 
## 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 
##   3   3   1   1   1   3   3   3   1   1   3   3   3   3   3   3   3   3   3   3 
## 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 
##   3   3   3   3   3   3   3   3   3   3   3   3   3   4   4   3   3   3   3   3 
## 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 
##   3   3   3   3   4   4   4   4   2   3   3   3   3   3   3   3   3   4   4   4 
## 719 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 
##   4   4   1   1   1   1   1   1   1   1   1   3   3   3   3   3   3   3   3   3 
## 740 741 742 743 744 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 
##   3   3   3   3   3   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3 
## 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 
##   1   1   1   2   4   4   3   3   3   3   2   4   4   1   4   3   3   3   3   3 
## 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 
##   3   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
## 801 
##   4 
## 
## $centers
##   against_bug against_dark against_dragon against_electric against_fairy
## 1   1.0954774     1.010050      0.9924623         1.042714      1.012563
## 2   0.8910256     1.096154      0.7820513         1.243590      0.974359
## 3   0.9601563     1.067969      0.9406250         1.092188      1.035937
## 4   0.9904891     1.057065      1.0679348         1.061141      1.245924
##   against_fight against_fire against_flying against_ghost against_grass
## 1      1.011307     1.183417       1.381910     0.9472362     0.9120603
## 2      1.048077     1.016026       1.038462     0.9487179     1.1474359
## 3      1.035937     1.216406       1.197656     0.9875000     1.0445312
## 4      1.160326     1.019022       1.074728     1.0353261     1.0353261
##   against_ground against_ice against_normal against_poison against_psychic
## 1       1.013819    1.331658      0.9346734      1.0816583       1.0854271
## 2       1.044872    1.038462      0.8076923      1.0256410       0.9487179
## 3       1.110156    1.132031      0.8757812      0.9523438       1.0203125
## 4       1.154891    1.258152      0.8926630      0.9076087       0.9116848
##   against_rock against_steel against_water   attack base_egg_steps
## 1     1.282663     0.8580402      1.016332 74.82412       5274.372
## 2     1.256410     1.2115385      1.025641 63.43590       5120.000
## 3     1.281250     0.9812500      1.044531 73.19375       5196.000
## 4     1.197011     1.0108696      1.058424 94.66848      13753.043
##   base_happiness base_total capture_rate  defense experience_growth  height_m
## 1       69.02010   410.1558     95.10050 65.90955         1059860.0 0.9442211
## 2       72.62821   393.9487    110.38462 67.87179          743589.7 0.9269231
## 3       69.53125   404.4094    116.79063 70.97500         1000000.0 1.0165625
## 4       50.40761   506.0489     62.58696 86.37500         1279673.9 1.7581522
##         hp sp_attack sp_defense    speed weight_kg
## 1 64.41709  72.97487   66.00503 66.02513  34.36985
## 2 69.60256  60.84615   74.33333 57.85897  29.45769
## 3 64.50000  66.01875   66.92500 62.79688  48.14000
## 4 82.15761  84.51630   82.15217 76.17935 127.14239
## 
## $totss
## [1] 2.045757e+13
## 
## $withinss
## [1] 2.051105e+09 6.320885e+11 4.603243e+08 1.988054e+12
## 
## $tot.withinss
## [1] 2.622654e+12
## 
## $betweenss
## [1] 1.783492e+13

PCA

poke_pca <- PCA(pkm_df2 %>% select(-name), scale.unit = T, ncp = 31, graph = F, 
    quali.sup = 32)

summary(poke_pca)
## 
## Call:
## PCA(X = pkm_df2 %>% select(-name), scale.unit = T, ncp = 31,  
##      quali.sup = 32, graph = F) 
## 
## 
## Eigenvalues
##                        Dim.1   Dim.2   Dim.3   Dim.4   Dim.5   Dim.6   Dim.7
## Variance               5.747   3.540   2.572   2.179   1.921   1.765   1.385
## % of var.             18.538  11.419   8.296   7.028   6.198   5.692   4.469
## Cumulative % of var.  18.538  29.957  38.254  45.282  51.480  57.172  61.641
##                        Dim.8   Dim.9  Dim.10  Dim.11  Dim.12  Dim.13  Dim.14
## Variance               1.324   1.249   1.103   0.998   0.793   0.756   0.666
## % of var.              4.270   4.028   3.557   3.220   2.557   2.438   2.149
## Cumulative % of var.  65.911  69.939  73.496  76.716  79.273  81.711  83.860
##                       Dim.15  Dim.16  Dim.17  Dim.18  Dim.19  Dim.20  Dim.21
## Variance               0.634   0.586   0.522   0.456   0.427   0.397   0.326
## % of var.              2.045   1.890   1.683   1.470   1.377   1.281   1.052
## Cumulative % of var.  85.905  87.795  89.479  90.949  92.326  93.607  94.659
##                       Dim.22  Dim.23  Dim.24  Dim.25  Dim.26  Dim.27  Dim.28
## Variance               0.321   0.305   0.257   0.224   0.189   0.145   0.088
## % of var.              1.035   0.983   0.829   0.723   0.608   0.467   0.283
## Cumulative % of var.  95.695  96.678  97.507  98.230  98.839  99.305  99.589
##                       Dim.29  Dim.30  Dim.31
## Variance               0.069   0.058   0.000
## % of var.              0.223   0.188   0.000
## Cumulative % of var.  99.812 100.000 100.000
## 
## Individuals (the 10 first)
##                       Dist    Dim.1    ctr   cos2    Dim.2    ctr   cos2  
## 1                 |  4.247 | -2.048  0.093  0.233 |  1.493  0.081  0.124 |
## 2                 |  3.850 | -0.926  0.019  0.058 |  1.735  0.109  0.203 |
## 3                 |  5.132 |  2.180  0.106  0.181 |  2.173  0.171  0.179 |
## 4                 |  4.058 | -1.563  0.054  0.148 | -1.664  0.100  0.168 |
## 5                 |  3.538 | -0.268  0.002  0.006 | -1.385  0.069  0.153 |
## 6                 |  6.684 |  2.348  0.123  0.123 |  1.093  0.043  0.027 |
## 7                 |  3.573 | -1.558  0.054  0.190 | -0.994  0.036  0.077 |
## 8                 |  3.013 | -0.316  0.002  0.011 | -0.736  0.020  0.060 |
## 9                 |  4.572 |  2.680  0.160  0.343 | -0.193  0.001  0.002 |
## 10                |  5.183 | -4.493  0.450  0.751 |  0.813  0.024  0.025 |
##                    Dim.3    ctr   cos2  
## 1                 -1.005  0.050  0.056 |
## 2                 -1.088  0.059  0.080 |
## 3                 -1.318  0.087  0.066 |
## 4                 -0.401  0.008  0.010 |
## 5                 -0.488  0.012  0.019 |
## 6                 -2.080  0.215  0.097 |
## 7                  0.228  0.003  0.004 |
## 8                  0.158  0.001  0.003 |
## 9                 -0.186  0.002  0.002 |
## 10                -0.516  0.013  0.010 |
## 
## Variables (the 10 first)
##                      Dim.1    ctr   cos2    Dim.2    ctr   cos2    Dim.3    ctr
## against_bug       | -0.021  0.008  0.000 |  0.309  2.699  0.096 |  0.068  0.179
## against_dark      |  0.123  0.262  0.015 | -0.181  0.928  0.033 | -0.727 20.578
## against_dragon    |  0.170  0.500  0.029 |  0.247  1.717  0.061 |  0.200  1.558
## against_electric  | -0.074  0.097  0.006 | -0.025  0.017  0.001 | -0.118  0.541
## against_fairy     |  0.147  0.376  0.022 |  0.382  4.129  0.146 |  0.509 10.062
## against_fight     |  0.146  0.369  0.021 | -0.329  3.049  0.108 |  0.712 19.731
## against_fire      | -0.161  0.449  0.026 |  0.390  4.300  0.152 | -0.268  2.797
## against_flying    | -0.251  1.094  0.063 |  0.702 13.934  0.493 | -0.080  0.250
## against_ghost     |  0.152  0.403  0.023 | -0.116  0.381  0.013 | -0.755 22.185
## against_grass     |  0.078  0.106  0.006 | -0.491  6.821  0.241 |  0.291  3.302
##                     cos2  
## against_bug        0.005 |
## against_dark       0.529 |
## against_dragon     0.040 |
## against_electric   0.014 |
## against_fairy      0.259 |
## against_fight      0.507 |
## against_fire       0.072 |
## against_flying     0.006 |
## against_ghost      0.571 |
## against_grass      0.085 |
## 
## Supplementary categories
##                        Dist     Dim.1    cos2  v.test     Dim.2    cos2  v.test
## no                |   0.479 |  -0.432   0.813 -16.157 |  -0.086   0.033  -4.120
## yes               |   4.941 |   4.455   0.813  16.157 |   0.892   0.033   4.120
##                       Dim.3    cos2  v.test  
## no                |   0.054   0.013   3.005 |
## yes               |  -0.554   0.013  -3.005 |
# FIT KMEANS FOR PKM
fit <- kmeans(pkm_df1[,-1], 3, iter.max=1000)

#capture rate barplot
barplot(table(pkm_df1$capture_rate), col="#336699") #plot

#pca <- prcomp(pkm_df1[,-1], scale=TRUE) #principle component analysis
pca_data <- mutate(fortify(pkm_df1), col=fit$cluster)
#We want to examine the cluster memberships for each #observation - see last column

ggplot(pca_data) + geom_point(aes(x=speed, y=height_m, fill=factor(col)),
size=3, col="#7f7f7f", shape=21) + theme_bw(base_family="Helvetica")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

NICE CLUSTER VISUALIZATION OF X AXIS OF SPEED AND Y-AXIS OF HEIGHT_M

PCA Visualize variance

fviz_eig(poke_pca, ncp = 15, addlabels = T, main = "Variance explained by each dimensions")

This is so you can see that we can segment/extr. the values of PC1 to PC13 for new data frame. If needed can be used to demonstrate supervised learning

df_pca <- data.frame(poke_pca$ind$coord[, 1:13]) %>% bind_cols(cluster = as.factor(km$cluster)) %>% 
    select(cluster, 1:13)
head(df_pca)
##   cluster      Dim.1     Dim.2      Dim.3        Dim.4       Dim.5      Dim.6
## 1       1 -2.0482689  1.492719 -1.0045448  0.286136546  1.26207290 -1.4101871
## 2       1 -0.9261837  1.735385 -1.0879066  0.004112326  1.49529970 -1.0978834
## 3       1  2.1803303  2.173474 -1.3182468 -0.435886223  2.32994924 -0.4853144
## 4       1 -1.5633020 -1.663909 -0.4007239 -1.503710041 -0.26792880 -0.6871526
## 5       1 -0.2678436 -1.385088 -0.4884444 -1.814505168 -0.02738300 -0.3548355
## 6       1  2.3480258  1.093030 -2.0803618 -4.421165007 -0.09279685  0.3653839
##        Dim.7       Dim.8      Dim.9      Dim.10    Dim.11     Dim.12
## 1  1.2486080 -0.65081332 -0.4836803  0.07307885 0.3195961 -0.5758634
## 2  1.4996984 -0.49281827 -0.5863074 -0.10586181 0.3463365 -0.5161645
## 3  2.0748878 -0.31191269 -0.2724118 -0.80905749 0.4415624 -0.7678341
## 4 -0.5432244 -0.06761517 -1.7412878  0.58487637 0.5462090 -0.5795146
## 5 -0.2712232  0.09403477 -1.8069230  0.43192314 0.7010062 -0.5202674
## 6 -1.5763563  1.60696746 -0.7618889 -0.34817371 0.8695344 -0.4785768
##        Dim.13
## 1 -0.02173397
## 2 -0.10239078
## 3  0.09489559
## 4  0.73142011
## 5  0.57696354
## 6  1.01798043
theme_set(theme_bw())

h.pcaScores <- data.frame(df_pca[, 1:3]) # we only need the first two principal components
ggplot(h.pcaScores, aes(y = Dim.1, x = Dim.2)) + geom_point(col = 'tomato2')

From a set of p=7 features, we have now plotted our pkm data into a low-dimensional scatter plot where essentially p=2. You can see some clusters within this scatter plot, although they are not too apparent.

TREES

CLASSIFICATION

pkm_df_x = pkm_df[3:8]
class.tree <- rpart(against_dark ~ ., data = pkm_df_x, 
                    control = rpart.control(maxdepth = 2, minsplit = 10), method = "class")
class.tree
## n= 801 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
## 1) root 801 236 1 (0.0037 0.16 0.71 0.13 0.0025)  
##   2) against_fairy>=1.5 112  44 0.5 (0.027 0.61 0.34 0.027 0)  
##     4) against_dragon< 1.5 72  10 0.5 (0.042 0.86 0.097 0 0) *
##     5) against_dragon>=1.5 40   9 1 (0 0.15 0.78 0.075 0) *
##   3) against_fairy< 1.5 689 162 1 (0 0.084 0.76 0.15 0.0029)  
##     6) against_dragon< 0.25 47   7 0.5 (0 0.85 0.15 0 0) *
##     7) against_dragon>=0.25 642 122 1 (0 0.028 0.81 0.16 0.0031) *
prp(class.tree, type = 1, extra = 1, split.font = 1, varlen = -10)  

pkm_df_x$against_dark = factor(pkm_df_x$against_dark, levels = c(0, 1))
library(caTools)
set.seed(123)
split = sample.split(pkm_df_x$against_dark, SplitRatio = 0.75)
training_set = subset(pkm_df_x, split == TRUE)
test_set = subset(pkm_df_x, split == FALSE)
library(rpart)
classifier = rpart(formula = against_dragon ~ .,
                   data = training_set)
plot(classifier)
text(classifier)

CONCLUSION

We can pull some conclusions regarding our dataset based on the previous cluster and principle component analysis. For example, we can separate our data into at least 4 clusters based on all of the numerical features, with more than 87% of the total sum of squares come from the distance of observations between clusters. Also note Cluster 2 has the unique traits that it has the most (if not all) of the legendary Pokemon, which make it the best overall in base_total battle stats. We can reduce our dimensions from 31 features into just 13 dimensions and still retain more than 80% of the variances using PCA. The dimensionality reduction can be useful if we apply the new PCA for machine learning applications. Moreover, However, as we have seen, the dimensionality reduction is not enough for us to visualize the clustering of our data, indicated by overlapping of clusters if we only use the first 2 dimensions. Perhaps the result from the gap statistic method is true, that there is only 1 big cluster