A Monitoria de Econometria
Por: hhhhgms • 14/12/2022 • Exam • 1.061 Palavras (5 Páginas) • 87 Visualizações
Monitoria 3 - Gráficos e Regressão Linear Simples
Gustavo Romero Cardoso e João Pedro Saldanha Corrêa
2022-08-29
library(tidyverse)
[pic 1]
- Warning: package ’tidyverse’ was built under R version 4.2.1
- -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.6 | v purrr | 0.3.4 | |
## v tibble | 3.1.7 | v dplyr | 1.0.9 |
## v tidyr | 1.2.0 | v stringr 1.4.0 | |
## v readr | 2.1.2 | v forcats 0.5.1 |
- Warning: package ’ggplot2’ was built under R version 4.2.1
- Warning: package ’readr’ was built under R version 4.2.1
- -- Conflicts ------------------------------------------ tidyverse_conflicts() --
- x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Parte 1 - Manipulação de bases
Contando observações em grupos
[pic 2]
data <- gss_cat
# Solução 1
data %>% count(partyid)
## # A tibble: 10 x 2 | |||
## | partyid | n | |
## | <fct> | <int> | |
## | 1 | No answer | 154 |
## | 2 | Don’t know | 1 |
## | 3 | Other party | 393 |
## | 4 | Strong republican | 2314 |
- 5 Not str republican 3032
## | 6 | Ind,near rep | 1791 |
## | 7 | Independent | 4119 |
## | 8 | Ind,near dem | 2499 |
## | 9 | Not str democrat | 3690 |
## 10 | Strong democrat | 3490 |
1
# Solução 2
[pic 3]
data %>% group_by(partyid) %>% tally()
## # A tibble: 10 x 2 | |||
## | partyid | n | |
## | <fct> | <int> | |
## | 1 | No answer | 154 |
## | 2 | Don’t know | 1 |
## | 3 | Other party | 393 |
## | 4 | Strong republican | 2314 |
- 5 Not str republican 3032
## | 6 | Ind,near rep | 1791 |
## | 7 | Independent | 4119 |
## | 8 | Ind,near dem | 2499 |
## | 9 | Not str democrat | 3690 |
## 10 | Strong democrat | 3490 |
- possível combinar grupos e aplicar o count(). No exemplo abaixo, juntamos 3 tipos de respostas em apenas um grupo.
gss_cat %>% mutate(partyid = fct_collapse(partyid, outros = c("No answer",
[pic 4]
"Don't know",
"Other party"))) %>%
count(partyid)
## # A tibble: 8 x 2 | ||
## | partyid | n |
## | <fct> | <int> |
## 1 outros | 548 | |
## 2 Strong republican | 2314 | |
## 3 Not str republican | 3032 | |
## 4 Ind,near rep | 1791 | |
## 5 Independent | 4119 | |
## 6 Ind,near dem | 2499 | |
## 7 Not str democrat | 3690 | |
## 8 Strong democrat | 3490 |
data %>% group_by(relig) %>% count()
[pic 5]
- # A tibble: 15 x 2
- # Groups: relig [15]
## | relig | n | |
## | <fct> | <int> | |
## | 1 | No answer | 93 |
## | 2 | Don’t know | 15 |
- 3 Inter-nondenominational 109
## | 4 | Native american | 23 |
## | 5 | Christian | 689 |
## | 6 | Orthodox-christian | 95 |
## | 7 | Moslem/islam | 104 |
## | 8 | Other eastern | 32 |
...