-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2a_matching_public_private_add_columns.R
394 lines (328 loc) · 15.9 KB
/
2a_matching_public_private_add_columns.R
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
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
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
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
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
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
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
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
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
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
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
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
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
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
394
##############################################
#### unir outras colunas na base de match ####
##############################################
# abrir base final com match
# para cada ano, ficar só com PIS e CBO e fazer lista
# abrir a base da RAIS de cada ano e selecionar as colunas de interesse
# filtrar na RAIS de cada ano esses PIS-CBO
# fazer uma lista dos CNPJs de cada ano
# abrir a base da RAIS estabelecimentos e ficar com CNPJ e Nome
# juntar o nome na base de indivíduos.
#####################
#### DEFINITIONS ####
#####################
rm(list=ls())
gc()
library("tidyverse")
library("readxl")
library("data.table")
# Para facilitar
select <- dplyr::select
filter <- dplyr::filter
`%notin%` = Negate(`%in%`)
personal_directory <- "C:/Users/taina/Desktop/RAIS/matching/dec2021/"
directory <- "C:/Users/taina/Desktop/RAIS/"
years <- as.character(c(1998:2004))
##################################
#### FUNCTION TO OPEN RAIS DF ####
##################################
open_files <- function(years_files = NA_character_, file_to_read = NA_character_){
if(years_files == "2002"){
df <- fread(file_to_read,
sep=";",
header=TRUE,
na.strings="NA",
showProgress = TRUE,
colClasses = "character",
select = c("OCUPACAO", "PIS",
"DT ADMISSAO", "RADIC CNPJ", "GRAU INSTR", "MES DESLIG", "CAUSA DESLI", "MUNICIPIO",
"SEXO", "TEMP EMPR", "TP VINCL", "EMP EM 31/12", "DT NASCIMENT")) %>%
rename(cbo = `OCUPACAO`,
date_adm = `DT ADMISSAO`,
CNPJ_root = `RADIC CNPJ`,
educ = `GRAU INSTR`,
month_quit = `MES DESLIG`,
reason_quit = `CAUSA DESLI`,
municipality = MUNICIPIO,
gender = SEXO,
time_employed = `TEMP EMPR`,
tp_contract = `TP VINCL`,
employed_dec = `EMP EM 31/12`,
b_date = `DT NASCIMENT`) %>%
mutate(cbo = str_replace(cbo, "CBO ", ""),
age = as.numeric(years_files) - as.numeric(substr(b_date,(nchar(b_date)+1)-4,nchar(b_date))),
date_adm = ifelse(nchar(date_adm) == 7, paste0("0", date_adm), date_adm),
date_adm = as.Date(date_adm, format = "%d%m%Y"),
date_quit = ifelse(month_quit == "00", NA,
as.Date(paste0("30/", month_quit, "/", years_files), format = "%d/%m/%Y")),
CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
select(-b_date, -month_quit)
} else if (years_files %in% as.character(1995:1998)){
df <- fread(file_to_read,
sep=";",
header=TRUE,
na.strings="NA",
showProgress = TRUE,
colClasses = "character",
select = c("CBO 94 Ocupação", "PIS",
"Ano Admissão", "CNPJ Raiz", "Grau Instrução 2005-1985", "Mês Admissão",
"Mês Desligamento", "Motivo Desligamento", "Município", "Sexo Trabalhador", "Tempo Emprego",
"Tipo Vínculo", "Vínculo Ativo 31/12", "Idade", "Tipo Admissão")) %>%
rename(cbo = `CBO 94 Ocupação`,
year_adm = `Ano Admissão`,
CNPJ_root = `CNPJ Raiz`,
educ = `Grau Instrução 2005-1985`,
month_adm = `Mês Admissão`,
month_quit = `Mês Desligamento`,
reason_quit = `Motivo Desligamento`,
municipality = `Município`,
gender = `Sexo Trabalhador`,
time_employed = `Tempo Emprego`,
tp_contract = `Tipo Vínculo`,
employed_dec = `Vínculo Ativo 31/12`,
age = `Idade`) %>%
mutate(cbo = str_replace(cbo, "CBO ", ""),
date_adm = as.Date(paste0("01/", month_adm, "/", year_adm), format = "%d/%m/%Y"),
date_quit = ifelse(month_quit == "00", NA,
as.Date(paste0("30/", month_quit, "/", years_files), format = "%d/%m/%Y")),
CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
select(-month_adm, -year_adm, -month_quit)
} else if (years_files %in% as.character(c(1999:2001))){
df <- fread(file_to_read,
sep=";",
header=TRUE,
na.strings="NA",
showProgress = TRUE,
colClasses = "character",
select = c("CBO 94 Ocupação", "PIS",
"Ano Admissão", "CNPJ Raiz", "Grau Instrução 2005-1985", "Mês Admissão",
"Mês Desligamento", "Motivo Desligamento", "Município", "Sexo Trabalhador",
"Tempo Emprego", "Tipo Vínculo", "Vínculo Ativo 31/12", "Idade", "Tipo Admissão")) %>%
rename(cbo = `CBO 94 Ocupação`,
year_adm = `Ano Admissão`,
CNPJ_root = `CNPJ Raiz`,
educ = `Grau Instrução 2005-1985`,
month_adm = `Mês Admissão`,
month_quit = `Mês Desligamento`,
reason_quit = `Motivo Desligamento`,
municipality = `Município`,
gender = `Sexo Trabalhador`,
time_employed = `Tempo Emprego`,
tp_contract = `Tipo Vínculo`,
employed_dec = `Vínculo Ativo 31/12`,
age = `Idade`)%>%
mutate(cbo = str_replace(cbo, "CBO ", ""),
date_adm = as.Date(paste0("01/", month_adm, "/", year_adm), format = "%d/%m/%Y"),
date_quit = ifelse(month_quit == "00", NA,
as.Date(paste0("30/", month_quit, "/", years_files), format = "%d/%m/%Y")),
CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
select(-month_adm, -year_adm, -month_quit)
} else{
df <- fread(file_to_read,
sep=";",
header=TRUE,
na.strings="NA",
showProgress = TRUE,
colClasses = "character",
select = c("OCUPACAO 94", "PIS",
"HORAS CONTR", "REM DEZ (R$)", "REM MED (R$)", "ULT REM", "SAL CONTR"
,"DT ADMISSAO", "RADIC CNPJ", "GRAU INSTR", "MES DESLIG", "CAUSA DESLI",
"MUNICIPIO", "SEXO", "TEMP EMPR", "TP VINCULO", "EMP EM 31/12", "DT NASCIMENT")) %>%
rename(cbo = `OCUPACAO 94`,
date_adm = `DT ADMISSAO`,
CNPJ_root = `RADIC CNPJ`,
educ = `GRAU INSTR`,
month_quit = `MES DESLIG`,
reason_quit = `CAUSA DESLI`,
municipality = MUNICIPIO,
gender = SEXO,
time_employed = `TEMP EMPR`,
tp_contract = `TP VINCULO`,
employed_dec = `EMP EM 31/12`,
b_date = `DT NASCIMENT`) %>%
mutate(cbo = str_replace(cbo, "CBO ", ""),
age = as.numeric(years_files) - as.numeric(substr(b_date,(nchar(b_date)+1)-4,nchar(b_date))),
date_adm = ifelse(nchar(date_adm) == 7, paste0("0", date_adm), date_adm),
date_adm = as.Date(date_adm, format = "%d%m%Y"),
date_quit = ifelse(month_quit == "00", NA,
as.Date(paste0("30/", month_quit, "/", years_files), format = "%d/%m/%Y")),
CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
select(-b_date, -month_quit)
}
return(df)
}
####################################################################
#### OPEN FINAL MATCH DF TO GET PIS AND CBO OF EACH OBSERVATION ####
####################################################################
list_PIS_cbo <- list()
for (i in seq_along(years)) {
list_PIS_cbo[[i]] <- readRDS(paste0(personal_directory, "list_final_df/final_df_", years[i], ".RDS")) %>%
select(cbo, PIS) %>%
mutate(PIS_cbo = paste0(PIS, "-", cbo)) %>%
pull(PIS_cbo)
}
##########################################################
#### OPEN RAIS AND FILTER FOR THE PIS-CBO OF INTEREST ####
##########################################################
list_cnpj <- list()
for(i in seq_along(years)){
print(years[i])
# list files for each year
files <- grep(list.files(path = paste0(directory, years[i]),
all.files = FALSE,
full.names = TRUE),
pattern= ('Estb|ESTB|IGNORADO'),
inv=TRUE, value=TRUE)
PIS_cbo_vec <- list_PIS_cbo[[i]]
list_data_temp <- list()
for(j in seq_along(files)) {
# open RAIS file
list_data_temp[[j]] <- open_files(years_file = years[i], file_to_read = files[j]) %>%
# filter for corresponding PIS-cbo
mutate(PIS_cbo = paste0(PIS, "-", cbo)) %>%
filter(PIS_cbo %in% PIS_cbo_vec)
print(j)
}
#combine all RAIS data
df_RAIS <- bind_rows(list_data_temp) %>% select(-PIS_cbo)
# open matched data
df_match <- readRDS(paste0(personal_directory, "list_final_df/final_df_", years[i], ".RDS")) %>%
# merge RAIS columns into matched df
left_join(df_RAIS, by = c("PIS", "cbo"))
# save a "full df" for each year
saveRDS(df_match, paste0(personal_directory, "list_final_df_full/final_df_full_", years[i], ".RDS"))
# save all CNPJ for each year
list_cnpj[[i]] <- df_RAIS %>% distinct(CNPJ_root) %>% pull(CNPJ_root)
# clean environment
remove(df_RAIS, df_match, list_data_temp)
}
#######################################################
#### GETTING CNPJ NAMES FROM RAIS ESTABELECIMENTOS ####
#######################################################
nat_jur_fed <- as.character(c(1015, 1040, 1074, 1104, 1139, 1163, 1252, 10))
years_names_cnpj <- c("2002", "2003", "2004")
# SÓ TEM NOME EM 2002, 2003 e 2004, e parece que só em 2002 está correto
list_cnpj_names <- list()
for(i in seq_along(years_names_cnpj)){
print(years_names_cnpj[i])
# list files for each year
files <- grep(list.files(path = paste0(directory, years_names_cnpj[i]),
all.files = FALSE,
full.names = TRUE),
pattern= ('Estb|ESTB'),
inv=FALSE, value=TRUE)
list_files <- list()
for (j in seq_along(files)) {
list_files[[j]] <- read_delim(files[j],
"|",
escape_double = FALSE,
col_types = cols(`CLAS CNAE 95` = col_character(),
`NATUR JUR` = col_character(),
`RADIC CNPJ` = col_character(),
`RAZAO SOCIAL` = col_character()),
locale = locale(decimal_mark = ",", grouping_mark = ".",
encoding = "WINDOWS-1252"),
trim_ws = TRUE) %>%
rename(CNPJ_root = `RADIC CNPJ`,
agency_name = `RAZAO SOCIAL`,
cnae95 = `CLAS CNAE 95`,
natjur = `NATUR JUR`) %>%
select(CNPJ_root, agency_name, cnae95, natjur) %>%
filter(natjur %in% nat_jur_fed & cnae95 == "75116")
}
list_cnpj_names[[i]] <- bind_rows(list_files) %>%
mutate(year = years_names_cnpj[i],
CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
filter(agency_name != "TESTE")
}
cnpj_names <- bind_rows(list_cnpj_names) %>%
count(CNPJ_root_std, agency_name) %>%
group_by(CNPJ_root_std) %>%
slice(which.max(n)) %>%
ungroup()
# adding an old dataset with CNPJ names (from code "analise_paula.Rmd")
rais_panel_public <- readRDS(paste0(directory,"_misc/rais_panel_public.Rds")) %>%
distinct(cnpj_raiz_std, name) %>%
rename(CNPJ_root_std = cnpj_raiz_std,
agency_name = name)
CNPJs_filter <- rais_panel_public$CNPJ_root_std
cnpj_names <- cnpj_names %>%
select(-n) %>%
filter(CNPJ_root_std %notin% CNPJs_filter) %>%
bind_rows(rais_panel_public) %>%
distinct(CNPJ_root_std, .keep_all = TRUE) %>%
mutate(agency_name = ifelse(agency_name == "ISC", "TRIBUNAL DE CONTAS DA UNIAO", agency_name))
write.csv2(cnpj_names, file = paste0(directory, "matching/dec2021/all_cnpj_names.csv"))
################################################
#### INCLUDING CNPJ NAMES IN THE FINAL DATA ####
################################################
for (i in seq_along(years)) {
df <- readRDS(file = paste0(personal_directory, "list_final_df_full/final_df_full_", years[i], ".RDS")) %>%
mutate(CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root)))) %>%
filter(PIS != "00000000000") %>%
distinct(PIS, cbo, CNPJ_root_std, .keep_all = TRUE) %>%
left_join(cnpj_names, by = "CNPJ_root_std")
df_cnpj_na <- df %>% filter(is.na(agency_name))
print(nrow(df_cnpj_na %>% distinct(CNPJ_root_std)))
print(nrow(df %>% distinct(CNPJ_root_std)))
# save a "full df" for each year
saveRDS(df, paste0(personal_directory, "list_final_df_full/final_df_full_", years[i], ".RDS"))
saveRDS(df_cnpj_na, paste0(personal_directory, "list_final_df_full/final_df_full_cnpj_na_", years[i], ".RDS"))
}
###############################################################
#### FILTERING ONLY FOR AGENCIES USED IN THE EXPERT SURVEY ####
###############################################################
load(paste0(directory, "matching/dec2021/agencies_manifesto.RData"))
write.csv2(ag_scores, file = paste0(directory, "matching/dec2021/all_agencies_exp_survey.csv"))
CNPJ_exp_sur <- read.csv2(paste0(directory, "matching/dec2021/all_cnpj_names_expert_survey.csv")) %>%
mutate(CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root_std)))) %>%
filter(expert_survey == 1) %>%
select(-X, -agency_name)
# nrow(CNPJ_exp_sur %>% distinct(agency_name)) # hoje tenho uma base com 70 nomes
agencies <- list()
for (i in seq_along(years)) {
print(years[i])
df <- readRDS(file = paste0(personal_directory, "list_final_df_full/final_df_full_", years[i], ".RDS"))
print(nrow(df))
df <- df %>%
left_join(CNPJ_exp_sur, by = "CNPJ_root_std") %>%
filter(expert_survey == 1)
print(nrow(df))
print(nrow(df %>% distinct(agency_name)))
CNPJ_df <- df %>% distinct(CNPJ_root_std) %>% pull(CNPJ_root_std)
agencies[[i]] <- CNPJ_exp_sur %>% filter(CNPJ_root_std %in% CNPJ_df) %>%
mutate(year = years[i])
# save a "full df" for each year
saveRDS(df, paste0(personal_directory, "list_final_df_full_exp_survey/final_df_full_exp_sur_", years[i], ".RDS"))
}
CNPJ_exp_sur <- read.csv2(paste0(directory, "matching/dec2021/all_cnpj_names_expert_survey.csv")) %>%
mutate(CNPJ_root_std = base::sprintf("%08.0f", as.numeric(as.character(CNPJ_root_std)))) %>%
filter(expert_survey == 1) %>%
select(-X, -expert_survey)
for(i in seq_along(agencies)){
df <- agencies[[i]] %>%
select(-expert_survey)
names(df) <- c("CNPJ_root_std", paste0("year_", years[i]))
CNPJ_exp_sur <- CNPJ_exp_sur %>%
left_join(df, by = "CNPJ_root_std")
}
write.csv2(CNPJ_exp_sur, file = paste0(directory, "matching/dec2021/all_cnpj_names_expert_survey_years.csv"))
####################################################
#### EXPORTING ANONIMIZED DATA TO SEND TO PAULA ####
####################################################
pis_vec <- c()
for (i in seq_along(years)) {
df <- readRDS(paste0(personal_directory, "list_final_df_full_exp_survey/final_df_full_exp_sur_", years[i], ".RDS"))
pis_vec <- unique(c(pis_vec, (df %>% pull(PIS))))
}
set.seed(05585040)
pis_ID <- tibble("PIS" = pis_vec) %>%
distinct(PIS) %>%
mutate(ID_fake = sample(length(pis_vec)))
for (i in seq_along(years)) {
df <- readRDS(paste0(personal_directory, "list_final_df_full_exp_survey/final_df_full_exp_sur_", years[i], ".RDS")) %>%
left_join(pis_ID, by = "PIS") %>%
select(-PIS)
saveRDS(df, paste0(personal_directory, "list_final_df_full_exp_survey_anon/final_df_full_exp_sur_anon_",
years[i], ".RDS"))
}