-
Notifications
You must be signed in to change notification settings - Fork 0
/
unemploymentChanges_scarring.R
1593 lines (1174 loc) · 58.5 KB
/
unemploymentChanges_scarring.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Scarring and other stuff 2022
library(tidyverse)
library(sf)
library(tmap)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
#CHANGES IN UNEMPLOYMENT----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Get unemp figures and map
#https://statisticsglobe.com/merge-csv-files-in-r
ea <- list.files(path = "StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_raw/EconActive", pattern = "*.shp", full.names = T) %>%
lapply(st_read) %>%
bind_rows()
ea <- st_set_crs(ea, "EPSG:27700")
st_crs(ea)
#Add censusYear
#Order should be correct, in order of files
ea$censusYear <- rep(seq(from = 1971, to = 2011, by = 10), each = nrow(ea)/5)
#Seems to have worked...
chk <- table(ea$label, ea$censusYear) %>% data.frame
mean(chk$Freq)
#Double check: populations should have gone up in each decade
ea %>%
st_set_geometry(NULL) %>%
group_by(censusYear) %>%
summarise(totaleconpop = sum(econActive,na.rm = T))
# summarise(totaleconpop = sum(econActive,na.rm = T) + sum(unemployed,na.rm = T))
#Hmm, that's inconclusive! Better do it the slow way
# ea <- list.files(path = "StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_raw/EconActive", pattern = "*.shp", full.names = T) %>%
# lapply(st_read)
#
# years <- seq(from = 1971, to = 2011, by = 10)
# for(x in c(1:5)) ea[[x]]$censusYear <- years[x]
#
# #Ready for combining
# ea <- do.call("rbind",ea)
#Same result as before... might just load that odd one and check it's right... tick. Overall drop in econ active + unemp?
#Is that right? Seems wrong!
# chk01 <- st_read("StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_raw/EconActive/2001econActive.shp")
# sum(chk01$econActive+chk01$unemployed)
#Harrumph, don't have TTWAs. Need shapefile with that in
gb <- st_read("C:/Users/admin/Dropbox/SheffieldMethodsInstitute/CountryOfBirthOpenDataSets/data/gb_shapefile/gb_altered_wards_n_postcodesectors_w_lookup.shp")
gb <- gb %>%
st_set_geometry(NULL) %>%
select(zone,country,ttwa)
#Well you'd hope so... phew
table(ea$label %in% gb$zone)
ea <- ea %>%
left_join(gb, by = c("label" = "zone"))
rm(gb)
#Order TTWAs by most populous - might just look at a subselection of em
#Note: total econ pop != total pop. This doesn't include econ inactive. But will be good enough for this
#Note 2: summed for every Census decade. Still fine for us, just getting an ordered size list of TTWAs (approx)
##DUH: ECONACTIVE INCLUDES UNEMPLOYED
#ea$totaleconpop = ea$econActive + ea$unemployed
#Yeah, that works - Sheffield/Roth is 9th on list. So, important.
ttwa_order <- ea %>%
st_set_geometry(NULL) %>%
group_by(ttwa) %>%
summarise(totaleconpop_per<- = sum(econActive)) %>%
arrange(-totaleconpop_perttwa)
#Random thing: where's the employment % coming from?
#I think maybe unemployed is *part* of "economically active"... which did I use?
#If so, econActive - unemployed = total employed. Then as prop of econ active...
#Ah yes, that's exactly what I did. Phew.
# ea <- ea %>%
# mutate(chk = ((econActive - unemployed ) / econActive ) * 100)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#PLOT OF TTWA-LEVEL EMPLOYMENT FOR GB, WITH SHEF LOCATED----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##Sum per TTWA all the various values to see GB as a whole
#I want min and max TTWAs, with Sheffield shown overlaid
ttwa_totemployment <- ea %>%
st_set_geometry(NULL) %>%
group_by(ttwa,censusYear) %>%
summarise(econActive = sum(econActive, na.rm=T), unemployed = sum(unemployed, na.rm=T), employed = econActive - unemployed,
percentemployed = (employed/econActive)*100)
#Plot by a few that had the biggest and smallest decreases in employment 71-81, plot Sheffield against those.
#For which we need the change between Censuses per TTWA
ttwa_lags <- ttwa_totemployment %>%
group_by(ttwa) %>%
mutate(percentEmployedDiff = percentemployed - lag(percentemployed)) %>%
filter(!is.na(percentEmployedDiff))#drop first decade, no change
#Widenemate
ttwa_lags_wide <- ttwa_lags %>%
select(ttwa,censusYear,percentEmployedDiff) %>%
spread(key = censusYear, value = percentEmployedDiff) %>%
filter(!is.na(ttwa)) %>%
rename(`71to81` = `1981`, `81to91` = `1991`, `91to01` = `2001`, `01to11` = `2011`) %>%
arrange(`71to81`)
#Pick top 5, bottom 5 and Sheffield
#(Using ordered lags to get highest, lowest changes but we want ttwa_totemployment for plotting)
tops <- ttwa_totemployment %>%
filter(ttwa %in% c(ttwa_lags_wide$ttwa[1:5],ttwa_lags_wide$ttwa[(nrow(ttwa_lags_wide)-4):nrow(ttwa_lags_wide)],"Sheffield & Rotherham"))
#11 of em, tick
unique(tops$ttwa)
#Mark sheffield as unique for plotting, mark top and bottom too
tops <- tops %>%
mutate(Sheffield = ifelse(ttwa == "Sheffield & Rotherham","Sheffield",NA)) %>%
mutate(Sheffield = replace(Sheffield, ttwa %in% ttwa_lags_wide$ttwa[1:5], "5 largest drops")) %>%
mutate(Sheffield = replace(Sheffield, ttwa %in% ttwa_lags_wide$ttwa[(nrow(ttwa_lags_wide)-4):nrow(ttwa_lags_wide)], "5 smallest drops")) %>%
mutate(Sheffield = factor(Sheffield, levels = c("5 largest drops","5 smallest drops","Sheffield")))
#Though if you think about it, the largest drops would HAVE to start higher to have room to drop that far
#Still - it does look like most of those large drops bounced back 81-91, Sheffield did not
#Can use this to describe bounce back though.
#Going to have to add Sheffield separately as we can't control line and shape size independently. Amazing!
ggplot(tops, aes(x = factor(censusYear), y = percentemployed, colour = Sheffield, shape = Sheffield, size = Sheffield, alpha = Sheffield, group = ttwa)) +
geom_line() +
geom_point() +
scale_size_manual(values = c(1,1,3)) +
scale_alpha_manual(values = c(0.6,0.6,1)) +
geom_point(data = tops %>% filter(ttwa!="Sheffield & Rotherham"), aes(x = factor(censusYear), y = percentemployed), size = 3, alpha = 0.5) +
geom_point(data = tops %>% filter(ttwa=="Sheffield & Rotherham"), aes(x = factor(censusYear), y = percentemployed), size = 7) +
labs(colour="",shape="",size="",alpha="") +
xlab("Census year") +
ylab("Percent employed")
#Save tops for writeup
saveRDS(tops,"R_data/topbottom_ttwas_7181empdrop_inc_Sheffield.rds")
#Lots of interesting things there - note Sheffield didn't bounce back 81-91. The 5 largest drops did.
#Which begs the question - how many other places saw drops in the following decade? How unique is Sheffield?
#Other thing to note: how high employment was in 1971 in the places that had the largest drops
#Which "tops" saw a drop 71-81 *and* a drop 81-91?
#Plus: What proportion actually did drop 71-81, and 81-91?
ttwa_lags_wide <- ttwa_lags_wide %>%
mutate(
drop7181 = ifelse(`71to81` < 0,T,F),
drop8191 = ifelse(`81to91` < 0,T,F),
drop7181and8191 = ifelse(`71to81` < 0 & `81to91` < 0,T,F)
)
#check props
table(ttwa_lags_wide$drop7181) %>% prop.table * 100#Every single TTWA, blimey
table(ttwa_lags_wide$drop8191) %>% prop.table * 100#Half and half
#So the next question makes no sense - 100% in 71to81, so yeah, all who dropped 81-91 also dropped decade earlier
#Better question's going to be - what places saw that continued drop?
#Where were they? Did they start out with the most precipitous drop, as in the plot above?
#Mark all where 81-91 drop
ttwa_totemployment <- ttwa_totemployment %>%
mutate(drop8191 = ifelse( ttwa %in% ttwa_lags_wide$ttwa[ttwa_lags_wide$drop8191],T,F))
#Let's see if it's possible to see them all, just for the pattern
ggplot(ttwa_totemployment, aes(x = factor(censusYear), y = percentemployed, colour = drop8191, group = ttwa)) +
geom_line() +
geom_point()
#OK, better thing to do here: let's look at 71 employment
#(And maybe 71-81 employment drop)
#For those who also dropped 81-91
totemployment1971 <- ttwa_totemployment %>% filter(censusYear==1971)
#Actually not that different
ggplot(totemployment1971, aes(x = percentemployed, colour = drop8191)) +
geom_density()
#Map?
ttwamap <- st_read('C:/Data/MapPolygons/GreatBritain/2001/TTWAs/greatBritainTTWAs.shp')
ttwamap <- ttwamap %>%
left_join(
totemployment1971 %>% select(ttwa,drop8191),
by = c('NAME'='ttwa')
)
#That's a weird geography! I don't know what's going on there. Clearly something...
tm_shape(ttwamap) +
tm_polygons(col = 'drop8191', palette = 'plasma')
#Note: this is exactly what the bounceback regressions are trying to look at.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#MAP 1: ANIMATE SHEFFIELD EMPLOYMENT CHANGES OVER 5 CENSUSES----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Check I have the right map
shef <- ea %>% filter(ttwa == "Sheffield & Rotherham")
map <- tm_shape(shef) +
tm_polygons(col = 'percentEmp', style = 'jenks', n = 10, palette = 'plasma') +
tm_facets(by = 'censusYear', ncol = 1, nrow = 1)
tmap_animation(map, filename = 'R_outputs/Scarring/sheffield_unemploymentchange5census.gif', width = 800, height = 600, fps = 1)
#SAVE ea for rmarkdown
#PLOT 2: Top few TTWAs, unemp change over timE----
#Just want average unempl per TTWA
#Erm. Occurs to me, it should be population weighted by ward, right?
#Or - we've got the raw numbers here, just sum per TTWA then re-find proportion.
#Yes, easier...
topttwas <- ea %>% filter(ttwa %in% ttwa_order$ttwa[1:10]) %>%
st_set_geometry(NULL) %>%
group_by(ttwa,censusYear) %>%
summarise(tot_econactive = sum(econActive), tot_unemployed = sum(unemployed)) %>%
mutate(percentUnemployed = (tot_unemployed / tot_econactive)*100)
ggplot(topttwas, aes(x = censusYear, y = percentUnemployed, colour = fct_reorder(ttwa,-percentUnemployed))) +
geom_line() +
geom_point() +
scale_colour_brewer(palette = "Paired")
#What's interesting in that last plot - if we look at, say, 20:
#Between decades, direction is more or less the same for all TTWAS...
#EXCEPT 81-91, where we get several where unemployment goes UP (against what appears to be the general trend)
#Think I wanna see a map of that, maybe in QGIS actually
#That needs decadal change numbers - wanna see if directional. Err, so.
#Tempted to find numbers then reattach to geography to speed up...
allttwas <- ea %>%
st_set_geometry(NULL) %>%
group_by(ttwa,censusYear) %>%
summarise(tot_econactive = sum(econActive, na.rm=T), tot_unemployed = sum(unemployed, na.rm=T)) %>%
mutate(percentUnemployed = (tot_unemployed / tot_econactive)*100) %>%
ungroup()
#Have to make wide for viewing different decades in QGIS
#Oh, first, need to find differences
#Already ordered by census year, so can group by ttwa again
allttwas <- allttwas %>%
group_by(ttwa) %>%
mutate(percentUnemployedDiff = percentUnemployed - lag(percentUnemployed)) %>%
filter(!is.na(percentUnemployedDiff))#drop first decade, no change
#Widenemate
allttwas_diffswide <- allttwas %>%
select(ttwa,censusYear,percentUnemployedDiff) %>%
spread(key = censusYear, value = percentUnemployedDiff) %>%
filter(!is.na(ttwa))
#Reattach geog and save for QGIS
#Need TTWA geog, in fact... Might work fine in R for plotting?
ttwamap <- st_read('C:/Data/MapPolygons/GreatBritain/2001/TTWAs/greatBritainTTWAs.shp')
#Tick
table(ttwamap$NAME %in% allttwas$ttwa)
#Don't actually need wide if plotting using tmap... though is it gonna be tricky to see? Let's see...
#Actually, just tried plotting just the st_geometry of the ttwas, it's sloooow. Let's do QGIS.
allttwas_diffswide <- ttwamap %>%
left_join(allttwas_diffswide, by = c('NAME' = 'ttwa'))
st_write(allttwas_diffswide, "QGIS/5census_unemploymentChangeBetweenCensuses_TTWA.shp")
#Just check - how many differ in their polarity of direction for each decade?
allttwas <- allttwas %>%
mutate(polaritypositive = percentUnemployedDiff > 0)
allttwas %>%
group_by(censusYear) %>%
summarise(mean(polaritypositive, na.rm=T))
#So yeah: 71-81 - unempl went up everywhere
#91-01 - dropped everywhere
#81-91 more mixed, 01-11 mixed but less so (77.7% of TTWAs, unempl went up.)
# PLOT 3: Z-SCORES----
#How does Sheffield differ relatively to the rest of GB in each decade? How does that look compared to the other top few TTWAs?
#Flip so we've got % UNemployed
ea <- ea %>% mutate(percentUnemployed = 100 - percentEmp)
#Get normalised value based on mean (Z scores)
#Z score = [mean(x) -x] / sd
ea <- ea %>%
group_by(censusYear) %>%
mutate(percentUnemployed_Zscore = (percentUnemployed - mean(percentUnemployed, na.rm=T))/sd(percentUnemployed, na.rm=T) )
#Just to look...
ea <- ea %>%
group_by(censusYear) %>%
mutate(meanpercentunemployed=mean(percentUnemployed, na.rm=T))
#Skeeeeew
ggplot(ea, aes(x = percentUnemployed_Zscore, colour = factor(censusYear))) +
geom_density() +
coord_cartesian(xlim=c(-5,2)) +
scale_colour_brewer(palette = "Set1")
#Interesting here:
#Only 1971 differs, with generally lower unemployment overall
#For the rest, thepattern remains very similar
#The geography of that may have changed of course, but...
#Now to actually plot Sheffield's wards vs some other places
ggplot(ea %>% filter(ttwa %in% ttwa_order$ttwa[1:10]), aes(x = ttwa, y = percentUnemployed_Zscore)) +
geom_point(size = 2, alpha = 0.5) +
facet_wrap(~factor(censusYear)) +
geom_hline(yintercept = 0, colour = 'green') +
coord_flip()
#Violin plot?
ggplot(ea %>% filter(ttwa %in% ttwa_order$ttwa[1:10]), aes(x = fct_reorder(ttwa,percentUnemployed_Zscore), y = percentUnemployed_Zscore)) +
geom_point(size = 0.75, alpha = 0.3, colour = 'blue') +
geom_violin(fill = 'black') +
facet_wrap(~factor(censusYear)) +
geom_hline(yintercept = 0, colour = 'green') +
coord_flip(ylim=c(-2.5,7.5))
#Err, don't use z scores??
ea <- ea %>%
group_by(censusYear) %>%
mutate(meanpercentunemployed = mean(percentUnemployed, na.rm=T))
ggplot(ea %>% filter(ttwa %in% ttwa_order$ttwa[1:10]), aes(x = fct_reorder(ttwa,percentUnemployed), y = percentUnemployed)) +
geom_point(size = 0.75, alpha = 0.3, colour = 'blue') +
geom_violin(fill = 'black') +
facet_wrap(~factor(censusYear)) +
geom_hline(aes(yintercept = meanpercentunemployed), colour = 'green') +
coord_flip()
# coord_flip(ylim=c(-2.5,7.5))
#Animated version with fixed x axis
#https://ryanpeek.org/2016-10-19-animated-gif_maps_in_r/
ea <- ea %>% mutate(ttwa = fct_reorder(ttwa, percentUnemployed_Zscore))
#save that version for use in output
saveRDS(ea,"R_data/econactive_ttwa_5census.rds")
#save images...
saveggplots <- function(year){
#non z score version
# ggplot(ea %>% filter(censusYear== year, ttwa %in% ttwa_order$ttwa[1:10]), aes(x = ttwa, y = percentUnemployed)) +
# geom_point(size = 2, alpha = 0.3, colour = 'blue') +
# geom_violin(fill = 'black') +
# # geom_hline(yintercept = 0, colour = 'green') +
# geom_hline(aes(yintercept = meanpercentunemployed), colour = 'green') +
# coord_flip(ylim=c(0,40)) +
# labs(title = year) +
# theme(plot.title = element_text(size=50, hjust = 0.5))
ggplot(ea %>% filter(censusYear== year, ttwa %in% ttwa_order$ttwa[1:10]), aes(x = ttwa, y = percentUnemployed_Zscore)) +
geom_point(size = 2, alpha = 0.3, colour = 'blue') +
geom_violin(fill = 'black') +
geom_hline(yintercept = 0, colour = 'green') +
coord_flip(ylim=c(-2.5,7.5)) +
labs(title = year) +
theme(plot.title = element_text(size=50, hjust = 0.5))
print(paste0("saving plot ", year))
ggsave(filename = paste0("R_outputs/Scarring/animation_plots/",year,".png"),
width = 7,height=7,dpi = 150)
}
seq(from = 1971, to=2011, by=10) %>% map_df(saveggplots)
library(magick)
list.files(path = "R_outputs/Scarring/animation_plots", pattern = "*.png", full.names = T) %>%
map(image_read) %>% # reads each path file
image_join() %>% # joins image
image_animate(fps=0.5) %>% # animates, can opt for number of loops
image_write("R_outputs/Scarring/census_unemployment_zscore_violinplots.gif")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#CHECKING ON UNEMPLOYMENT NUMBERS 1: INACTIVE PROPORTIONS 1991----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Looking back at the OG 1971 table...
ea71 <- read_csv('1971/GreatBritain/gb_econActive1971.csv')
#Just looking at 1991 LBS economic position LBS08.
#It's got a good 'economically inactive' breakdown - I'd like to see the geography of that
#(Using the original LBS wards huh? If I can find those.)
#I want to see the age breakdown too, plot-wise.
#Err, going to be a very large amount of data - maybe stick to England for now
ea91 <- read_csv('1991/GreatBritain/LBS08_economicactivity_allvars.csv')
#Hmm, for the headline figures, I probably didn't want everyone in every geography did I? Could have compiled at larger areas.
#Let's just look at a map of the main econ inactive categories
#Shorter names to work with shp
ea91inactive <- ea91 %>%
select(zoneID = `Zone ID`, total = l080001, allInactive = l080172, studInactive = l080191,
sickInactive = l080210, retiInactive = l080229, othInactive = l080248)
#Get proportions
ea91inactive <- ea91inactive %>%
mutate(across(allInactive:othInactive, ~(./total)*100, .names = "pc_{.col}")) %>%
mutate(sick_pcInac = (sickInactive/allInactive)*100 )
#orig LBS wards?
# wards91 <- st_read('C:/Data/MapPolygons/GreatBritain/1991/GB_wards_pcs_agg4correctCount.shp')
#
# table(wards91$label %in% ea91$`Zone ID`)
# #Not the original wards... Hmmph, I seem not to have eng/wales/scotland joined anywhere pre-altered. Weird. Must have, surely?
# table(ea91$`Zone ID` %in% wards91$label)
#Let's just look at England to start with then
england91wards <- st_read('C:/Data/MapPolygons/England/1991/England_wa_1991/england_wa_1991.shp')
table(ea91$`Zone ID` %in% england91wards$label)
table(england91wards$label %in% ea91$`Zone ID`)
wales91wards <- st_read('C:/Data/MapPolygons/Wales/1991/Wales_wa_1991_gen3/wales_wa_1991_gen3.shp')
table(wales91wards$label %in% ea91$`Zone ID`)
scots91wards <- st_read('C:/Data/MapPolygons/Scotland/1991/Scotland_census_pseudoPostcodeSectors_1991/soctland_pseudo_pcs_IDtoSingleRow.shp')
table(scots91wards$label %in% ea91$`Zone ID`)
#OK, join all those
gb91wards <- bind_rows(england91wards,wales91wards,scots91wards)
table(gb91wards$label %in% ea91$`Zone ID`)
#Not perfect. Why not? Leave for now
table(ea91inactive$zoneID %in% gb91wards$label)
gb91wards <- gb91wards %>%
left_join(ea91inactive, by = c('label' = 'zoneID'))
st_write(gb91wards,'qgis/econinactive_percents_gb91wards.shp')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#CHECKING ON UNEMPLOYMENT NUMBERS 1: INACTIVE PROPORTIONS 1971----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Starting with EDs, might collate to 91 olnerwards at some point
ea71 <- read_csv('C:/Users/admin/Dropbox/SheffieldMethodsInstitute/Census_dx/1971/GreatBritain/gb_econActive1971.csv')
#Looking at table in CASWEB...
#We have 'total / working / seeking work / sick' and one male / two female cats to sum
#Male/female combined
#Is unclear what that slight diff is. Let's try sick as prop of total
ea71 <- ea71 %>%
mutate(
total = c71s05_257+c71s05_258+c71s05_259,
working = c71s05_260+c71s05_263+c71s05_266,
seekingwork = c71s05_261+c71s05_264+c71s05_267,
sick = c71s05_262+c71s05_265+c71s05_268,
check = working + seekingwork + sick,
pc_sick_oftotal = (sick/total)*100
)
ea71eds <- st_read('C:/Data/MapPolygons/GreatBritain/1971/gb71eds_dissolvedByID.shp')
#tick
table(ea71eds$zone_code %in% ea71$`Zone Code`)
ea71eds <- ea71eds %>%
left_join(
ea71 %>% select(`Zone Code`, pc_sick_oftotal),
by = c('zone_code' = 'Zone Code')
)
st_write(ea71eds,'QGIS/ea71eds_percentsickoftotal.shp')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#RECREATING VENABLES/RICE PAPER----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Rice, P.G., Venables, A.J., 2021. The persistent consequences of adverse shocks: how the 1970s shaped UK regional inequality. Oxford Review of Economic Policy 37, 132–151. https://doi.org/10.1093/oxrep/graa057
#Basics are not very complex.
#Figure 2a and b:
#(a) Change in "ppt diff from average employment" 71-81 vs that change 71-11. (Showing where it's persisted.)
#(b) Same but 71-81 compared to 81-11. They're looking for bounce- back, so e.g. a -10% point drop would then be +10%. They're calling that "convergence" – discussed in the article above as:
#"The classic (or neoclassical) forces for convergence are simply that wage adjustment will cause some combination of replacement jobs moving into adversely affected places, and population moving out." 133/4
#So let's look at the same here... maybe for all the data at once to start with, before breaking down by...
#Oh, this is so going to be a multilevel thing! Or should be.
#Or maybe they actually already did it.
#Have to find ppt diff from averages first, don't I? Then diff between decades for the set decades.
ea <- ea %>%
group_by(censusYear) %>%
mutate(pc_unempl_pptdiff_fromav = percentUnemployed - mean(percentUnemployed, na.rm=T))#already is ppt, so this should be fine?
#Did this above, but putting here to figure out how I've got opposite polarities... Oh, did I not re-run z-score after I got it backwards??
#Yup, that was it!
# ea <- ea %>%
# group_by(censusYear) %>%
# mutate(percentUnemployed_Zscore = (percentUnemployed - mean(percentUnemployed, na.rm=T))/sd(percentUnemployed, na.rm=T) )
#Then I need the various different decades next to each other for a decadal change...
ea_pptdiff_wide <- ea %>%
st_set_geometry(NULL) %>%
select(label,ttwa,censusYear,pc_unempl_pptdiff_fromav) %>%
spread(key = censusYear, value = pc_unempl_pptdiff_fromav)
#Need two diffs for plotting
ea_pptdiff_wide <- ea_pptdiff_wide %>%
mutate(
diff71_81 = `1981`-`1971`,
diff71_11 = `2011`-`1971`,
diff81_11 = `2011`-`1981`
)
#plot diffs, as per Venables paper
ggplot(ea_pptdiff_wide, aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm')+
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
ggplot(ea_pptdiff_wide, aes(x = diff71_81, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm')+
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
#Look at some TTWAs
plotppt_ttwa1 <- function(x){
ggplot(ea_pptdiff_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
plotppt_ttwa2 <- function(x){
ggplot(ea_pptdiff_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
plotppt_ttwa1('Sheffield & Rotherham')
plotppt_ttwa2('Sheffield & Rotherham')
plotppt_ttwa1('London')
plotppt_ttwa2('London')
ea_pptdiff_wide_geo <- left_join(
ea %>% filter(censusYear==1971) %>% select(label),#just need labels for one arbitrary year
ea_pptdiff_wide %>% select(label,diff71_81:diff81_11),
by='label'
)
#Really annoying delete behaviour - no option to "delete if already exists" (or maybe there is, I can't find it)
st_write(ea_pptdiff_wide_geo, "qgis/ea_pptdiff_wide_geo.shp", delete_dsn = T)
#Rank position change might work better? Let's see
#1 is lowest unemployment (do we want that reversed?)
ea <- ea %>%
group_by(censusYear) %>%
mutate(unempl_rank = rank(percentUnemployed))
ea_rank_wide <- ea %>%
st_set_geometry(NULL) %>%
select(label,ttwa,censusYear,unempl_rank) %>%
spread(key = censusYear, value = unempl_rank)
#Need two diffs for plotting
ea_rank_wide <- ea_rank_wide %>%
mutate(
diff71_81 = `1981`-`1971`,
diff71_11 = `2011`-`1971`,
diff81_11 = `2011`-`1981`,
diff81_91 = `1991`-`1981`,
diff91_11 = `2011`-`1991`
)
#plot diffs, as per Venables paper, using RANK this time
ggplot(ea_rank_wide, aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm')
ggplot(ea_rank_wide, aes(x = diff71_81, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm')
#Is this actually just something intrinsic to that comparison?
#How does 81-91 vs 81-11 look?
#Actually, more evidence of bounce back here... But yes, similar structure
ggplot(ea_rank_wide, aes(x = diff81_91, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm')
ggplot(ea_rank_wide, aes(x = diff81_91, y = diff91_11)) +
geom_point() +
geom_smooth(method='lm')
#Now just for Sheffield TTWA 71-81
plot_ttwa1 <- function(x){
ggplot(ea_rank_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
plot_ttwa2 <- function(x){
ggplot(ea_rank_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
plot_ttwa1('Sheffield & Rotherham')
plot_ttwa2('Sheffield & Rotherham')
plot_ttwa1('London')
plot_ttwa2('London')
#OK, gonna have to see where the fook these rank changes have happened
#Sheffield mostly going up??
ea_rank_wide_geo <- left_join(
ea %>% filter(censusYear==1971) %>% select(label),#just need labels for one arbitrary year
ea_rank_wide %>% select(label,diff71_81:diff91_11),
by='label'
)
st_write(ea_rank_wide_geo, "qgis/ea_rank_wide_geo.shp", delete_dsn = T)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#COMPARE MEASURES: PPT, Z SCORE ETC----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#While ppt and z-score will just be linear scale of each other, comparing won't be because ppt distances from the mean will differ / won't be standardised. If we're just after relative change in position of wards / LAs, whatever, not sure ppt works. Z-score has its own problems, but I think maybe ranking doing away with any sense of scale might not be great either.
#Z-score by Census decade... oh, we already found it above.
#Except, that was by TTWA wasn't it?
#Nope: per Census. Makes sense. Same here, we want to be comparing across GB.
#So let's just compare rank / ppt dist from mean / z score for a couple of places
#Err... they'll be exactly linear, right? It's only in the time comparison that issues will emerge
ggplot(ea %>% filter(ttwa=='Sheffield & Rotherham'), aes(x = pc_unempl_pptdiff_fromav, percentUnemployed_Zscore)) +
geom_point() +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
#Oops. accidentally plotted every Census - but that does make the point:
#Z-score scale stays very close to identical across Censuses
#While ppt dist from mean varies between Censuses
#Z-score vs rank? Is effectively going to show a cumul dist function innit? Yup.
#But it does also show they don't vary across Censuses very much, same as Z-score.
#Which is what I think we want.
ggplot(ea %>% filter(ttwa=='Sheffield & Rotherham'), aes(x = unempl_rank, percentUnemployed_Zscore)) +
geom_point()
#Z-score diffs:
ea_z_wide <- ea %>%
st_set_geometry(NULL) %>%
select(label,ttwa,censusYear,percentUnemployed_Zscore) %>%
spread(key = censusYear, value = percentUnemployed_Zscore)
#Need two diffs for plotting
ea_z_wide <- ea_z_wide %>%
mutate(
diff71_81 = `1981`-`1971`,
diff71_11 = `2011`-`1971`,
diff81_11 = `2011`-`1981`,
diff81_91 = `1991`-`1981`,
diff91_11 = `2011`-`1991`
)
#Now I'm guessing we'll see some quite large differences between diffs
#for ppt vs z-score
#(Which will require combining two dfs / renaming...)
ea_z_and_ppt_wide <- ea_z_wide %>%
select(label,ttwa,
zdiff71_81 = diff71_81,
zdiff71_11 = diff71_11,
zdiff81_11 = diff81_11,
zdiff81_91 = diff81_91,
zdiff91_11 = diff91_11
) %>%
left_join(ea_pptdiff_wide %>%
select(label,
pptdiff71_81 = diff71_81,
pptdiff71_11 = diff71_11,
pptdiff81_11 = diff81_11
),
by = 'label'
)
#compare:
#OK, so yeah: many many are not the same polarity
#i.e. often, z-score is showing a drop between Censuses where ppt shows a rise
ggplot(ea_z_and_ppt_wide %>% filter(ttwa=='London'), aes(x = zdiff71_81, y = pptdiff71_81)) +
geom_point() +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
ggplot(ea_z_and_ppt_wide %>% filter(ttwa=='Sheffield & Rotherham'), aes(x = zdiff71_81, y = pptdiff71_81)) +
geom_point() +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
#All wards
ggplot(ea_z_wide, aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
#cf. ppt
ggplot(ea_pptdiff_wide, aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
#So let's check z-score diff plots
#Now just for Sheffield TTWA 71-81
zplot_ttwa1 <- function(x){
ggplot(ea_z_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff71_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
zplot_ttwa2 <- function(x){
ggplot(ea_z_wide %>% filter(ttwa==x), aes(x = diff71_81, y = diff81_11)) +
geom_point() +
geom_smooth(method='lm') +
geom_vline(xintercept = 0, colour = 'blue') +
geom_hline(yintercept = 0, colour = 'blue')
}
zplot_ttwa1('Sheffield & Rotherham')
zplot_ttwa2('Sheffield & Rotherham')
zplot_ttwa1('London')
plotppt_ttwa1('London')
zplot_ttwa2('London')
#Let's check out the geography of the z-plots...
ea_z_wide_geo <- left_join(
ea %>% filter(censusYear==1971) %>% select(label),#just need labels for one arbitrary year
ea_z_wide %>% select(label,diff71_81:diff91_11),
by='label'
)
#st_write(ea_z_wide_geo, "qgis/ea_z_wide_geo.shp")
st_write(ea_z_wide_geo, "qgis/ea_z_wide_geo.shp", delete_dsn = T)
#~~~~~~~~~~~~~~~~~~~~~~~~
#STARING AT SHEFFIELD----
#~~~~~~~~~~~~~~~~~~~~~~~~
#Actually looking at numbers
ttwa <- ea %>% filter(ttwa=='Sheffield & Rotherham')
ttwa <- ea %>% filter(ttwa=='London')
#overlay Sheffield on top of all
ttwa2 <- ea
ttwa_wide_percentunemp_shef <- ttwa %>%
st_set_geometry(NULL) %>%
select(label,censusYear,percentUnemployed) %>%
spread(key = censusYear, value = percentUnemployed) %>%
mutate(type = "Sheffield")
ttwa_wide_percentunemp_all <- ttwa2 %>%
st_set_geometry(NULL) %>%
select(label,censusYear,percentUnemployed) %>%
spread(key = censusYear, value = percentUnemployed) %>%
mutate(type = "all wards")
#join both
ttwa_wide_percentunemp <- rbind(ttwa_wide_percentunemp_shef,ttwa_wide_percentunemp_all)
# ttwa_wide_percentunemp$type <- factor(ttwa_wide_percentunemp$type, levels = c("all wards"))
#Too crude to see
#pairs(ttwa_wide_percentunemp %>% select(-label))
ggplot(ttwa_wide_percentunemp, aes(x = `1971`, y = `1981`, colour = type, shape = type, size = type)) +
geom_point() +
geom_abline(slope = 1, intercept = 0) +
scale_color_manual(values=c("black", "red"))
ggplot(ttwa_wide_percentunemp, aes(x = `1971`, y = `2011`)) +
geom_point() +
geom_abline(slope = 1, intercept = 0)
#So totally did bounce back. Of course.
ggplot(ttwa_wide_percentunemp, aes(x = `1981`, y = `2011`)) +
geom_point() +
geom_abline(slope = 1, intercept = 0)
#Overlaying sheffield on all (doing this way to control order)
#Like these!
ggplot() +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="all wards"), aes(x = `1971`, y = `1981`), alpha = 0.5) +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="Sheffield"), aes(x = `1971`, y = `1981`), colour="red", size = 2, shape = 17) +
geom_abline(slope = 1, intercept = 0, colour='blue')
ggplot() +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="all wards"), aes(x = `1981`, y = `1991`), alpha = 0.5) +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="Sheffield"), aes(x = `1981`, y = `1991`), colour="red", size = 2, shape = 17) +
geom_abline(slope = 1, intercept = 0, colour='blue')
ggplot() +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="all wards"), aes(x = `1971`, y = `2011`), alpha = 0.5) +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="Sheffield"), aes(x = `1971`, y = `2011`), colour="red", size = 2, shape = 17) +
geom_abline(slope = 1, intercept = 0)
ggplot() +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="all wards"), aes(x = `1981`, y = `2011`), alpha = 0.5) +
geom_point(data = ttwa_wide_percentunemp %>% filter(type=="Sheffield"), aes(x = `1981`, y = `2011`), colour="red", size = 2, shape = 17) +
geom_abline(slope = 1, intercept = 0)
#But the paper's point is about equilibriating across the country, hence the whole-country comparison.
#And abstracting from decadal details.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#VARIOUS COMPARISONS OF PPT Z AND RANK, DISTRIBUTIONS----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#diff from av: different spread across Censuses
ggplot(ea, aes(x = pc_unempl_pptdiff_fromav , y = factor(censusYear))) +
geom_point(size = 0.75, alpha = 0.3, colour = 'blue') +
geom_violin(fill = 'black') +
geom_vline(xintercept = 0, colour = 'green')
#Z score will have about the same range but not nec same shape?
ggplot(ea, aes(x = percentUnemployed_Zscore, y = factor(censusYear))) +
geom_point(size = 0.75, alpha = 0.3, colour = 'blue') +
geom_violin(fill = 'black') +
geom_vline(xintercept = 0, colour = 'green')
#And rank is just rank
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#SEEING IF CAN USE AGE DATA FOR 16-64 DENOM FOR EMPLOYMENT COUNT----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Which will involve remembering where the age data was stored.
# Location of those newly harmonised vars:
# C:\Users\admin\Dropbox\SheffieldMethodsInstitute\Census_dx\StitchOutputs\GreatBritain\LBS_postcodeSectorWard_5Census_csvs
# Created in this script:
# newVarHarmonising2020.R
#Let's get one decade of age and econ active and see...
age71 <- read_csv("StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_csvs/1971age.csv")
#Is that the range we got from the others? Yup. Starts at 15 not 16. Maybe that's OK.
#age91 <- read_csv("StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_csvs/1991age.csv")
ea71 <- read_csv("StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_csvs/1971econActive.csv")
#Hmm. Orig 1971 data doesn't actually contain "unemployed". It's got working, seeking work, sick.
#I think I probably made econactive working + seeking work, didn't I?
#Yes: englandWalesDataWrangling.R line 1867
#And yes - the number working is then just the inverse, econactive - unemployed ("seeking work")
#I think the same applies to all Census decades
#Which means I should be able to use total 15-64 age pop as denom
#Note: people over the age of 64 can be working, so it's less than ideal in my opinion, but hey
#1. Convert all EA to employed number, which is just inverse of unemployed / econ active for all (or should be!)
#Using previously combined version above...
ea <- ea %>%
mutate(employed = econActive - unemployed)
#Now to get employment rate compared to all 16-64 year olds (but has to be 15 to 64 year olds here)
#Will need age, with censusYear added, to join
age <- list.files(path = "StitchOutputs/GreatBritain/LBS_postcodeSectorWard_5Census_csvs", pattern = "*age.csv", full.names = T) %>%
lapply(read_csv) %>%
bind_rows()
#Add censusYear
#Order should be correct, in order of files
age$censusYear <- rep(seq(from = 1971, to = 2011, by = 10), each = nrow(age)/5)
#Check worked - should be more 75+ every decade
#Yup - wow, that's a LOT more! Number of over 75s doubled?
age %>%
select(censusYear,X75.) %>%
group_by(censusYear) %>%
summarise(mean(X75., na.rm=T))
#Sum ages 15-64
age <- age %>%
mutate(`15 to 64` = X15.19+X20.24+X25.29+X30.34+X35.39+X40.44+X45.49+X50.54+X55.59+X60.64)
#Merge in key columns to EA
ea <- ea %>%
left_join(
age %>% select(label,censusYear,`15 to 64`),
by = c("label","censusYear")
)
#Erm... oh actually, this does make sense. Actual number of 16-64 is going to be larger than econ active isn't it?
#(Though econ active can include people still working past 64 I believe...)
ggplot(
ea %>% st_set_geometry(NULL) %>% filter(censusYear == 1981) %>% ungroup() %>% select(econActive,`15 to 64`) %>% gather(key = varname, value = count),
aes(x = count, colour = varname)
) +
geom_boxplot()
#This might be more of a problem... Ah no, they mostly behave
ggplot(
ea %>% st_set_geometry(NULL) %>% filter(censusYear == 1981) %>% ungroup() %>% select(employed,`15 to 64`) %>% gather(key = varname, value = count),
aes(x = count, colour = varname)
) +
geom_boxplot()