R Markdown

Introdução

Atividade 4 - uma qualitativa versus uma quantitativa.

Este relátorio é sobre a base de dados df-pokemon, será analisado o poder de defesa e ataque dos grupos de tipos de pokemons.

Passo 1 - Carregar as bibliotecas

library(readxl)
library(flextable)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Passo 2 - Carregar o banco de dados

load("~/Base_de_dados-master/df_pokemon.RData")

As primeiras linhas do banco de dados

df %>% head() %>% data.frame() %>% flextable()

Duas Qualitativas

Vamos fazer a análise de duas variáveis qualitativas.

tabela = table(df$type_1,df$defense)
tabela
##           
##             5 10 15 20 23 25 28 30 32 33 34 35 37 38 39 40 41 42 43 44 45 47 48
##   bug       0  0  0  0  0  0  0  2  1  0  0  2  0  0  0  4  1  1  0  0  5  0  0
##   dark      0  0  0  0  0  0  0  1  0  0  0  1  1  0  0  1  0  1  0  0  0  0  0
##   dragon    0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  2  0  0
##   electric  0  0  1  0  0  0  0  0  1  1  1  0  1  0  0  5  0  0  0  0  0  0  0
##   fairy     0  0  0  0  0  0  1  0  0  0  0  0  0  0  1  0  0  0  0  0  0  1  1
##   fighting  0  0  0  0  0  0  0  1  0  0  0  2  0  0  0  1  0  0  0  0  0  0  0
##   fire      0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  5  0  0  2  1  3  0  1
##   flying    0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
##   ghost     0  0  0  0  0  0  0  1  0  0  1  1  0  0  0  0  0  0  0  1  1  0  1
##   grass     0  0  0  0  0  0  0  1  0  0  0  3  0  0  0  3  0  0  0  0  4  0  2
##   ground    0  0  0  0  0  1  0  0  0  0  0  1  0  0  0  1  0  0  0  0  2  0  0
##   ice       0  0  1  0  0  0  0  1  0  0  0  1  0  0  0  2  0  0  0  0  1  0  0
##   normal    2  1  1  1  1  0  0  4  0  0  1  3  0  1  1  5  1  1  2  1  5  0  1
##   poison    0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  2  0  0  0  1  0  1  0
##   psychic   0  0  1  0  0  1  0  1  0  0  0  2  0  0  0  1  0  0  1  0  5  0  2
##   rock      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  1  0  0
##   steel     0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   water     0  0  0  2  0  0  0  2  0  0  0  4  0  1  0  4  0  0  1  0  3  0  3
##           
##            49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
##   bug       0  8  1  0  0  0  7  0  0  0  1  3  0  1  0  0  1  0  0  0  0  3  0
##   dark      0  3  0  1  1  0  1  0  0  0  0  2  0  0  0  0  1  0  0  0  0  4  0
##   dragon    0  0  0  0  1  0  0  0  0  0  0  2  0  0  0  0  2  0  0  0  0  2  0
##   electric  1  2  0  1  0  0  2  0  2  0  0  3  0  0  1  0  0  0  1  0  0  5  0
##   fairy     0  1  0  0  0  0  0  0  0  0  0  1  0  0  0  0  2  1  0  1  0  0  0
##   fighting  0  2  0  0  1  0  2  0  0  0  0  3  0  1  0  0  0  0  0  0  0  2  0
##   fire      0  0  0  1  0  0  4  0  1  4  0  2  0  0  1  0  1  1  1  0  0  3  2
##   flying    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
##   ghost     0  0  0  0  0  0  1  0  0  0  0  4  0  0  0  0  1  0  0  0  0  1  0
##   grass     1  5  0  0  0  0  3  0  0  0  0  4  0  1  2  1  5  0  1  0  0  4  0
##   ground    0  3  0  0  0  0  1  0  0  0  0  2  0  0  0  0  0  0  0  0  0  0  0
##   ice       0  3  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  2  0
##   normal    0  7  0  0  0  0  3  0  0  0  0 10  1  2  1  2  5  1  0  0  1  8  0
##   poison    0  1  0  1  1  0  0  0  1  0  0  2  0  1  0  0  1  0  2  0  1  1  0
##   psychic   0  4  0  0  0  1  2  0  0  1  0  1  0  0  0  0  4  0  0  0  0  5  0
##   rock      0  3  0  0  0  0  0  0  0  0  0  1  0  0  0  0  3  0  1  0  0  1  0
##   steel     0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
##   water     0  6  0  1  1  0  6  1  0  1  0  3  0  1  2  1  7  0  1  2  0  5  0
##           
##            72 73 75 76 77 78 79 80 82 83 84 85 86 87 88 89 90 91 95 97 99 100
##   bug       0  0  1  0  0  0  0  3  0  0  0  3  1  0  0  1  3  0  1  0  1   2
##   dark      0  0  2  0  0  0  0  0  0  0  0  0  0  0  1  0  2  0  1  0  0   1
##   dragon    0  0  0  0  0  0  0  2  0  0  0  0  0  0  0  0  6  0  2  0  0   2
##   electric  0  0  1  0  1  0  1  1  0  0  0  2  0  0  0  0  0  0  1  0  0   0
##   fairy     1  1  1  0  0  0  0  0  0  0  0  1  1  0  0  0  0  0  2  0  0   0
##   fighting  0  0  3  0  0  1  1  1  0  0  0  2  0  0  0  0  0  0  2  0  0   0
##   fire      2  0  1  0  0  2  0  1  0  0  0  1  0  0  0  0  2  0  0  0  0   0
##   flying    0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0   0
##   ghost     0  0  0  1  0  0  0  0  0  0  0  1  0  0  0  0  2  0  0  0  0   0
##   grass     2  0  3  0  0  0  0  3  0  2  0  4  0  0  0  0  0  1  3  0  0   2
##   ground    0  0  0  0  0  1  0  3  0  0  1  1  0  0  0  0  1  0  2  0  0   0
##   ice       0  0  0  0  0  0  0  4  0  0  0  2  0  0  0  0  1  0  0  0  0   2
##   normal    0  0  4  0  2  0  0  3  0  0  1  0  1  0  0  0  2  0  3  0  0   1
##   poison    0  0  1  0  1  0  0  1  1  1  0  0  0  1  0  0  2  0  1  0  0   0
##   psychic   0  0  2  1  0  0  0  1  0  0  0  1  0  0  0  0  1  0  1  0  0   3
##   rock      1  0  0  0  2  0  0  0  0  0  0  2  0  0  0  0  2  0  1  1  0   3
##   steel     0  0  0  0  0  0  0  1  0  0  0  1  1  0  0  0  0  1  1  0  0   4
##   water     0  1  4  1  0  1  2  8  0  0  0  6  0  0  2  0  4  0  3  0  0   6
##           
##            102 103 105 106 108 110 112 115 116 118 119 120 121 122 125 129 130
##   bug        1   0   1   0   0   0   1   0   0   0   0   0   0   0   1   0   0
##   dark       0   0   1   0   0   1   0   1   0   0   0   0   0   0   0   0   0
##   dragon     0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0
##   electric   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0
##   fairy      0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
##   fighting   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
##   fire       0   0   0   1   0   0   0   0   0   0   0   1   0   0   0   0   0
##   flying     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
##   ghost      0   0   0   0   1   0   0   0   0   0   0   1   0   1   0   0   1
##   grass      0   0   1   0   0   0   0   1   0   0   0   0   0   1   1   0   1
##   ground     0   0   2   0   0   2   0   0   0   1   0   2   0   0   1   0   1
##   ice        0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0
##   normal     0   0   1   0   0   1   0   0   0   0   0   1   0   0   0   0   0
##   poison     0   0   0   0   0   1   0   0   0   0   0   1   0   0   0   0   0
##   psychic    0   0   1   0   0   0   0   0   0   0   0   1   0   0   0   0   2
##   rock       0   0   2   0   0   1   0   3   0   1   1   0   0   0   1   0   2
##   steel      0   0   0   0   0   0   0   1   1   0   0   1   0   0   0   1   1
##   water      0   1   2   0   0   1   0   2   0   0   0   0   0   0   0   0   1
##           
##            131 133 135 140 145 150 160 168 180 184 200 230
##   bug        0   0   0   1   0   0   0   0   0   0   0   1
##   dark       0   0   0   0   0   0   0   0   0   0   0   0
##   dragon     0   0   0   0   0   0   0   0   0   0   0   0
##   electric   0   0   0   0   0   0   0   0   0   0   0   0
##   fairy      0   0   0   0   0   0   0   0   0   0   0   0
##   fighting   0   0   0   0   0   0   0   0   0   0   0   0
##   fire       0   0   0   1   0   0   0   0   0   0   0   0
##   flying     0   0   0   0   0   0   0   0   0   0   0   0
##   ghost      0   0   1   0   1   0   0   0   0   0   0   0
##   grass      1   0   0   0   0   0   0   0   0   0   0   0
##   ground     0   0   0   1   0   0   0   0   0   0   0   0
##   ice        0   0   0   0   0   0   0   0   0   1   0   0
##   normal     0   0   0   0   0   0   0   0   0   0   0   0
##   poison     0   0   0   0   0   0   0   0   0   0   0   0
##   psychic    0   0   0   0   0   0   0   0   0   0   0   0
##   rock       0   0   1   0   1   1   1   1   0   0   1   0
##   steel      0   0   0   2   0   3   0   0   1   0   1   0
##   water      0   1   0   0   0   0   0   0   1   0   0   0
tabela2 = table(df$type_1,df$attack)
tabela2
##           
##            5 10 15 20 22 23 24 25 27 29 30 33 35 36 38 40 41 42 43 44 45 46 47
##   bug      0  1  0  2  1  0  0  2  0  1  3  0  5  0  0  1  0  0  0  0  4  0  2
##   dark     0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   dragon   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   electric 0  0  0  0  0  0  0  0  0  0  1  0  1  0  1  3  0  0  0  0  2  0  0
##   fairy    0  0  0  1  0  0  0  1  0  0  0  0  0  0  1  1  0  0  0  0  2  0  0
##   fighting 0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  1  0  0  0  0  0  0  0
##   fire     0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  0  0  0  1  0  0
##   flying   0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0
##   ghost    0  0  0  0  0  0  0  0  0  0  2  0  1  0  0  2  0  0  0  0  0  0  0
##   grass    0  0  0  0  0  0  0  0  1  0  2  0  3  0  0  3  0  0  0  0  3  0  0
##   ground   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0
##   ice      0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  1  0  0  0  0  0  0  0
##   normal   2  1  0  2  0  0  0  0  0  0  3  0  0  1  0  1  0  0  0  0  5  1  0
##   poison   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0  1
##   psychic  0  0  0  1  0  1  0  4  0  0  3  1  2  0  0  1  0  0  0  0  4  0  0
##   rock     0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  0  0  2  0  0
##   steel    0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   water    0  1  1  2  0  0  0  0  0  0  3  0  0  0  1  4  0  0  0  1  3  0  0
##           
##            48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
##   bug       0  0  1  0  1  1  0  2  0  0  0  1  3  0  0  1  0  4  0  0  0  0  3
##   dark      0  0  1  0  0  0  1  2  0  0  0  0  1  0  0  0  0  4  0  0  0  0  0
##   dragon    0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  2
##   electric  0  0  3  0  0  0  0  4  0  0  1  0  2  0  0  1  0  2  0  0  0  0  1
##   fairy     1  0  1  0  1  0  0  0  0  0  0  0  0  0  0  0  0  2  0  0  0  0  1
##   fighting  0  0  0  0  0  0  0  0  0  0  0  0  2  0  0  0  0  0  0  0  0  0  1
##   fire      0  0  2  0  2  1  0  0  0  0  1  1  2  0  0  1  2  0  0  0  1  1  1
##   flying    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
##   ghost     0  0  3  0  0  0  0  1  0  0  0  0  2  0  0  0  0  1  1  0  0  0  2
##   grass     0  2  2  0  0  1  0  3  0  0  0  0  4  1  3  0  0  3  0  1  2  0  2
##   ground    0  0  1  0  0  0  0  1  0  0  0  0  1  0  0  0  0  0  1  0  0  0  2
##   ice       0  0  6  0  0  0  0  1  0  0  0  0  2  0  0  0  0  1  0  0  0  1  1
##   normal    1  0  3  1  0  0  0  7  2  0  0  0  8  0  0  0  0  3  1  0  0  0  6
##   poison    0  0  2  0  0  0  0  0  0  1  0  0  2  1  1  1  0  1  0  0  0  0  0
##   psychic   3  0  3  0  0  0  0  3  0  1  1  0  0  0  0  0  0  3  0  0  0  0  1
##   rock      0  0  1  0  2  0  0  2  0  0  0  1  1  0  0  0  1  0  0  0  0  0  0
##   steel     0  0  1  0  0  0  0  2  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
##   water     3  1  5  1  1  2  0  2  1  0  1  0  2  0  0  2  1  9  1  1  0  1  5
##           
##            71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
##   bug       0  0  1  0  1  1  1  0  1  0  0  0  0  2  0  0  0  0  5  0  0  0  1
##   dark      0  0  0  0  2  0  0  0  0  0  0  0  0  3  0  0  1  0  4  0  1  0  0
##   dragon    0  0  0  0  2  0  0  0  1  0  0  0  1  0  0  1  0  0  2  0  0  0  0
##   electric  0  0  0  0  3  0  0  0  0  0  0  1  0  3  0  0  0  0  2  0  0  0  0
##   fairy     0  1  0  0  0  0  0  0  2  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   fighting  0  0  0  0  0  0  0  0  3  0  1  0  0  1  0  0  0  0  0  0  1  0  0
##   fire      0  0  1  0  1  1  0  1  0  1  0  0  2  3  0  0  0  0  2  0  0  1  0
##   flying    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   ghost     0  0  0  0  1  0  0  0  1  0  0  0  0  0  0  0  0  0  1  0  1  0  0
##   grass     0  0  0  0  3  0  0  1  2  0  2  0  0  3  1  0  0  1  2  0  1  0  1
##   ground    0  2  0  1  2  0  0  0  2  0  1  0  0  2  0  0  0  0  0  0  0  0  0
##   ice       0  0  0  0  0  0  0  0  3  0  0  0  0  1  0  0  0  0  0  0  0  0  0
##   normal    1  0  0  0  1  2  2  0  9  1  1  1  0  6  0  0  0  0  2  1  0  0  0
##   poison    0  1  1  0  1  0  0  0  2  0  0  0  0  1  0  0  0  0  3  0  1  1  0
##   psychic   0  1  1  0  3  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0
##   rock      0  0  0  0  1  0  1  0  3  1  0  0  1  0  0  0  0  1  0  0  0  0  0
##   steel     0  0  0  0  2  0  0  0  4  0  0  0  0  2  0  0  0  1  2  0  0  0  0
##   water     0  1  1  0  7  0  0  2  3  0  1  2  1  3  1  1  0  0  3  0  2  0  0
##           
##            95 97 98 100 102 103 104 105 106 107 108 109 110 112 115 117 120 121
##   bug       2  0  0   1   0   1   0   0   0   0   0   1   1   0   0   0   1   0
##   dark      1  0  0   0   0   0   0   2   0   0   0   0   0   0   0   0   1   0
##   dragon    1  0  0   2   0   0   0   0   0   0   0   0   0   0   0   1   2   0
##   electric  0  0  0   1   0   0   0   0   0   0   0   0   0   0   2   0   1   0
##   fairy     0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0
##   fighting  1  0  0   2   0   0   0   3   0   0   0   0   1   0   0   0   2   0
##   fire      2  1  1   3   0   0   1   0   0   0   0   0   1   0   1   0   1   0
##   flying    0  0  0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0
##   ghost     0  0  0   2   0   0   0   0   0   0   0   0   1   0   1   0   0   0
##   grass     1  0  1   5   0   0   0   1   0   1   0   1   1   0   1   0   0   0
##   ground    1  0  0   3   0   0   0   0   0   0   0   0   0   1   0   1   1   0
##   ice       1  0  0   1   0   0   0   0   0   0   0   0   1   0   0   1   0   0
##   normal    3  0  0   3   0   0   0   0   0   0   0   0   4   0   2   0   2   0
##   poison    1  0  0   1   1   0   0   1   1   0   0   0   0   0   0   0   0   0
##   psychic   0  0  0   3   0   0   0   1   0   0   0   0   1   0   0   0   0   0
##   rock      3  0  0   2   0   0   0   3   0   0   0   0   0   1   1   0   1   1
##   steel     0  0  0   2   0   0   0   0   0   0   0   0   2   0   0   0   1   0
##   water     6  0  1   3   0   0   1   4   0   0   1   0   1   0   0   0   3   0
##           
##            123 124 125 129 130 131 134 135 140 147 150 160 165
##   bug        0   0   2   0   1   0   0   1   0   0   0   0   0
##   dark       0   0   2   0   1   1   0   0   0   0   0   0   0
##   dragon     0   0   0   0   2   0   1   1   0   1   2   0   0
##   electric   1   0   0   0   0   0   0   0   0   0   0   0   0
##   fairy      0   0   0   0   0   1   0   0   0   0   0   0   0
##   fighting   0   1   2   0   1   0   0   0   1   0   0   0   0
##   fire       1   0   0   0   2   0   0   0   1   0   0   0   0
##   flying     0   0   0   0   0   0   0   0   0   0   0   0   0
##   ghost      0   0   0   0   0   0   0   0   0   0   0   0   0
##   grass      0   0   0   0   1   0   0   0   0   0   0   0   0
##   ground     0   1   1   0   1   0   0   1   1   0   1   0   0
##   ice        0   0   0   0   1   0   0   0   0   0   0   0   0
##   normal     1   0   0   0   1   0   0   0   0   0   0   2   0
##   poison     0   0   0   0   0   0   0   0   0   0   0   0   0
##   psychic    0   0   2   0   0   0   0   0   0   0   1   0   0
##   rock       0   0   2   1   0   0   1   1   1   0   0   0   1
##   steel      0   0   0   0   0   0   0   1   0   0   0   0   0
##   water      0   0   1   0   1   0   0   0   0   0   0   0   0

