-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_me_Industry_tool.R
2567 lines (2142 loc) · 108 KB
/
source_me_Industry_tool.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
# 1.1.1 PRE-REQS----------------
#' ensure that the current versions of the following files are available in subdirectory "LMO Master Databases"
#' "JO single variables.csv"
#' "Industry characteristics.xlsx"
#' "Emp single variables.csv"
#' "DS single variables.csv"
#' "Occupation characteristics.xlsx"
#' "HOO list.xlsx"
#'
#' and the current version of these files are available in subdirectory "Tableau Tool Inputs"
#' "Jobs_and_Industry.csv"
#' "NOC Mappings.csv"
#' "IndustryProfiles_Descriptions.xlsx"
#' "2020Preliminary wages.xlsx": LIKELY THIS FILE NAME WILL CHANGE AND THEREFORE NEED TO CHANGE LINE 5 OF FILE "read_common_data.R"
# 1.1.2 fair bit of code duplication between the Rmd versions of LMO tool and industry tool. Load the common code.
source("previously_duplicated_code.R")
# 1.1.3 read specific data-------------------
jobs_NOC <- read.csv(here("Tableau Tool Inputs", "Jobs_and_Industry.csv"))
wages <- read.xlsx(here("Tableau Tool Inputs", "2020Preliminary wages.xlsx"))
wages_cleaned <- wages[, c("NOC", "Economic.Region", "Low.Wage.($).2020.(1st.decile)", "Median.Wage.($).2020", "High.Wage.($).2020.(9th.decile)")]
colnames(wages_cleaned) <- c("NOC", "Region", "Low Wage", "Median Wage", "High Wage")
# 1.2_Prep_Job_Openings---------
# The JO_Employment data frame has the columns: Date, NOC, Description, Industry, Geographic Area, Deaths, Expansion Demand, Job Openings, Replacement Demand, Retirement, Employment, Aggregate Industry
job_openings <- JO_Employment
job_openings$Industry <-
as.factor(trimws(tolower(job_openings$Industry))) # factor, trim white space, lower case for industry column
job_openings$`Aggregate Industry` <- tolower(job_openings$`Aggregate Industry`)
ind_char <- ind_char_raw
ind_char$Industry <-
as.factor(trimws(tolower(ind_char$Industry))) # trim white space, factor, to lower case for industry column
# Rename columns
colnames(ind_char) <-
c(
"Industry Code",
"Industry",
"NAICS Definition",
"Agg Industry Code",
"Aggregate Industry",
"Sector Code",
"Sector",
"Ind Group: Trades Intensive Industries",
"Ind group: Tech Intensive Industries",
"ITA Sector Advisory Group"
)
# Create a copy of the industry characteristics file with only the first seven columns, Industry Code, Industry, NAICS definitions, agg industry code, aggregate industry, sector code, sector
ind_char2 <- filter(ind_char, Sector != "Total")
for (i in 1:nrow(ind_char2)) {
if (ind_char2$`Ind group: Tech Intensive Industries`[i] == "Tech intensive") {
ind_char2$Sector[i] <- "Technology"
} else {
next
}
}
ind_char2 <- subset(ind_char2, select = -c(`Ind Group: Trades Intensive Industries`, `Ind group: Tech Intensive Industries`, `ITA Sector Advisory Group`))
# Merge these two data frames
# Columns are industry code, industry, date, noc, description, geographic area, jobs openings, expansion demand, replacement demand, deaths, retirements, employment, aggregate industry.x (code for aggregate industry from df 1),NAICS defintion, Aggregate Industry.y (code for aggregate industry from df2), Sector code (aggregate industry), Sector
Jobs_and_industry <-
merge(job_openings,
ind_char2,
by = c("Industry Code", "Industry"),
all = TRUE
)
Jobs_and_industry <-
subset(Jobs_and_industry, select = -`Aggregate Industry.x`) # don't need this column (letter code for aggregate industry)
Jobs_and_industry <-
subset(Jobs_and_industry, select = -`Agg Industry Code`) # don't need this column (letter code for aggregate industry)
colnames(Jobs_and_industry)[which(colnames(Jobs_and_industry) == "Aggregate Industry.y")] <-
"Aggregate Industry" # rename aggregate industry column
Jobs_and_industry$`Industry Code` <-
as.factor(Jobs_and_industry$`Industry Code`) # factor industry code
Jobs_and_industry$Industry <-
as.factor(Jobs_and_industry$Industry) # factor industry
Jobs_and_industry$Date <-
as.factor(Jobs_and_industry$Date) # factor date
Jobs_and_industry$Description <-
as.factor(Jobs_and_industry$Description) # factor description
Jobs_and_industry$`NAICS Definition` <-
as.factor(Jobs_and_industry$`NAICS Definition`) # factor NAICS definitions
Jobs_and_industry$`Aggregate Industry` <-
Jobs_and_industry$`Aggregate Industry` # factor aggregate industry
Jobs_and_industry$`Sector Code` <-
as.factor(Jobs_and_industry$`Sector Code`) # factor sector code
Jobs_and_industry$Sector <-
as.factor(Jobs_and_industry$Sector) # factor sector
Jobs_and_industry$`Geographic Area` <-
as.factor(Jobs_and_industry$`Geographic Area`) # factor geographic area
Jobs_and_industry <-
filter(Jobs_and_industry, Industry != "all industries") # remove all industries
# factor hierarchical structure column
# Broad occupational category, major group, minor group, unit group
noc_mappings <- noc_mappings_raw
noc_mappings$`Hierarchical structure` <-
as.factor(noc_mappings$`Hierarchical structure`)
# Do broad occupational categories first
noc_broad_occ <-
filter(
noc_mappings,
noc_mappings$`Hierarchical structure` == "Broad occupational category"
) # filter to only include broad occupational category
noc_broad_occ <-
noc_broad_occ[, c("Code", "Class title")] # we only want code and class title
noc_broad_occ$Code <-
paste0("#", noc_broad_occ$Code) # put a pound symbol in front of the one digit code
colnames(noc_broad_occ) <-
c("NOC1", "NOC1 Description") # rename these NOCs to be NOC1 and NOC1 Description
# Do major groups next
noc_major_group <-
filter(
noc_mappings,
noc_mappings$`Hierarchical structure` == "Major group"
)
# Need to fix up 01-05 and 07-09, so there are individual lines for each code
fix <- filter(noc_major_group, Code %in% c("01-05", "07-09"))
fix_bind <-
as.data.frame(rbind(fix[1, ], fix[1, ], fix[1, ], fix[1, ], fix[1, ], fix[2, ], fix[2, ], fix[2, ]))
fix_bind$Code <- c("01", "02", "03", "04", "05", "07", "08", "09")
# remove from original data frame and add in fixed codes
noc_major_group <-
filter(noc_major_group, Code != "01-05") %>% filter(Code != "07-09")
noc_major_group <- rbind(fix_bind, noc_major_group)
noc_major_group <- noc_major_group[, c("Code", "Class title")]
for (i in 1:nrow(noc_major_group)) {
if (nchar(noc_major_group$Code[i]) < 2) {
noc_major_group$Code[i] <- paste0("0", noc_major_group$Code[i])
}
}
noc_major_group$Code <-
paste0("#", noc_major_group$Code) # put a pound symbol in front of the two digit codes
colnames(noc_major_group) <-
c("NOC2", "NOC2 Description") # rename these NOCS to be NOC2 and NOC2 Description
# Do minor groups next
noc_minor_group <-
filter(
noc_mappings,
noc_mappings$`Hierarchical structure` == "Minor group"
) # filter to only include minor groups
noc_minor_group <-
noc_minor_group[, c("Code", "Class title")] # only need code and class title
# If there are only two digits, add a zero out front, if there is only one digit add two zeros out front
for (i in 1:nrow(noc_minor_group)) {
if (nchar(noc_minor_group$Code[i]) == 2) {
noc_minor_group$Code[i] <- paste0("0", noc_minor_group$Code[i])
} else {
if (nchar(noc_minor_group$Code[i]) < 2) {
noc_minor_group$Code[i] <- paste0("00", noc_minor_group$Code[i])
} else {
noc_minor_group$Code[i] <- noc_minor_group$Code[i]
}
}
}
noc_minor_group$Code <-
paste0("#", noc_minor_group$Code) # put a pound symbol in front of the three digit codes
colnames(noc_minor_group) <-
c("NOC3", "NOC3 Description") # rename these NOCS to be NOC3 and NOC3 Description
# Finally do the unit group
noc_unit <-
filter(
noc_mappings,
noc_mappings$`Hierarchical structure` == "Unit group"
)
noc_unit <- noc_unit[, c("Code", "Class title")]
# If there are only three digits, put one zero in front, if only two digits, put two zeros out front
for (i in 1:nrow(noc_unit)) {
if (nchar(noc_unit$Code[i]) == 3) {
noc_unit$Code[i] <- paste0("0", noc_unit$Code[i])
} else {
if (nchar(noc_unit$Code[i]) == 2) {
noc_unit$Code[i] <- paste0("00", noc_unit$Code[i])
} else {
noc_unit$Code[i] <- noc_unit$Code[i]
}
}
}
noc_unit$Code <-
paste0("#", noc_unit$Code) # put a pound symbol in front of the four digit codes
colnames(noc_unit) <-
c("NOC", "NOC4 Description") # Rename these nocs to be NOC (to match our current data frame) and NOC4 description
jobs_employment <-
Jobs_and_industry # make a copy of Jobs_and_industry data frame, and name it jobs_employment
jobs_employment <-
filter(jobs_employment, Industry != "all industries") # remove the "all industries" rows
jobs_employment$`Industry Code` <-
as.factor(jobs_employment$`Industry Code`) # factor industry code column
jobs_employment$Industry <-
as.factor(tolower(jobs_employment$Industry)) # factor and lower case for the industry column
jobs_employment$NOC <-
as.factor(jobs_employment$NOC) # factor the NOC column
jobs_employment$`Geographic Area` <-
as.factor(jobs_employment$`Geographic Area`) # factor the geographic area column
jobs_employment$`Aggregate Industry` <-
as.factor(jobs_employment$`Aggregate Industry`)
education_occupation <- education_occupation_raw
education_occupation <-
education_occupation[, c(1, 6, 7, 8, 10)] # We only want the columns with the various NOCS and Typical Education Background
education_occupation$NOC <-
as.factor(education_occupation$NOC) # Factor 4 digit NOC
education_occupation$NOC1 <-
as.factor(education_occupation$NOC1) # Factor 1 digit NOC
education_occupation$NOC2 <-
as.factor(education_occupation$NOC2) # Factor 2 digit NOC
education_occupation$NOC3 <-
as.factor(education_occupation$NOC3) # Factor 3 digit NOC
jobs_employment <-
merge(jobs_employment, education_occupation, by = c("NOC")) # Merge jobs_employment data frame with education data
# we need to have typical education background, NOC (hierarchy), occupation title, employment, expansion year1-year3, replacement year1-year3, job openings year1-year3 (wages too?) (**)
jobs_employment$Industry <-
tolower(jobs_employment$Industry) # ensure that industry is lower case (easier to match up)
jobs_employment$Industry <-
as.factor(jobs_employment$Industry) # ensure that industry is a factor
# 1.3_Aggregated_Industries-----------------
# we need to calculate growth rates for both the sectors, individual industries and the aggregate industries because these cannot be aggregated directly in Tableau
# we will start by looping over the aggregated industries first
# Create two empty data frames to act as storage as we loop through
by_agg_industry <- NULL # placeholder for first loop
by_aggregated_industry <- NULL # placeholder for second loop
# Handle by aggregate industry first, for each geography and at each aggregate industry level we are going to calculate employment year1, Employment Growth (CAGR) year1-year2, Employment Growth (CAGR) year2-year3, Employment Growth (CAGR) year1-year3, Expansion year1-year3, Replacement year1-year3, Total Job Openings year1-year3
# The final data frame will have five columns: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
# Need number of job openings for BC (year1) and (year1-year3)
JO_BC_year1 <- filter(jobs_employment, `Geographic Area` == "British Columbia") %>% filter(Date == year1)
JO_BC_year1_year3 <- filter(jobs_employment, `Geographic Area` == "British Columbia") %>% filter(Date %in% c(as.numeric(year1 + 1):year3))
for (i in 1:nlevels(jobs_employment$`Geographic Area`)) {
# loop through each geographic area
temp <-
filter(
jobs_employment,
`Geographic Area` == levels(jobs_employment$`Geographic Area`)[i]
) # filter a temporary data frame from "job_employment" that only includes the geographic area of interest
temp$`Aggregate Industry` <-
as.factor(temp$`Aggregate Industry`) # ensure aggregate industries area a factor
# We need a column with labels for Employment year1, Employment Growth (CAGR) year1-year2, Employment Growth (CAGR) year2-year3, Employment Growth (CAGR) year1-year3, Expansion year1-year3, Replacement year1-year3, Total Job Openings year1-year3 for the AGGREGATED INDUSTRIES
for (j in 1:nlevels(temp$`Aggregate Industry`)) {
# loop through each aggregate industry
# We are going to get the employment year1 data for the geography and aggregate industry first
temp_employment_year1 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date == year1) # filter to only include geography/aggregate industry and year to year1
employment_year1 <-
sum(as.numeric(temp_employment_year1$Employment)) # we need to sum this value to get employment for the aggregate industry
agg_industry_employment <- # place this into a data frame: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment year1",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp_employment_year1$`Aggregate Industry`)[j],
"Value" = employment_year1
)
# Calculate the employment growth from year1 to year2: This is calculated as the (employment in year2/employment in year1 ^(1/n) - 1) (the CAGR, where n is the number of years)
temp_employmentgrowth_year1_year2 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date %in% c(year1, year2))
# Calculate the CAGR for year1 to year2
CAGR1 <-
100 * (((
sum(
filter(temp_employmentgrowth_year1_year2, Date %in% year2)$Employment
) / sum(
filter(temp_employmentgrowth_year1_year2, Date %in% year1)$Employment
)
)^(1 / 5)) - 1)
# Calculate the CAGR for year2 to year3
temp_employmentgrowth_year2_year3 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date %in% c(year2, year3))
CAGR2 <-
100 * (((
sum(
filter(temp_employmentgrowth_year2_year3, Date %in% year3)$Employment
) / sum(
filter(temp_employmentgrowth_year2_year3, Date %in% year2)$Employment
)
)^(1 / 5)) - 1)
# Calculate the CAGR for year1 to year3
temp_employmentgrowth_year1_year3 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date %in% c(year1, year3))
CAGR3 <-
100 * (((
sum(
filter(temp_employmentgrowth_year1_year3, Date %in% year3)$Employment
) / sum(
filter(temp_employmentgrowth_year1_year3, Date %in% year1)$Employment
)
)^(1 / 10)) - 1)
# We assign each of these CAGRS to a data frame with the same format as before: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
agg_industry_employment2 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year1-year2",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = CAGR1
)
agg_industry_employment3 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year2-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = CAGR2
)
agg_industry_employment4 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year1-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = CAGR3
)
# Calculate the expansion demand from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
expansion <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year3
)
)
expansion <- sum(expansion$`Expansion Demand`)
expansion_year1_year2 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year2
)
)
expansion_year1_year2 <- sum(expansion_year1_year2$`Expansion Demand`)
expansion_year2_year3 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year2 + 1):year3
)
)
expansion_year2_year3 <- sum(expansion_year2_year3$`Expansion Demand`)
# Calculate the replacement demand from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
replacement <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year3
)
)
replacement <- sum(replacement$`Replacement Demand`)
replacement_year1_year2 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year2
)
)
replacement_year1_year2 <- sum(replacement_year1_year2$`Replacement Demand`)
replacement_year2_year3 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year2 + 1):year3
)
)
replacement_year2_year3 <- sum(replacement_year2_year3$`Replacement Demand`)
# Calculate the job openings from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
job_openings_tot <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year3
)
)
job_openings_tot <- sum(job_openings_tot$`Job Openings`)
# We assign expansion demand, replacement demand and job openings year1-year3,year1-year2,year2-year3 to a data frame with the same format as before: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
agg_industry_employment5 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year1-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = expansion
)
agg_industry_employment6 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = replacement
)
agg_industry_employment7 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = job_openings_tot
)
agg_industry_employment8 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year1-year2",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = expansion_year1_year2
)
agg_industry_employment9 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year2-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = expansion_year2_year3
)
agg_industry_employment10 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year2",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = replacement_year1_year2
)
agg_industry_employment11 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year2-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = replacement_year2_year3
)
# Calculate the job openings from year1-year2 (filter the data frame to include years from 2020-year2 and sum for the aggregate industry and geography)
job_openings_tot_year1_year2 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year1 + 1):year2
)
)
job_openings_tot_year1_year2 <- sum(job_openings_tot_year1_year2$`Job Openings`)
# Calculate the job openings from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
job_openings_tot_year2_year3 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(
Date %in% c(
as.numeric(year2 + 1):year3
)
)
job_openings_tot_year2_year3 <- sum(job_openings_tot_year2_year3$`Job Openings`)
agg_industry_employment12 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year2-year3",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = job_openings_tot_year2_year3
)
agg_industry_employment13 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year2",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = job_openings_tot_year1_year2
)
# Calculate Employment as a share of BC
employment_year1_shareBC <- 100 * employment_year1 / sum(JO_BC_year1$Employment)
agg_industry_employment14 <- # place this into a data frame: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment year1 as a Share of BC Emplyoment year1",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp_employment_year1$`Aggregate Industry`)[j],
"Value" = employment_year1_shareBC
)
# We need job openings (year1-year3 as a share of BC, and replacement year1-year3 as a share of BC)
job_openings_share <- 100 * job_openings_tot / sum(JO_BC_year1_year3$`Job Openings`)
agg_industry_employment15 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year3 as a Share of BC",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = job_openings_share
)
replacement_share <- 100 * replacement / job_openings_tot
agg_industry_employment16 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year3 as a Share of BC",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = replacement_share
)
# Calculate the average annual replacement rate
avg <- NULL
for (z in 3:nlevels(as.factor(temp$Date))) {
date1 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date %in% levels(as.factor(temp$Date))[z])
date2 <-
unique(filter(temp, temp$`Aggregate Industry` == levels(temp$`Aggregate Industry`)[j])) %>% filter(Date %in% levels(as.factor(temp$Date))[z])
emp <- sum(date1$Employment)
replacement2 <- sum(date2$`Replacement Demand`)
percent <- as.numeric(replacement2) / as.numeric(emp)
avg <- c(percent, avg)
}
replacement_rate <- mean(avg) * 100
agg_industry_employment17 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Annual Replacement Rate",
"Level" = "Aggregate Industry",
"Level Value" = levels(temp$`Aggregate Industry`)[j],
"Value" = replacement_rate
)
# Now we bind all of the aggregate industry information together into one data frame - this loop will repeat again for each level of aggregate industries for this specific geography
by_agg_industry <-
rbind(
agg_industry_employment,
agg_industry_employment2,
agg_industry_employment3,
agg_industry_employment4,
agg_industry_employment5,
agg_industry_employment6,
agg_industry_employment7,
agg_industry_employment8,
agg_industry_employment9,
agg_industry_employment10,
agg_industry_employment11,
agg_industry_employment12,
agg_industry_employment13,
agg_industry_employment14,
agg_industry_employment15,
agg_industry_employment16,
agg_industry_employment17,
by_agg_industry
)
}
# Once the variables have been calculate for each aggregate industry in the geography, the loop will be exited. We bind the results of all the aggregate industries for that geography into the data frame "by_aggregated_industry" and then we go on to the next geography in the loop and repeat
by_aggregated_industry <-
rbind(by_agg_industry, by_aggregated_industry)
}
# Ensure there are no duplicate entries in the data frame which may cause double counting.
by_aggregated_industry <- unique(by_aggregated_industry)
# 1.4_Sectors----------
# we need to calculate growth rates for both the sectors, individual industries and the aggregate industries because these cannot be aggregated directly in Tableau
# we will start by looping over the aggregated industries first
# Create two empty data frames to act as storage as we loop through
by_sector <- NULL # placeholder for first loop
by_sectors <- NULL # placeholder for second loop
# Handle by aggregate industry first, for each geography and at each aggregate industry level we are going to calculate employment year1, Employment Growth (CAGR) year1-year2, Employment Growth (CAGR) year2-year3, Employment Growth (CAGR) year1-year3, Expansion year1-year3, Replacement year1-year3, Total Job Openings year1-year3
# The final data frame will have five columns: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
# Need number of job openings for BC (year1) and (year1-year3)
JO_BC_year1 <- filter(jobs_employment, `Geographic Area` == "British Columbia") %>% filter(Date == year1)
JO_BC_year1_year3 <- filter(jobs_employment, `Geographic Area` == "British Columbia") %>% filter(Date %in% c(as.numeric(year1 + 1):year3))
for (i in 1:nlevels(jobs_employment$`Geographic Area`)) {
# loop through each geographic area
temp <-
filter(
jobs_employment,
`Geographic Area` == levels(jobs_employment$`Geographic Area`)[i]
) # filter a temporary data frame from "job_employment" that only includes the geographic area of interest
temp$`Sector` <-
as.factor(temp$`Sector`) # ensure that "sector code" (which is actually are aggregate industries) is a factor
# We need a column with labels for Employment year1, Employment Growth (CAGR) year1-year2, Employment Growth (CAGR) year2-year3, Employment Growth (CAGR) year1-year3, Expansion year1-year3, Replacement year1-year3, Total Job Openings year1-year3 for the AGGREGATED INDUSTRIES
for (j in 1:nlevels(temp$`Sector`)) {
# loop through each aggregate industry
# We are going to get the employment year1 data for the geography and aggregate industry first
temp_employment_year1 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date == year1) # filter to only include geography/aggregate industry and year to year1
employment_year1 <-
sum(as.numeric(temp_employment_year1$Employment)) # we need to sum this value to get employment for the aggregate industry
agg_industry_employment <- # place this into a data frame: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment year1",
"Level" = "Sector",
"Level Value" = levels(temp_employment_year1$`Sector`)[j],
"Value" = employment_year1
)
# Calculate the employment growth from year1 to year2: This is calculated as the (employment in year2/employment in year1 ^(1/n) - 1) (the CAGR, where n is the number of years)
temp_employmentgrowth_year1_year2 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date %in% c(year1, year2))
# Calculate the CAGR for year1 to year2
CAGR1 <-
100 * (((
sum(
filter(temp_employmentgrowth_year1_year2, Date %in% year2)$Employment
) / sum(
filter(temp_employmentgrowth_year1_year2, Date %in% year1)$Employment
)
)^(1 / 5)) - 1)
# Calculate the CAGR for year2 to year3
temp_employmentgrowth_year2_year3 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date %in% c(year2, year3))
CAGR2 <-
100 * (((
sum(
filter(temp_employmentgrowth_year2_year3, Date %in% year3)$Employment
) / sum(
filter(temp_employmentgrowth_year2_year3, Date %in% year2)$Employment
)
)^(1 / 5)) - 1)
# Calculate the CAGR for year1 to year3
temp_employmentgrowth_year1_year3 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date %in% c(year1, year3))
CAGR3 <-
100 * (((
sum(
filter(temp_employmentgrowth_year1_year3, Date %in% year3)$Employment
) / sum(
filter(temp_employmentgrowth_year1_year3, Date %in% year1)$Employment
)
)^(1 / 10)) - 1)
# We assign each of these CAGRS to a data frame with the same format as before: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
agg_industry_employment2 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year1-year2",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = CAGR1
)
agg_industry_employment3 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year2-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = CAGR2
)
agg_industry_employment4 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment Growth year1-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = CAGR3
)
# Calculate the expansion demand from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
expansion <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year3
)
)
expansion <- sum(expansion$`Expansion Demand`)
expansion_year1_year2 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year2
)
)
expansion_year1_year2 <- sum(expansion_year1_year2$`Expansion Demand`)
expansion_year2_year3 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year2) + 1):year3
)
)
expansion_year2_year3 <- sum(expansion_year2_year3$`Expansion Demand`)
# Calculate the replacement demand from year1-year3 (filter the data frame to include years from year1-year3 and sum for the aggregate industry and geography)
replacement <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year3
)
)
replacement <- sum(replacement$`Replacement Demand`)
replacement_year1_year2 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year2
)
)
replacement_year1_year2 <- sum(replacement_year1_year2$`Replacement Demand`)
replacement_year2_year3 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year2 + 1):year3
)
)
)
replacement_year2_year3 <- sum(replacement_year2_year3$`Replacement Demand`)
# Calculate the job openings from year1-year3 (filter the data frame to include years from year1-year3 and sum for the aggregate industry and geography)
job_openings_tot <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year3
)
)
job_openings_tot <- sum(job_openings_tot$`Job Openings`)
# We assign expansion demand, replacement demand and job openings year1-year3,year1-year2,year2-year3 to a data frame with the same format as before: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
agg_industry_employment5 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year1-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = expansion
)
agg_industry_employment6 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = replacement
)
agg_industry_employment7 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = job_openings_tot
)
agg_industry_employment8 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year1-year2",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = expansion_year1_year2
)
agg_industry_employment9 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Expansion year2-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = expansion_year2_year3
)
agg_industry_employment10 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year2",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = replacement_year1_year2
)
agg_industry_employment11 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year2-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = replacement_year2_year3
)
# Calculate the job openings from year1-year2 (filter the data frame to include years from 2020-year2 and sum for the aggregate industry and geography)
job_openings_tot_year1_year2 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year1) + 1):year2
)
)
job_openings_tot_year1_year2 <- sum(job_openings_tot_year1_year2$`Job Openings`)
# Calculate the job openings from year1-year3 (filter the data frame to include years from 2020-year3 and sum for the aggregate industry and geography)
job_openings_tot_year2_year3 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(
Date %in% c(
(as.numeric(year2) + 1):year3
)
)
job_openings_tot_year2_year3 <- sum(job_openings_tot_year2_year3$`Job Openings`)
agg_industry_employment12 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year2-year3",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = job_openings_tot_year2_year3
)
agg_industry_employment13 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year2",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = job_openings_tot_year1_year2
)
# Calculate Employment as a share of BC
employment_year1_shareBC <- 100 * employment_year1 / sum(JO_BC_year1$Employment)
agg_industry_employment14 <- # place this into a data frame: Geographic Area (specifying the region), Variable (Employment year1, Job Openings etc), Level (Aggregate Industry or LMO 61 Industry level), Level Value (the name assigned to the industry or aggregate industry) and Value (numeric value for the variable)
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Employment year1 as a Share of BC Emplyoment year1",
"Level" = "Sector",
"Level Value" = levels(temp_employment_year1$`Sector`)[j],
"Value" = employment_year1_shareBC
)
# We need job openings (year1-year3 as a share of BC, and replacement year1-year3 as a share of BC)
job_openings_share <- 100 * job_openings_tot / sum(JO_BC_year1_year3$`Job Openings`)
agg_industry_employment15 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Job Openings year1-year3 as a Share of BC",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = job_openings_share
)
replacement_share <- 100 * replacement / job_openings_tot
agg_industry_employment16 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Replacement year1-year3 as a Share of BC",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = replacement_share
)
# Calculate the average annual replacement rate
avg <- NULL
for (z in 3:nlevels(as.factor(temp$Date))) {
date1 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date %in% levels(as.factor(temp$Date))[z])
date2 <-
unique(filter(temp, temp$`Sector` == levels(temp$`Sector`)[j])) %>% filter(Date %in% levels(as.factor(temp$Date))[z])
emp <- sum(date1$Employment)
replacement2 <- sum(date2$`Replacement Demand`)
percent <- as.numeric(replacement2) / as.numeric(emp)
avg <- c(percent, avg)
}
replacement_rate <- mean(avg) * 100
agg_industry_employment17 <-
data.frame(
"Geographic Area" = levels(jobs_employment$`Geographic Area`)[i],
"Variable" = "Annual Replacement Rate",
"Level" = "Sector",
"Level Value" = levels(temp$`Sector`)[j],
"Value" = replacement_rate
)
# Now we bind all of the aggregate industry information together into one data frame - this loop will repeat again for each level of aggregate industries for this specific geography
by_sector <-
rbind(
agg_industry_employment,
agg_industry_employment2,
agg_industry_employment3,
agg_industry_employment4,
agg_industry_employment5,
agg_industry_employment6,
agg_industry_employment7,
agg_industry_employment8,
agg_industry_employment9,
agg_industry_employment10,
agg_industry_employment11,
agg_industry_employment12,
agg_industry_employment13,
agg_industry_employment14,
agg_industry_employment15,
agg_industry_employment16,
agg_industry_employment17,
by_sector
)
}