/
exploitation_fichier_INDCVI_INSEE.R
370 lines (313 loc) · 16.7 KB
/
exploitation_fichier_INDCVI_INSEE.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
## stockage dans dossier temporaire
url_data <- "https://www.insee.fr/fr/statistiques/fichier/2409376/rp2013_indcvi_txt.zip"
download.file(url_data, destfile = "/tmp/rp2013_indcvi_txt.zip")
#data <- read.table(unz(temp, "a1.dat"))
system("7z x -o/tmp /tmp/rp2013_indcvi_txt.zip")
FD_INDCVI_2013 <- fread( "/tmp/FD_INDCVI_2013.txt" ,
sep=";",
dec=".",
stringsAsFactors = FALSE,
header= TRUE,
colClasses=list(character=c("REGION2016","REGION","IRIS","IRAN","ILT","ILETUD","HLML","DNAI","DEPT","ARM","CANTVILLE")))
#############################
#############
## permiers comptages
# référentiel carto IRIS cohérent avec IRIS data
# sauf 800210601 non présent dans carto
# a transformer en 800210606
FD_INDCVI_2013_IDF <- FD_INDCVI_2013 %>%
filter(substr(CANTVILLE, 1, 2) %in% c('75','77','78','91','92','93','94','95'))
rm(FD_INDCVI_2013)
##############################
#### 1er type de cas : IRIS dont les 4 derniers digits ont été masqués pour cause de secret statistique
#### on affecte un code IRIS selon la répartition de la population dans les IRIS de la commmune (hors IRIS non XXXX)
### infos sur les IRIS qui permettront de résoudre ce type de cas
IRIS_input_cas_COMMXXXX <-
IRIS_FR_df %>%
left_join(IRISnew_RP2013 %>% select(CODE_IRIS, P13_POP), by = c("CODE_IRIS" = "CODE_IRIS")) %>%
select(CODE_IRIS, P13_POP) %>%
left_join(
FD_INDCVI_2013_IDF %>%
group_by(IRIS) %>%
summarise(nb_ind = sum(IPONDI)) ,
by = c("CODE_IRIS" = "IRIS")
) %>%
filter(is.na(nb_ind) & !substr(CODE_IRIS,6,9) %in% '0000') %>%
select(CODE_IRIS, P13_POP) %>%
mutate(DEPCOM = substr(CODE_IRIS,1,5))
# table de passage avec ratio de ventilation
PASSAGE_IRIS_DEPCOMXXXX_IRIS_DEPCOMIRIS <-
IRIS_input_cas_COMMXXXX %>%
left_join(
FD_INDCVI_2013_IDF %>%
group_by(IRIS) %>%
summarise(nb_ind = sum(IPONDI)) %>%
filter(grepl("XXX",IRIS)) %>%
mutate(DEPCOM = substr(IRIS,1,5)) ,
by = c("DEPCOM" = "DEPCOM")
) %>%
rename(codIRIS_DEPCOMIRIS = CODE_IRIS ,
codIRIS_DEPCOMXXXX = IRIS ) %>%
select(codIRIS_DEPCOMIRIS, codIRIS_DEPCOMXXXX, P13_POP) %>%
filter(!is.na(codIRIS_DEPCOMXXXX)) %>%
group_by(codIRIS_DEPCOMXXXX) %>%
mutate(P13_POP_tot= sum(P13_POP)) %>%
mutate(ratio=P13_POP / P13_POP_tot) %>%
select(codIRIS_DEPCOMIRIS, codIRIS_DEPCOMXXXX) %>%
mutate(annee = 10,
typemodif = "d") %>%
left_join(tablecorresp_IRIS_ratio %>% select(codIRIS_DEPCOMIRIS, codIRIS_DEPCOMXXXX,ratio),
by = c("codIRIS_DEPCOMIRIS" = "codIRIS_DEPCOMIRIS", "codIRIS_DEPCOMXXXX" = "codIRIS_DEPCOMXXXX")) %>%
mutate(typemodif = ifelse(ratio == 1, "c", typemodif))
### RECTIFICATION DES LIGNES AVEC IRIS COMMXXXX
### fonction dérivée du package COGugaison : https://github.com/antuki/COGugaison
changement_COG_numerique_modif <- function (table_entree, annees, codgeo_entree = colnames(table_entree)[1],
var_num = colnames(table_entree)[sapply(table_entree, is.numeric)],
agregation = T)
{
for (i in 1:(length(annees) - 1)) {
provisoire <- merge(table_entree, get(paste0("PASSAGE_",
annees[i], "_", annees[i + 1])), by.x = codgeo_entree,
by.y = paste0("cod", annees[i]), all.x = T, all.y = F)
provisoire[which(is.na(with(provisoire, get(paste0("cod",
annees[i + 1]))))), "ratio"] <- 1
provisoire[which(is.na(with(provisoire, get(paste0("cod",
annees[i + 1]))))), paste0("cod", annees[i +
1])] <- as.character(provisoire[which(is.na(with
(provisoire,
get
(paste0("cod",
annees[i + 1]))))), codgeo_entree])
provisoire[, c(var_num)] <- (provisoire[, c(var_num,
"ratio")] * provisoire[, "ratio"])[, -(length(var_num) +
1)]
provisoire <- provisoire[, -which(colnames(provisoire) ==
codgeo_entree)]
provisoire <- provisoire[, -((ncol(provisoire) -
2):ncol(provisoire))]
names(provisoire)[which(names(provisoire) == paste0("cod",
annees[i + 1]))] <- codgeo_entree
table_finale <- provisoire[, colnames(table_entree)]
table_entree <- table_finale
}
if (agregation) {
table_finale <- aggregate(table_finale[, c(var_num)],
by = list(with(table_finale, get(codgeo_entree))),
FUN = sum)
colnames(table_finale) <- c(codgeo_entree, var_num)
}
table_finale <- table_finale[order(table_finale[, codgeo_entree]),
]
return(table_finale)
}
# traitement du sous ensemble de la table de détail avec IRIS COMMXXXX
FD_INDCVI_2013_IRISXXXX_RECOD <-
changement_COG_numerique_modif(table_entree=FD_INDCVI_2013_IDF %>%
filter(substr(IRIS,6,9) %in% 'XXXX' & !IRIS %in% 'ZZZZZZZZZ'),
annees=c("IRIS_DEPCOMXXXX","IRIS_DEPCOMIRIS"),
codgeo_entree = "IRIS",
agregation = F,
var_num = "IPONDI")
##############################
#### 2ème type de cas : IRIS non renseignés (ZZZZZZZZZ) car commune non irisées
#### on affecte un code IRIS (code commune + 0000) selon la répartition de la population du canton dans chaque commune
### infos sur les IRIS qui permettront de résoudre ce type de cas
IRIS_input_cas_ZZZZZZZZZ <-
IRIS_FR_df %>% filter(substr(CODE_IRIS, 1, 2) %in% c('75','77','78','91','92','93','94','95')) %>%
left_join(IRISnew_RP2013 %>% select(CODE_IRIS, P13_POP), by = c("CODE_IRIS" = "CODE_IRIS")) %>%
mutate(DEPCOM = substr(CODE_IRIS,1,5)) %>%
## MODIFS !!
mutate(DEPCOM = ifelse(substr(DEPCOM,1,2) == '75' ,'75056',
ifelse(substr(DEPCOM,1,3) == '132' ,'13055',
ifelse(substr(DEPCOM,1,4) == '6938' ,'69123',DEPCOM))) ) %>%
left_join(COMM2015_CANTON,
by = c("DEPCOM" = "CODGEO")) %>%
left_join(
FD_INDCVI_2013_IDF %>%
filter(IRIS %in% 'ZZZZZZZZZ') %>%
group_by(CANTVILLE) %>%
summarise(nb_ind = sum(IPONDI)),
by = c("CV" = "CANTVILLE")
) %>%
filter(!is.na(nb_ind)) %>%
group_by(CV) %>%
mutate(P13_POP_tot= sum(P13_POP)) %>%
mutate(ratio=P13_POP / P13_POP_tot) %>%
mutate(codCAN_ZZZZZZZZZ = paste0(CV,"_ZZZZZZZZZ"),
codCAN_DEPCOMIRIS = paste0(CV,"_",CODE_IRIS ))
# table de passage avec ratio de ventilation
PASSAGE_CAN_ZZZZZZZZZ_CAN_DEPCOMIRIS <-
IRIS_input_cas_ZZZZZZZZZ %>%
ungroup() %>%
select(codCAN_DEPCOMIRIS, codCAN_ZZZZZZZZZ) %>%
mutate(annee = 10,
typemodif = "d") %>%
left_join(IRIS_input_cas_ZZZZZZZZZ %>% ungroup() %>% select(codCAN_DEPCOMIRIS, codCAN_ZZZZZZZZZ,ratio),
by = c("codCAN_DEPCOMIRIS" = "codCAN_DEPCOMIRIS", "codCAN_ZZZZZZZZZ" = "codCAN_ZZZZZZZZZ")) %>%
mutate(typemodif = ifelse(ratio == 1, "c", typemodif))
# traitement du sous ensemble de la table de détail avec IRIS ZZZZZZZZZ
FD_INDCVI_2013_ZZZZZZZZZ_RECOD <-
changement_COG_numerique_modif(table_entree=FD_INDCVI_2013_IDF %>%
filter(IRIS %in% 'ZZZZZZZZZ') %>%
mutate(CAN_ZZZZZZZZZ = paste0(CANTVILLE, "_ZZZZZZZZZ")),
annees=c("CAN_ZZZZZZZZZ","CAN_DEPCOMIRIS"),
codgeo_entree = "CAN_ZZZZZZZZZ",
agregation = F,
var_num = "IPONDI")
##############################
#### 3ème type de cas : IRIS bien codés
#### on les récupère tels quels
FD_INDCVI_2013_IRISPROPRES <-
FD_INDCVI_2013_IDF %>%
filter(!substr(IRIS,6,9) %in% 'XXXX' & !IRIS %in% 'ZZZZZZZZZ')
FD_INDCVI_2013 %>% filter(substr(IRIS,1,2) %in% '75') %>% distinct(IRIS) %>% View()
####
## AGGREGATION DES 3 SOUS PARTIES
FD_INDCVI_2013_IRISOK_IDF <-
FD_INDCVI_2013_IRISPROPRES %>%
rename(IRIS_estime = IRIS) %>%
rbind(FD_INDCVI_2013_ZZZZZZZZZ_RECOD %>%
select(-IRIS) %>%
rename(IRIS_estime = CAN_ZZZZZZZZZ) %>%
mutate(IRIS_estime = substr(IRIS_estime,6,14))) %>%
rbind(FD_INDCVI_2013_IRISXXXX_RECOD %>%
rename(IRIS_estime = IRIS))
# verif cohérence avec IRIS P13_POP
test_IRIS <-
IRIS_RP2013_POP %>% select(IRIS, P13_POP) %>%
left_join(
FD_INDCVI_2013_IRISOK_IDF %>%
group_by(IRIS_estime) %>%
summarise(nb = sum(IPONDI)), by = c("IRIS" = "IRIS_estime")) %>%
filter(substr(IRIS,1,2) %in% '75') %>%
mutate(iso = ifelse(round(P13_POP) == round(nb), "iso",NA),
diff = P13_POP - nb)
#################################################
######## calcul des indicateurs de diasporas
# référentiels départements et régions
url_dep <- "https://www.insee.fr/fr/statistiques/fichier/2560698/depts2015-txt.zip"
download.file(url_dep, destfile = "/tmp/depts2015-txt.zip")
system("7z x -o/tmp /tmp/depts2015-txt.zip")
url_reg <- "https://www.insee.fr/fr/statistiques/fichier/2560698/reg2015-txt.zip"
download.file(url_reg, destfile = "/tmp/reg2015-txt.zip")
system("7z x -o/tmp /tmp/reg2015-txt.zip")
ref_reg <- fread( "/tmp/reg2015.txt" ,sep="\t", stringsAsFactors = FALSE,header= TRUE, colClasses=list(character=c("REGION","CHEFLIEU")), encoding = 'UTF-8')
ref_dep <- fread( "/tmp/depts2015.txt" ,sep="\t", stringsAsFactors = FALSE,header= TRUE, colClasses=list(character=c("REGION","DEP", "CHEFLIEU")))
REF_DEP_REG <- ref_dep %>%
select(-CHEFLIEU, - TNCC) %>%
rename(NCC_dep = NCC, NCCENR_dep = NCCENR) %>%
left_join(ref_reg %>%
select(-CHEFLIEU, - TNCC) %>%
rename(NCC_reg = NCC, NCCENR_reg = NCCENR),
by = "REGION")
IRIS_RP2013_DIASPORAS_REG <-
IRIS_RP2013_POP %>% select(IRIS, P13_POP) %>%
filter(substr(IRIS,1,2) %in% c('75','77','78','91','92','93','94','95')) %>%
left_join(
FD_INDCVI_2013_IRISOK_IDF %>%
left_join(REF_DEP_REG %>%
select(DEP, REGION) %>%
rename(REGION_NAI = REGION),
by = c("DNAI" = "DEP")) %>%
mutate(REGION_NAI = ifelse(INAI == 5, "TOMCOM", ifelse(INAI == 6, "ETRANGER",as.character(REGION_NAI) ))) %>%
group_by(IRIS_estime, REGION_NAI) %>%
summarise(nb = sum(IPONDI)) %>%
mutate(REGION_NAI = paste0("REGNAI_", REGION_NAI)) %>%
spread( REGION_NAI, nb) %>%
mutate_each(funs(replace(.,is.na(.),0))), by = c("IRIS" = "IRIS_estime")) %>%
mutate_each(funs(replace(.,is.na(.),0)))
# ORIGINE_DNAI
conv_accents_2 <- function(x) {
x <- gsub(pattern = "\xe9", replacement = "é", x = x)
x <- gsub(pattern = "\xce", replacement = "I", x = x)
x <- gsub(pattern = "\xf4", replacement = "ô", x = x)
x <- gsub(pattern = "\xe8", replacement = "è", x = x)
return(x)
}
REF_PAYS_REG_DEP_ARBO <-
REF_DEP_REG %>%
mutate(NCCENR_dep = conv_accents_2(NCCENR_dep), NCCENR_reg = conv_accents_2(NCCENR_reg)) %>%
rbind(c(NA, '975', 'Saint-Pierre-et-Miquelon', 'Saint-Pierre-et-Miquelon', NA, NA),
c(NA, '977', 'Saint-Barthélemy', 'Saint-Barthélemy', NA, NA),
c(NA, '978', 'Saint-Martin', 'Saint-Martin', NA, NA),
c(NA, '98', 'Autres Collectivités Outre-Mer', 'Autres Collectivités Outre-Mer', NA, NA),
c(NA, '99', 'Etranger', 'Etranger', NA, NA)
) %>%
mutate(NCCENR_reg = ifelse(DEP %in% c('971','972','973','974','975','976','977','978','98'),"Outre-Mer",
ifelse(DEP %in% '99', "Etranger", as.character(NCCENR_reg)))) %>%
mutate(NCCENR_pays = ifelse(NCCENR_reg %in% 'Etranger', 'Etranger', 'France')) %>%
mutate(NCCENR_reg = gsub("-"," ",NCCENR_reg),NCCENR_dep = gsub("-"," ",NCCENR_dep))
# volume des parisiens selon lieu de naissance
DNAI_SEQ_PARIS <-
FD_INDCVI_2013_IRISOK_IDF %>%
filter(substr(IRIS_estime,1,2) %in% '75') %>%
group_by(DNAI) %>%
summarise(nb = sum(IPONDI)) %>%
mutate(nb = round(nb)) %>%
left_join(REF_PAYS_REG_DEP_ARBO, by = c("DNAI" = "DEP")) %>%
mutate(zone = paste0("PAYS-",NCCENR_pays, "-", NCCENR_reg, "-", NCCENR_dep)) %>%
mutate(zone = ifelse(zone %in% 'PAYS-Etranger-Etranger-Etranger','PAYS-Etranger',as.character(zone) )) %>%
select(zone, nb)
DNAI_SEQ_93 <-
FD_INDCVI_2013_IRISOK_IDF %>%
filter(substr(IRIS_estime,1,5) %in% '93048') %>%
group_by(DNAI) %>%
summarise(nb = sum(IPONDI)) %>%
mutate(nb = round(nb)) %>%
left_join(REF_PAYS_REG_DEP_ARBO, by = c("DNAI" = "DEP")) %>%
mutate(zone = paste0(NCCENR_pays, "-", NCCENR_reg, "-", NCCENR_dep)) %>%
#mutate(zone = ifelse(zone %in% 'Etranger-Etranger-Etranger','Etranger',as.character(zone) )) %>%
select(zone, nb)
# suppression fichiers temporaires
rm(FD_INDCVI_2013_IDF, FD_INDCVI_2013_IRISPROPRES, FD_INDCVI_2013_ZZZZZZZZZ_RECOD, FD_INDCVI_2013_IRISXXXX_RECOD)
#################################################
######## calcul des indicateurs de colocation
IRIS_RP2013_COLOCATION <-
IRIS_RP2013_POP %>% select(IRIS, P13_POP) %>%
filter(substr(IRIS,1,2) %in% c('75','77','78','91','92','93','94','95')) %>%
left_join(
FD_INDCVI_2013_IRISOK_IDF %>%
filter(AGEREVQ >= 20) %>%
group_by(IRIS_estime, LPRM) %>%
summarise(nb = sum(IPONDI)) %>%
mutate(LPRM = paste0("LPRM_", LPRM)) %>%
spread( LPRM, nb) %>%
mutate_each(funs(replace(.,is.na(.),0))) %>%
as.data.frame() %>%
mutate(P13_POP_sup20 = rowSums(.[2:11])), by = c("IRIS" = "IRIS_estime")) %>%
mutate_each(funs(replace(.,is.na(.),0))) # %>%
# mutate_each(funs(pct_POP_sup20 = ./P13_POP_sup20), starts_with("LPRM"))
# mutate(POPsup20ans_horsrefconjointHLO = LPRM_3 + LPRM_4 + LPRM_5 + LPRM_6 + LPRM_7 + LPRM_8 + LPRM_9,
# pct_LPRM3_POPsup20ans_horsrefconjointHLO = LPRM_3 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM4_POPsup20ans_horsrefconjointHLO = LPRM_4 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM5_POPsup20ans_horsrefconjointHLO = LPRM_5 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM6_POPsup20ans_horsrefconjointHLO = LPRM_6 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM7_POPsup20ans_horsrefconjointHLO = LPRM_7 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM8_POPsup20ans_horsrefconjointHLO = LPRM_8 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM9_POPsup20ans_horsrefconjointHLO = LPRM_9 / POPsup20ans_horsrefconjointHLO)
#idem par canton
CV_RP2013_COLOCATION <-
FD_INDCVI_2013 %>%
filter(AGEREVQ >= 20) %>%
group_by(CANTVILLE, LPRM) %>%
summarise(nb = sum(IPONDI)) %>%
mutate(LPRM = paste0("LPRM_", LPRM)) %>%
spread( LPRM, nb) %>%
mutate_each(funs(replace(.,is.na(.),0))) %>%
as.data.frame() %>%
mutate(P13_POP_sup20 = rowSums(.[2:11])) %>%
mutate_each(funs(pct_POP_sup20 =100* ./P13_POP_sup20), starts_with("LPRM")) #%>%
# mutate(POPsup20ans_horsrefconjointHLO = LPRM_3 + LPRM_4 + LPRM_5 + LPRM_6 + LPRM_7 + LPRM_8 + LPRM_9,
# pct_LPRM3_POPsup20ans_horsrefconjointHLO = LPRM_3 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM4_POPsup20ans_horsrefconjointHLO = LPRM_4 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM5_POPsup20ans_horsrefconjointHLO = LPRM_5 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM6_POPsup20ans_horsrefconjointHLO = LPRM_6 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM7_POPsup20ans_horsrefconjointHLO = LPRM_7 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM8_POPsup20ans_horsrefconjointHLO = LPRM_8 / POPsup20ans_horsrefconjointHLO,
# pct_LPRM9_POPsup20ans_horsrefconjointHLO = LPRM_9 / POPsup20ans_horsrefconjointHLO)
CV_comparo <- CV_spdf %>% as.data.frame() %>% left_join(CV_RP2013_COLOCATION, by = c("id" = "CANTVILLE")) %>% filter(is.na(LPRM_1))
CV_comparo_2 <- CV_RP2013_COLOCATION %>% left_join(CV_spdf %>% as.data.frame() %>% mutate(z = "z"), by = c("CANTVILLE" = "id")) %>% filter(is.na(z))
# chiffres france
FR_RP2013_COLOCATION <-
CV_RP2013_COLOCATION %>% ungroup() %>% select(-CANTVILLE) %>% summarise_each(funs(sum))
mutate_each(funs(pct_POP = ./P13_POP), starts_with("LPRM"))