# This is the R chunk for the required packages
library(readr)
library(dplyr)
library(magrittr)
library(tidyr)
library(MVN)
In this report, after import two datasets, tidy and manipulate the datasets and create one more varable then join them togather as a new dataframe to investigate. First, we need to understand the dataset structure and types of variables. Then scan all missing values, special values, obvious errors and outliers of all numeric variables, deal with the missing values and outliers. In the end, Apply appropriate transformation for decrease the skewness of the variables and convert the distribution into a normal distribution.
Both data sets are taken from https://www.kaggle.com website.
https://www.kaggle.com/terminus7/pokemon-challenge?select=pokemon.csv
using read_csv() function to import the two datasets.
using head() function to check the dataset respectively
using str() function to check the class of each variable
“pokemon” dataset have 12 variables and the detials are given below:
# [numeric] pokemon ID number
Name [character] name of pokemon
Type 1 [character] 1st attack type
Type 2 [character] 2nd attack type
HP [numeric] Hitpoints
Attack [numeric] Attack force
Defense [numeric] Defense points
Sp. Atk [numeric] Special attack force
Sp. Def [numeric] Special defense points
Speed [numeric] Speed of pokemon
Generation [numeric] Development stage
Legendary [logical] Legendary status
“combats” dataset have 3 variables, the detials are given below:
First_pokemon [numeric] ID number of one combatants
Second_pokemon [numeric] ID number of another combatants
Winner [numeric] ID number of the winner
pokemon <- read_csv("2619_4359_bundle_archive/pokemon.csv")
Parsed with column specification:
cols(
`#` = [32mcol_double()[39m,
Name = [31mcol_character()[39m,
`Type 1` = [31mcol_character()[39m,
`Type 2` = [31mcol_character()[39m,
HP = [32mcol_double()[39m,
Attack = [32mcol_double()[39m,
Defense = [32mcol_double()[39m,
`Sp. Atk` = [32mcol_double()[39m,
`Sp. Def` = [32mcol_double()[39m,
Speed = [32mcol_double()[39m,
Generation = [32mcol_double()[39m,
Legendary = [33mcol_logical()[39m
)
head(pokemon)
combats <- read_csv("2619_4359_bundle_archive/combats.csv")
Parsed with column specification:
cols(
First_pokemon = [32mcol_double()[39m,
Second_pokemon = [32mcol_double()[39m,
Winner = [32mcol_double()[39m
)
head(combats)
str(pokemon)
tibble [800 × 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ # : num [1:800] 1 2 3 4 5 6 7 8 9 10 ...
$ Name : chr [1:800] "Bulbasaur" "Ivysaur" "Venusaur" "Mega Venusaur" ...
$ Type 1 : chr [1:800] "Grass" "Grass" "Grass" "Grass" ...
$ Type 2 : chr [1:800] "Poison" "Poison" "Poison" "Poison" ...
$ HP : num [1:800] 45 60 80 80 39 58 78 78 78 44 ...
$ Attack : num [1:800] 49 62 82 100 52 64 84 130 104 48 ...
$ Defense : num [1:800] 49 63 83 123 43 58 78 111 78 65 ...
$ Sp. Atk : num [1:800] 65 80 100 122 60 80 109 130 159 50 ...
$ Sp. Def : num [1:800] 65 80 100 120 50 65 85 85 115 64 ...
$ Speed : num [1:800] 45 60 80 80 65 80 100 100 100 43 ...
$ Generation: num [1:800] 1 1 1 1 1 1 1 1 1 1 ...
$ Legendary : logi [1:800] FALSE FALSE FALSE FALSE FALSE FALSE ...
- attr(*, "spec")=
.. cols(
.. `#` = [32mcol_double()[39m,
.. Name = [31mcol_character()[39m,
.. `Type 1` = [31mcol_character()[39m,
.. `Type 2` = [31mcol_character()[39m,
.. HP = [32mcol_double()[39m,
.. Attack = [32mcol_double()[39m,
.. Defense = [32mcol_double()[39m,
.. `Sp. Atk` = [32mcol_double()[39m,
.. `Sp. Def` = [32mcol_double()[39m,
.. Speed = [32mcol_double()[39m,
.. Generation = [32mcol_double()[39m,
.. Legendary = [33mcol_logical()[39m
.. )
str(combats)
tibble [50,000 × 3] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ First_pokemon : num [1:50000] 266 702 191 237 151 657 192 73 220 302 ...
$ Second_pokemon: num [1:50000] 298 701 668 683 231 752 134 545 763 31 ...
$ Winner : num [1:50000] 298 701 668 683 151 657 134 545 763 31 ...
- attr(*, "spec")=
.. cols(
.. First_pokemon = [32mcol_double()[39m,
.. Second_pokemon = [32mcol_double()[39m,
.. Winner = [32mcol_double()[39m
.. )
Both data set needs to be tidy so they can merge togather.
combine three function group_by(), mutate() and n_distinct() to calculate how many combats pokemon wins for each winner, create a new column named “No.win”.
select variable “Winner” and “No.win” and then using unique() function filter out the unique rows
change the name of the key variables of the two datasets into “pokemon ID” in order to merge.
combats <- combats %>% group_by(Winner) %>% mutate(No.win = n_distinct(cumsum(Winner)))
combats <- combats[ ,3:4] %>% unique()
names(pokemon)[names(pokemon) == "#"] <- "pokemon ID"
names(combats)[names(combats) == "Winner"] <- "pokemon ID"
using inner_join() function join the two datasets, new dataset name is “pokemon_com”
pokemon_com <- pokemon %>% left_join(combats, by = "pokemon ID")
head(pokemon_com)
using factor() function to convert variable “Generation” into factor with levels and labels
using as.charactor() function convert varaible “pokemon ID” from numeric to charactor
using str() function to check the class of the variables
using attributes() function to check the attributes of the dataset
using summary() function to get the statistics of the variables
pokemon_com$Generation <- factor(pokemon_com$Generation, levels = c(1,2,3,4,5,6), labels = c("Gen1", "Gen2", "Gen3", "Gen4", "Gen5", "Gen6"))
pokemon_com$`pokemon ID` <- as.character(pokemon_com$`pokemon ID`)
str(pokemon_com)
tibble [800 × 13] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ pokemon ID: chr [1:800] "1" "2" "3" "4" ...
$ Name : chr [1:800] "Bulbasaur" "Ivysaur" "Venusaur" "Mega Venusaur" ...
$ Type 1 : chr [1:800] "Grass" "Grass" "Grass" "Grass" ...
$ Type 2 : chr [1:800] "Poison" "Poison" "Poison" "Poison" ...
$ HP : num [1:800] 45 60 80 80 39 58 78 78 78 44 ...
$ Attack : num [1:800] 49 62 82 100 52 64 84 130 104 48 ...
$ Defense : num [1:800] 49 63 83 123 43 58 78 111 78 65 ...
$ Sp. Atk : num [1:800] 65 80 100 122 60 80 109 130 159 50 ...
$ Sp. Def : num [1:800] 65 80 100 120 50 65 85 85 115 64 ...
$ Speed : num [1:800] 45 60 80 80 65 80 100 100 100 43 ...
$ Generation: Factor w/ 6 levels "Gen1","Gen2",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Legendary : logi [1:800] FALSE FALSE FALSE FALSE FALSE FALSE ...
$ No.win : int [1:800] 37 46 89 70 55 64 115 119 114 19 ...
attributes(pokemon_com)
$row.names
[1] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 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] 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
$names
[1] "pokemon ID" "Name" "Type 1" "Type 2" "HP" "Attack" "Defense" "Sp. Atk"
[9] "Sp. Def" "Speed" "Generation" "Legendary" "No.win"
$class
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
summary(pokemon_com)
pokemon ID Name Type 1 Type 2 HP Attack
Length:800 Length:800 Length:800 Length:800 Min. : 1.00 Min. : 5
Class :character Class :character Class :character Class :character 1st Qu.: 50.00 1st Qu.: 55
Mode :character Mode :character Mode :character Mode :character Median : 65.00 Median : 75
Mean : 69.26 Mean : 79
3rd Qu.: 80.00 3rd Qu.:100
Max. :255.00 Max. :190
Defense Sp. Atk Sp. Def Speed Generation Legendary No.win
Min. : 5.00 Min. : 10.00 Min. : 20.0 Min. : 5.00 Gen1:166 Mode :logical Min. : 3.00
1st Qu.: 50.00 1st Qu.: 49.75 1st Qu.: 50.0 1st Qu.: 45.00 Gen2:106 FALSE:735 1st Qu.: 36.00
Median : 70.00 Median : 65.00 Median : 70.0 Median : 65.00 Gen3:160 TRUE :65 Median : 62.00
Mean : 73.84 Mean : 72.82 Mean : 71.9 Mean : 68.28 Gen4:121 Mean : 63.86
3rd Qu.: 90.00 3rd Qu.: 95.00 3rd Qu.: 90.0 3rd Qu.: 90.00 Gen5:165 3rd Qu.: 91.00
Max. :230.00 Max. :194.00 Max. :230.0 Max. :180.00 Gen6: 82 Max. :152.00
NA's :17
using colSums() and is.na() function to check the number of missing value for each column
using impute() function impute information for each column that has missing value.
using sapply() and is.special function to check every numeric variables whether they have infinite or NaN values after impute
using sapply() and is.na() function to check the number of missing value for each column after impute
colSums(is.na(pokemon_com))
pokemon ID Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed
0 1 0 386 0 0 0 0 0 0
Generation Legendary No.win
0 0 17
pokemon_com$No.win <- impute(pokemon_com$No.win, fun = "0")
pokemon_com$`Type 2` <- impute(pokemon_com$`Type 2`, fun = "no record")
is.special <- function(x){
if (is.numeric(x)) (is.infinite(x) | is.nan(x))
}
sapply(pokemon_com, function(x) sum( is.special(x) ))
pokemon ID Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed
0 0 0 0 0 0 0 0 0 0
Generation Legendary No.win
0 0 0
sapply(pokemon_com, function(x) sum( is.na(x) ))
pokemon ID Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed
0 1 0 0 0 0 0 0 0 0
Generation Legendary No.win
0 0 0
In this case, variable No.win represent the number of wins for the each pokemon. The missing value in this variables means that pokemon never win. So, we use 0 to replace all NAs using impute(). The missing value in varaible Type 2 means that pokemon does not have 2nd attack type. So, we fill all missing values with “no record” using impute().
using boxplot() function to detect outliers for varaibles HP, Attack, Defense, Sp. Atk, Sp. Def and Speed by its generation select all numeric variables as a new dataframe name “pokemon_sub”
using summary() function to check the descriptive statistics
using sapply() function and a user defined function “cap” to the dataframe in order to deal with outliers
using summary() function to check summary statistics again
boxplot(pokemon_com$HP ~ pokemon_com$Generation, main = "Hitpoints by generation", ylab = "HP", xlab = "generation")
boxplot(pokemon_com$Attack ~ pokemon_com$Generation, main = "Attack force by generation", ylab = "Attack force", xlab = "generation")
boxplot(pokemon_com$Defense ~ pokemon_com$Generation, main = "Defense points by generation", ylab = "Defense points", xlab = "generation")
boxplot(pokemon_com$`Sp. Atk` ~ pokemon_com$Generation, main = "Special attack force by generation", ylab = "Special attack force", xlab = "generation")
boxplot(pokemon_com$`Sp. Def` ~ pokemon_com$Generation, main = "Special defense points by generation", ylab = "Special defense points", xlab = "generation")
boxplot(pokemon_com$Speed ~ pokemon_com$Generation, main = "Speed by generation", ylab = "Speed", xlab = "generation")
cap <- function(x){
quantiles <- quantile( x, c(.05, 0.25, 0.75, .95 ) )
x[ x < quantiles[2] - 1.5*IQR(x) ] <- quantiles[1]
x[ x > quantiles[3] + 1.5*IQR(x) ] <- quantiles[4]
x
}
pokemon_sub <- pokemon_com[ ,5:10]
summary(pokemon_sub)
HP Attack Defense Sp. Atk Sp. Def Speed
Min. : 1.00 Min. : 5 Min. : 5.00 Min. : 10.00 Min. : 20.0 Min. : 5.00
1st Qu.: 50.00 1st Qu.: 55 1st Qu.: 50.00 1st Qu.: 49.75 1st Qu.: 50.0 1st Qu.: 45.00
Median : 65.00 Median : 75 Median : 70.00 Median : 65.00 Median : 70.0 Median : 65.00
Mean : 69.26 Mean : 79 Mean : 73.84 Mean : 72.82 Mean : 71.9 Mean : 68.28
3rd Qu.: 80.00 3rd Qu.:100 3rd Qu.: 90.00 3rd Qu.: 95.00 3rd Qu.: 90.0 3rd Qu.: 90.00
Max. :255.00 Max. :190 Max. :230.00 Max. :194.00 Max. :230.0 Max. :180.00
pokemon_cap <- sapply(pokemon_sub, FUN = cap)
summary(pokemon_cap)
HP Attack Defense Sp. Atk Sp. Def Speed
Min. : 10.00 Min. : 5.00 Min. : 5.00 Min. : 10.00 Min. : 20.00 Min. : 5.00
1st Qu.: 50.00 1st Qu.: 55.00 1st Qu.: 50.00 1st Qu.: 49.75 1st Qu.: 50.00 1st Qu.: 45.00
Median : 65.00 Median : 75.00 Median : 70.00 Median : 65.00 Median : 70.00 Median : 65.00
Mean : 68.21 Mean : 78.62 Mean : 72.88 Mean : 72.27 Mean : 71.44 Mean : 68.14
3rd Qu.: 80.00 3rd Qu.:100.00 3rd Qu.: 90.00 3rd Qu.: 95.00 3rd Qu.: 90.00 3rd Qu.: 90.00
Max. :125.00 Max. :165.00 Max. :150.00 Max. :160.00 Max. :150.00 Max. :150.00
In this case, the best way to handle those outliers is to replacing the outliers with the nearest neighbours that are not outliers. Because exclude or delete the outliers will miss part of the information in the dataset, replace outlier values with mean or median may cause mistakes. So, we choose using capping to deal with those outliers.
using hist() function to plot the histogram of variable HP, we observe that HP have a right-skewed distribution
applying the logarithmic transformation (base 10) using the log10() function then plot histogram
using the log() function applying the natural logarithm then plot histogram
using sqrt() function applying the square root transformation then plot histogram
As seen from the histograms, square root transformation worked better than the other two transformation for reducing right skewness. It decrease the skewness and convert the distribution into a normal distribution.
hist(pokemon_com$HP)
log_HP <- log10(pokemon_com$HP)
hist(log_HP)
ln_HP <- log(pokemon_com$HP)
hist(ln_HP)
sqrt_HP <- sqrt(pokemon_com$HP)
hist(sqrt_HP)