Vamos estudar a análise de duas variáveis qualitativas com a base Titanic.RData
Para o sumário:
output: html_document: toc: true toc_float: true code_folding: hide
A primeira classe sobreviveu mas do que a segunda e a terceira?
# Carregar a base de dados titanic
load("C:/Users/18112914737/Desktop/Titanic.RData")
# Caso precise corrigir o encoding:
Titanic$Classe = iconv(Titanic$Classe, "latin1", "UTF-8")
Tabelas e tabelas de proporções
tabela_classe = table(Titanic$Classe, Titanic$Sobreviveu)
tabela_classe
##
## Não sobreviveu Sobreviveu
## Primeira 122 202
## Segunda 167 118
## Terceira 528 178
## Tripulação 673 212
#Quantas pessoas sobreviveram em cada classe separadamente? A soma de cada classe dá 100%
prop.table(tabela_classe, 1)*100
##
## Não sobreviveu Sobreviveu
## Primeira 37.65432 62.34568
## Segunda 58.59649 41.40351
## Terceira 74.78754 25.21246
## Tripulação 76.04520 23.95480
# Quantas pessoas de modo geral sobreviveram? A soma de todas as porcentagens dá em 100%
prop.table(tabela_classe, 2)*100
##
## Não sobreviveu Sobreviveu
## Primeira 8.187919 28.450704
## Segunda 11.208054 16.619718
## Terceira 35.436242 25.070423
## Tripulação 45.167785 29.859155
# Conclusão: 62% da primeira classe sobreviveu e apenas 24% da
1 prop da linha 2 prop data da coluna 3 prop da
62% da primeira classe sobreviveu e apenas 24% da tripulação sobreviveu
# Relação idade e sobrevivência
#tabela:
#
tabela_idade = table(Titanic$Idade, Titanic$Sobreviveu)
tabela_idade
##
## Não sobreviveu Sobreviveu
## criança 52 57
## adulto 1438 653
prop.table(tabela_idade, 1)*100
##
## Não sobreviveu Sobreviveu
## criança 47.70642 52.29358
## adulto 68.77092 31.22908
prop.table(tabela_idade, 2)*100
##
## Não sobreviveu Sobreviveu
## criança 3.489933 8.028169
## adulto 96.510067 91.971831
(com base na linha, que diz o total na classe) # teoria verificada em parte
barplot(tabela_classe, beside=TRUE, col = c("pink", "hotpink", "skyblue", "green"), main = "Sobrevivência do Titanic", legend.text = rownames(tabela_classe))
?barplot
## starting httpd help server ... done
Aqui eu mudei a ordem entre as variáveis Classe e Sobreviveu
tabela_classe2 = table(Titanic$Sobreviveu, Titanic$Classe)
tabela_classe2
##
## Primeira Segunda Terceira Tripulação
## Não sobreviveu 122 167 528 673
## Sobreviveu 202 118 178 212
barplot(tabela_classe2, beside=TRUE, col = c("pink", "hotpink"), main = "Sobrevivência do Titanic", legend.text = rownames(tabela_classe2))
Realmente teve uma desigualdade de sobrevivência no Titanic
#PARTE2
#Carregando a base de dados
#------------------------------------
# Carregar a biblioteca
#------------------------------------
library(electionsBR)
## Warning: package 'electionsBR' was built under R version 4.2.3
##
## To cite electionsBR in publications, use: citation('electionsBR')
## To learn more, visit: http://electionsbr.com
electionsBR::parties_br()
## [1] "AVANTE" "CIDADANIA" "DC" "DEM"
## [5] "MDB" "NOVO" "PATRIOTA" "PC do B"
## [9] "PCB" "PCO" "PDT" "PEN"
## [13] "PHS" "PMB" "PMN" "PODE"
## [17] "PP" "PPL" "PPS" "PR"
## [21] "PRB" "PROS" "PRP" "PRTB"
## [25] "PSB" "PSC" "PSD" "PSDB"
## [29] "PSDC" "PSL" "PSOL" "PSTU"
## [33] "PT" "PT do B" "PTB" "PTC"
## [37] "PTN" "PV" "REDE" "REPUBLICANOS"
## [41] "SD" "SOLIEDARIEDADE" "UP"
# Carregar a base de dados
banco2 = candidate_fed(year = 2018, uf="RJ")
## Processing the data...
## Done.
table(banco2$DS_CARGO)
##
## 1º SUPLENTE 2º SUPLENTE DEPUTADO ESTADUAL DEPUTADO FEDERAL
## 17 18 2465 1154
## GOVERNADOR SENADOR VICE-GOVERNADOR
## 14 17 14
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## 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
%>% (operador pipe: significa ao mesmo tempo)
dep_fed_2018 = banco2 %>% filter(DS_CARGO=="DEPUTADO FEDERAL")
dep_est_2018 = banco2 %>% filter(DS_CARGO=="DEPUTADO ESTADUAL")
###Tabela Deputado
tabela_dep_fed = table(dep_fed_2018$SG_PARTIDO, dep_fed_2018$DS_GENERO)
tabela_dep_fed
##
## FEMININO MASCULINO
## AVANTE 16 33
## DC 17 32
## DEM 7 16
## MDB 8 14
## NOVO 6 13
## PATRIOTA 13 22
## PC do B 3 6
## PCB 1 0
## PCO 1 1
## PDT 20 34
## PHS 21 47
## PMB 14 26
## PMN 5 15
## PODE 15 26
## PP 4 10
## PPL 15 36
## PPS 4 10
## PR 7 22
## PRB 18 43
## PROS 3 11
## PRP 22 48
## PRTB 18 38
## PSB 3 15
## PSC 18 38
## PSD 19 43
## PSDB 7 14
## PSL 21 48
## PSOL 15 33
## PSTU 0 1
## PT 8 17
## PTB 3 11
## PTC 7 23
## PV 4 16
## REDE 5 10
## SOLIDARIEDADE 10 24
prop.table(tabela_dep_fed, 1)*100
##
## FEMININO MASCULINO
## AVANTE 32.65306 67.34694
## DC 34.69388 65.30612
## DEM 30.43478 69.56522
## MDB 36.36364 63.63636
## NOVO 31.57895 68.42105
## PATRIOTA 37.14286 62.85714
## PC do B 33.33333 66.66667
## PCB 100.00000 0.00000
## PCO 50.00000 50.00000
## PDT 37.03704 62.96296
## PHS 30.88235 69.11765
## PMB 35.00000 65.00000
## PMN 25.00000 75.00000
## PODE 36.58537 63.41463
## PP 28.57143 71.42857
## PPL 29.41176 70.58824
## PPS 28.57143 71.42857
## PR 24.13793 75.86207
## PRB 29.50820 70.49180
## PROS 21.42857 78.57143
## PRP 31.42857 68.57143
## PRTB 32.14286 67.85714
## PSB 16.66667 83.33333
## PSC 32.14286 67.85714
## PSD 30.64516 69.35484
## PSDB 33.33333 66.66667
## PSL 30.43478 69.56522
## PSOL 31.25000 68.75000
## PSTU 0.00000 100.00000
## PT 32.00000 68.00000
## PTB 21.42857 78.57143
## PTC 23.33333 76.66667
## PV 20.00000 80.00000
## REDE 33.33333 66.66667
## SOLIDARIEDADE 29.41176 70.58824
# Gráfico Tabela Deputados
barplot(tabela_dep_fed)
# traposta de dados
tabela_dep_fed2 = table(dep_fed_2018$DS_GENERO, dep_fed_2018$SG_PARTIDO)
tabela_dep_fed2
##
## AVANTE DC DEM MDB NOVO PATRIOTA PC do B PCB PCO PDT PHS PMB PMN
## FEMININO 16 17 7 8 6 13 3 1 1 20 21 14 5
## MASCULINO 33 32 16 14 13 22 6 0 1 34 47 26 15
##
## PODE PP PPL PPS PR PRB PROS PRP PRTB PSB PSC PSD PSDB PSL PSOL PSTU
## FEMININO 15 4 15 4 7 18 3 22 18 3 18 19 7 21 15 0
## MASCULINO 26 10 36 10 22 43 11 48 38 15 38 43 14 48 33 1
##
## PT PTB PTC PV REDE SOLIDARIEDADE
## FEMININO 8 3 7 4 5 10
## MASCULINO 17 11 23 16 10 24
barplot(tabela_dep_fed2, col = c("purple", "pink2"),
legend.text = rownames(tabela_dep_fed2),
ylim = c(0,100))
table(dep_fed_2018$DS_SIT_TOT_TURNO)
##
## #NULO# ELEITO POR MÉDIA ELEITO POR QP NÃO ELEITO
## 45 8 38 265
## SUPLENTE
## 798
dep_fed_eleito_2018 = dep_fed_2018 %>% filter(DS_SIT_TOT_TURNO=="ELEITO POR MÉDIA"|DS_SIT_TOT_TURNO=="ELEITO POR QP")
tabela_eleitos = table(dep_fed_eleito_2018$DS_GENERO, dep_fed_eleito_2018$SG_PARTIDO)
barplot(tabela_eleitos, col=c("plum", "skyblue"))
dep_fed_eleito_2018 = dep_fed_2018 %>% filter(DS_SIT_TOT_TURNO=="ELEITO POR MÉDIA"|DS_SIT_TOT_TURNO=="ELEITO POR QP")
tabela_cor = table(dep_fed_eleito_2018$DS_COR_RACA, dep_fed_eleito_2018$SG_PARTIDO)
barplot(tabela_cor)
dep_est_2018 %>% filter(DS_SIT_TOT_TURNO=="ELEITO POR MÉDIA"|DS_SIT_TOT_TURNO=="ELEITO POR QP") %>%
select(DS_COR_RACA, SG_PARTIDO) %>% table() %>% prop.table(2)
## SG_PARTIDO
## DS_COR_RACA AVANTE DC DEM MDB NOVO PATRIOTA
## BRANCA 1.0000000 0.5000000 0.8000000 1.0000000 1.0000000 0.0000000
## PARDA 0.0000000 0.5000000 0.2000000 0.0000000 0.0000000 1.0000000
## PRETA 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## SG_PARTIDO
## DS_COR_RACA PC do B PDT PHS PMB PODE PP
## BRANCA 0.0000000 1.0000000 0.5000000 1.0000000 0.0000000 1.0000000
## PARDA 0.0000000 0.0000000 0.5000000 0.0000000 1.0000000 0.0000000
## PRETA 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## SG_PARTIDO
## DS_COR_RACA PPS PR PRB PROS PRP PRTB
## BRANCA 1.0000000 1.0000000 0.6666667 0.0000000 1.0000000 1.0000000
## PARDA 0.0000000 0.0000000 0.3333333 1.0000000 0.0000000 0.0000000
## PRETA 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## SG_PARTIDO
## DS_COR_RACA PSB PSC PSD PSDB PSL PSOL
## BRANCA 0.5000000 0.5000000 1.0000000 1.0000000 0.5000000 0.4000000
## PARDA 0.5000000 0.5000000 0.0000000 0.0000000 0.5000000 0.0000000
## PRETA 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.6000000
## SG_PARTIDO
## DS_COR_RACA PT PTB PTC SOLIDARIEDADE
## BRANCA 1.0000000 1.0000000 1.0000000 0.3333333
## PARDA 0.0000000 0.0000000 0.0000000 0.6666667
## PRETA 0.0000000 0.0000000 0.0000000 0.0000000
dep_fed_eleito_2018 %>%
select(DS_COR_RACA, DS_GENERO) %>%
table() %>%
barplot(beside=T, col=c("hotpink", "skyblue", "pink1"))
senador = banco2 %>% filter(DS_CARGO=="SENADOR")
table(senador$DS_GENERO)
##
## FEMININO MASCULINO
## 4 13
prop.table(table(senador$DS_GENERO))
##
## FEMININO MASCULINO
## 0.2352941 0.7647059
gov = banco2 %>% filter(DS_CARGO=="GOVERNADOR")