Perguntas:

1. Qual tipo (da variável tipo1) tem o ataque mais forte

2. Qual tipo (da variável tipo1) tem a defesa mais forte

Variável resposta: attack, defense

Variável explicativa: type_1

Qualitativa versus Quantitativa

 df %>% 
    select(attack,type_1) %>% 
    group_by(type_1) %>%
    summarise(média=mean(attack),
              mediana=median(attack),
              desvio_padrão=sd(attack)) %>% 
    flextable() %>% theme_zebra()
 df %>% 
   select(defense,type_1) %>% 
   group_by(type_1) %>%
   summarise(média=mean(defense),
             mediana=median(defense),
             desvio_padrão=sd(defense)) %>% 
   flextable() %>% theme_zebra()

O boxplot

boxplot(df$attack ~ df$type_1,
        col=c("pink"),
        main="Gráfico 1 - boxplot do poder de ataque por tipo 1")

boxplot(df$defense ~ df$type_1,
        col=c("skyblue"),
        main="Gráfico 1 - boxplot do poder de defesa por tipo 1")

Conclusão

Ao agrupar as variáveis qualitativas “attack” e “defense” pela variável qualitativa “type_1”, foi possível calcular a média, mediana e o desvio padrão da força de ataque e defesa de cada grupo de pokemons, identificando quais grupos são mais coesos, isso é, têm forças de ataque ou defesa parecidos, se são bem variados e se têm outlier(s).

Para isso, foi feito um gráfico de caixas onde obtemos essas informações de forma mais visual e integral. Assim, é possível analisar a coesão dos grupos para cada variável qualitativa. No ataque a maior parte dos grupos são bem equilibrados, têm a caixa dividida ao meio e a caixa centralizada à linha pontilhada, tendo outliers presentes apenas nos grupos fairy, normal, psychic e steel e sendo o grupo rock com o maior máximo.

Na defesa, os grupos parecem ser muito variados, as caixas estão, em sua maioria, descentralizadas tanto com o total, quanto com a mediana. Há também mais outliers, estão presentes no grupo fire, ice, normal, psychic, rock e water e o grupo com o maior máximo é o steel.