load("C:/Estatistica/Base_de_dados-master/Titanic.RData")
str(Titanic)
## 'data.frame': 2200 obs. of 4 variables:
## $ Classe : Factor w/ 4 levels "Tripulação","Primeira",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ Idade : Factor w/ 2 levels "criança","adulto": 2 2 2 2 2 2 2 2 2 2 ...
## $ Sexo : Factor w/ 2 levels "Feminino","Masculino": 2 2 2 2 2 2 2 2 2 2 ...
## $ Sobreviveu: Factor w/ 2 levels "Não sobreviveu",..: 2 2 2 2 2 2 2 2 2 2 ...
R: 2200 observações, logo, 2200 pessoas.
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
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.4 v stringr 1.4.0
## v tidyr 1.1.2 v forcats 0.5.0
## v readr 1.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Titanic %>%
group_by(Sobreviveu) %>%
summarise(quantidade = n())
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 2 x 2
## Sobreviveu quantidade
## <fct> <int>
## 1 Não sobreviveu 1490
## 2 Sobreviveu 710
R: 710 pessoas sobreviveram.
library(dlookr)
## Loading required package: mice
##
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
##
## filter
## The following objects are masked from 'package:base':
##
## cbind, rbind
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##
## Attaching package: 'dlookr'
## The following object is masked from 'package:base':
##
## transform
diagnose(Titanic)
## # A tibble: 4 x 6
## variables types missing_count missing_percent unique_count unique_rate
## <chr> <chr> <int> <dbl> <int> <dbl>
## 1 Classe factor 0 0 4 0.00182
## 2 Idade factor 0 0 2 0.000909
## 3 Sexo factor 0 0 2 0.000909
## 4 Sobreviveu factor 0 0 2 0.000909
diagnose_category(Titanic, Sobreviveu)
## # A tibble: 2 x 6
## variables levels N freq ratio rank
## * <chr> <fct> <int> <int> <dbl> <int>
## 1 Sobreviveu Não sobreviveu 2200 1490 67.7 1
## 2 Sobreviveu Sobreviveu 2200 710 32.3 2
R: 32,3%
Titanic %>%
group_by(Sexo, Sobreviveu) %>%
summarise(quantidade = n())
## `summarise()` regrouping output by 'Sexo' (override with `.groups` argument)
## # A tibble: 4 x 3
## # Groups: Sexo [2]
## Sexo Sobreviveu quantidade
## <fct> <fct> <int>
## 1 Feminino Não sobreviveu 126
## 2 Feminino Sobreviveu 344
## 3 Masculino Não sobreviveu 1364
## 4 Masculino Sobreviveu 366
R: 344 mulheres sobreviveram.
Titanic %>%
group_by(Idade, Sobreviveu) %>%
summarise(quantidade = n())
## `summarise()` regrouping output by 'Idade' (override with `.groups` argument)
## # A tibble: 4 x 3
## # Groups: Idade [2]
## Idade Sobreviveu quantidade
## <fct> <fct> <int>
## 1 criança Não sobreviveu 52
## 2 criança Sobreviveu 57
## 3 adulto Não sobreviveu 1438
## 4 adulto Sobreviveu 653
R: 57 crianças sobreviveram.
Titanic %>%
group_by(Classe, Sobreviveu) %>%
summarise(quantidade = n())
## `summarise()` regrouping output by 'Classe' (override with `.groups` argument)
## # A tibble: 8 x 3
## # Groups: Classe [4]
## Classe Sobreviveu quantidade
## <fct> <fct> <int>
## 1 Tripulação Não sobreviveu 673
## 2 Tripulação Sobreviveu 212
## 3 Primeira Não sobreviveu 122
## 4 Primeira Sobreviveu 202
## 5 Segunda Não sobreviveu 167
## 6 Segunda Sobreviveu 118
## 7 Terceira Não sobreviveu 528
## 8 Terceira Sobreviveu 178
R: 178 pessoas de terceira classe sobreviveram.
Titanic %>%
group_by (Sexo, Sobreviveu) %>%
summarise (n=n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
## `summarise()` regrouping output by 'Sexo' (override with `.groups` argument)
## # A tibble: 4 x 4
## # Groups: Sexo [2]
## Sexo Sobreviveu n rel.freq
## <fct> <fct> <int> <chr>
## 1 Feminino Não sobreviveu 126 27%
## 2 Feminino Sobreviveu 344 73%
## 3 Masculino Não sobreviveu 1364 79%
## 4 Masculino Sobreviveu 366 21%
Titanic %>%
group_by (Idade, Sobreviveu) %>%
summarise (n=n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
## `summarise()` regrouping output by 'Idade' (override with `.groups` argument)
## # A tibble: 4 x 4
## # Groups: Idade [2]
## Idade Sobreviveu n rel.freq
## <fct> <fct> <int> <chr>
## 1 criança Não sobreviveu 52 48%
## 2 criança Sobreviveu 57 52%
## 3 adulto Não sobreviveu 1438 69%
## 4 adulto Sobreviveu 653 31%
R: 52% das crianças sobreviveram.
Titanic %>%
group_by (Classe, Sobreviveu) %>%
summarise (n=n()) %>%
mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
## `summarise()` regrouping output by 'Classe' (override with `.groups` argument)
## # A tibble: 8 x 4
## # Groups: Classe [4]
## Classe Sobreviveu n rel.freq
## <fct> <fct> <int> <chr>
## 1 Tripulação Não sobreviveu 673 76%
## 2 Tripulação Sobreviveu 212 24%
## 3 Primeira Não sobreviveu 122 38%
## 4 Primeira Sobreviveu 202 62%
## 5 Segunda Não sobreviveu 167 59%
## 6 Segunda Sobreviveu 118 41%
## 7 Terceira Não sobreviveu 528 75%
## 8 Terceira Sobreviveu 178 25%
25% da Terceira classe sobreviveu.
R: Sendo as variáveis qualitativas, o gráfico mais indicado pra representar esse tipo de dado é um Gráfico de Barras, podendo também ser usado um Gráfico de Pizza.
tab_surv <-table(Titanic$Sobreviveu)
barplot(tab_surv,
col = c("red", "blue"),
ylim = c(0,1600))
tab_12 <-table(Titanic$Sobreviveu, Titanic$Sexo)
pie(tab_12,
labels = c("Fem - Não Sobreviveu", "Fem - Sobreviveu", "Masc - Não Sobreviveu", "Masc - Sobreviveu"),
col = c("#FFFF00", "#58FA58", "#FE2E2E", "#0000FF"))