-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.html
5656 lines (5574 loc) · 160 KB
/
work.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Yinuo Zhao" />
<title>Investigating the Socioeconomic Dynamics of Crime: Exploring the Relationship Between Income and Crime Rates in Canada</title>
<script src="site_libs/header-attrs-2.25/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.4.2/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark the anchor link active (and if it's in a dropdown, also mark that active)
var dropdown = menuAnchor.closest('li.dropdown');
if (window.bootstrap) { // Bootstrap 4+
menuAnchor.addClass('active');
dropdown.find('> .dropdown-toggle').addClass('active');
} else { // Bootstrap 3
menuAnchor.parent().addClass('active');
dropdown.addClass('active');
}
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">JSC370 Final Project</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Topic Description</a>
</li>
<li>
<a href="visual.html">Interactive Visualizations</a>
</li>
<li>
<a href="report.html">Full Report</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/inorrr/income_and_crime/">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/yinuozhao/">
<span class="fa fa-linkedin fa-lg"></span>
</a>
</li>
<li>
<a href="https://jsc370.github.io/JSC370-2024/">JSC370 Home</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Investigating the Socioeconomic Dynamics of
Crime: Exploring the Relationship Between Income and Crime Rates in
Canada</h1>
<h4 class="author">Yinuo Zhao</h4>
<h4 class="date">Mar 2, 2024</h4>
</div>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p><em>“Provide background on your data sets and a clear formulated
question or hypothesis.”</em></p>
<p>For this project, my question of Interest is: <strong>“How does crime
rate relate to income in Canada?”</strong></p>
<p>In order to answer this question, data on both crime and
socioeconomic status are needed. However, I found no existing data set
that contains all desired information, therefore this needs to be
achieved through merging more than one data sets. After choosing
carefully, the following two separate data sets are obtained:</p>
<ol style="list-style-type: decimal">
<li><p><strong>“Income of individuals by age group, sex and income
source, Canada, provinces and selected census metropolitan
areas”</strong>. Released 2023-05-02. This data set is annually updated
and maintained by Statistics Canada (Table 11-10-0239-01). Data is
collected through the Survey of Labor and Income Dynamics, Survey of
Consumer Finances, and Canadian Income Survey.</p></li>
<li><p><strong>“Incident-based crime statistics, by detailed violations,
Canada, provinces, territories, Census Metropolitan Areas and Canadian
Forces Military Police”</strong>. Released 2023-07-27. This data set is
also annually updated and maintained by Statistics Canada (Table
35-10-0177-01, formerly CANSIM 252-0051). Data is collected through the
Uniform Crime Reporting Survey.</p></li>
</ol>
<p>Understanding the relationship between crime rates and income in
Canada is crucial for policymakers, law enforcement agencies, and social
welfare programs. Exploring this correlation can shed light on the
socioeconomic factors driving criminal behavior and help formulate
targeted interventions to alleviate poverty and reduce crime.
Additionally, elucidating this connection can inform broader discussions
on social inequality, justice, and community well-being in Canadian
society.</p>
</div>
<div id="methods" class="section level2">
<h2>Methods</h2>
<p><em>“Include how and where the data were acquired, how you cleaned
and wrangled the data, what tools you used for data
exploration.”</em></p>
<p>Both data sets are downloaded directly from <strong>Statistics
Canada</strong>, which is usually considered to be an reliable source.
Because they share the same source, the data sets follows similar
structure and all contains the two columns <code>GEO</code> and
<code>REF_DATE</code> where the former one refers to the geographical
region and the second one refers to the year of data. Thus, it’s
possible to combine the two data sets to obtain all information
needed.</p>
<p>However, it is worth mentioning that both data sets are huge and
contains <strong>unrelated information</strong>. Therefore, cleaning and
wrangling are needed for more convenient analysis and more efficient
computing & uploading, as the original data sets are oversize thus
cannot be pushed to github repository.</p>
<p>Reference:</p>
<ol style="list-style-type: decimal">
<li>The census data set: <a
href="https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=1110023901"
class="uri">https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=1110023901</a></li>
<li>The crime data set: <a
href="https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=3510017701"
class="uri">https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=3510017701</a></li>
</ol>
<pre class="r"><code>fn1 <- "https://raw.githubusercontent.com/inorrr/JSC370_project/main/census.csv"
fn2 <- "https://raw.githubusercontent.com/inorrr/JSC370_project/main/crime.csv"
if (!file.exists("data/census.csv"))
download.file(fn1, destfile = "data/census.csv")
census_df <- data.table::fread("data/census.csv")
if (!file.exists("data/crime.csv"))
download.file(fn2, destfile = "data/crime.csv")
crime_df <- data.table::fread("data/crime.csv")</code></pre>
<div id="data-wrangling-and-cleaning" class="section level3">
<h3>Data Wrangling and Cleaning</h3>
<ol style="list-style-type: decimal">
<li>First I dropped the unrelated columns in both columns, keeping only
information revelant to the question of interest.</li>
</ol>
<pre class="r"><code>crime_df <- crime_df[, c("REF_DATE", "GEO", "Violations", "Statistics", "VALUE", "UOM")]
census_df <- census_df[, c("REF_DATE", "GEO", "Age group", "Sex", "Income source", "Statistics", "VALUE", "UOM", "SCALAR_FACTOR")]</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>I noticed that in the GEO column:</li>
</ol>
<ul>
<li>both data sets uses a <strong>mix of province & city
names</strong>. I removed all observations with city names and keep only
those province data so that the data set is smaller(since the province
data contains cities already) and avoids redundancy.</li>
<li>Notice that the census data only have data from the 10 provinces,
not the <strong>3 territories</strong>, I filtered out those data from
the crime data set as there are no matching data.</li>
<li>Notice that there is a <strong>square bracket</strong> with a number
behind each province name in the crime data, therefore I removed it with
regular expression so that we can join the two data sets on province
later.</li>
</ul>
<pre class="r"><code>table(census_df$GEO)
provinces1 <- c("Alberta [48]", "British Columbia [59]", "Manitoba [46]", "New Brunswick [13]",
"Newfoundland and Labrador [10]", "Saskatchewan [47]",
"Nova Scotia [12]", "Ontario [35]",
"Prince Edward Island [11]", "Quebec [24]")
provinces2 <- c("Alberta", "British Columbia", "Manitoba", "New Brunswick",
"Newfoundland and Labrador", "Saskatchewan","Nova Scotia",
"Ontario", "Prince Edward Island", "Quebec")
crime_df <- crime_df[crime_df$GEO %in% provinces1, ]
census_df <- census_df[census_df$GEO %in% provinces2, ]
crime_df$GEO <- gsub("\\s*\\[\\d+\\]$", "", crime_df$GEO)
table(crime_df$GEO)</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>In the Statistics column of the crime data, there are many measures
related to crime, but I’m only interested in the number of incidents
(<code>Actual incidents</code>) and crime rate
(<code>Rate per 100,000 population</code>), thus statistics related to
charges are removed. The Crime Severity
Index(<code>Percentage contribution to the Crime Severity Index (CSI)</code>)
seems to be interesting and thus is kept.</li>
</ol>
<pre class="r"><code>crime_df <- crime_df %>% filter(Statistics == "Actual incidents" |
Statistics == "Rate per 100,000 population" |
Statistics == "Percentage contribution to the Crime Severity Index (CSI)")</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>In the crime data frame,</li>
</ol>
<ul>
<li>there are <strong>314 different types of crime</strong>, which does
much more detailed categorization than I’m interested in. Therefore, to
avoid having too much computation to merge data later on, I choose too
keep only the bigger categories (i.e. total robbery, total assaults,
etc).</li>
<li>There are also square brackets at the end so I removed them.</li>
</ul>
<pre class="r"><code>print(length(unique(crime_df$Violations)))
table(crime_df$Violations)
# Identify rows that start with "Total"
total_rows <- grepl("^Total", crime_df$Violations)
# Subset the dataframe to keep only the rows starting with "Total"
crime_df <- crime_df[total_rows, , drop = FALSE]
# Remove square brackets and numbers at the end
crime_df$Violations <- gsub("\\s*\\[\\d+\\]$", "", crime_df$Violations)</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>In the census data frame:</li>
</ol>
<ul>
<li><p>the column <code>Age group</code> specifies the age however since
we do not have this information in the crime data frame, we need to
combine all age groups. This can be done by taking the average of the
categories.</p></li>
<li><p>Same for <code>Sex</code>, same method is used.</p></li>
</ul>
<pre class="r"><code>table(census_df$"Age group")
table(census_df$"Sex")
# first we merge the age group categories
census_df <- census_df %>%
group_by(REF_DATE, GEO, Sex, `Income source`, Statistics, UOM, SCALAR_FACTOR) %>%
summarise(VALUE = mean(VALUE, na.rm = TRUE))
# next we merge the age group categories
census_df <- census_df_new %>%
group_by(REF_DATE, GEO, `Income source`, Statistics, UOM, SCALAR_FACTOR) %>%
summarise(VALUE = mean(VALUE, na.rm = TRUE))</code></pre>
<ol start="4" style="list-style-type: decimal">
<li>I keep only the data between 1998 and 2021, as that’s the year range
where the two data sets overlap.</li>
</ol>
<pre class="r"><code>crime_df <- crime_df %>% filter(REF_DATE >= 1998 & REF_DATE <= 2021)
census_df <- census_df %>% filter(REF_DATE >= 1998 & REF_DATE <= 2021)</code></pre>
<ol start="5" style="list-style-type: decimal">
<li>Write the cleaned data frame to CSV files.</li>
</ol>
<pre class="r"><code>write.csv(crime_df, "/Users/yinuozhao/Desktop/UofT/JSC370/JSC370-2024-main/JSC370_project/crime.csv")
write.csv(census_df, "/Users/yinuozhao/Desktop/UofT/JSC370/JSC370-2024-main/JSC370_project/census.csv")</code></pre>
<p>At this point both the crime data frame and census data frame has
<code>REF_DATE</code> and <code>GEO</code> in common, and they each have
another <strong>categorical variable</strong>, which is
<code>Income source</code> for census data and <code>Violation</code>(it
means crime type) for crime data. While it may seem to make sense to
join the two data sets using REF_DATE and GEO directly, the results
would involves the data for all combinations of Income source and
Violation for each REF_DATE and GEO. This will be a huge data set and
thus slow down the computation. Therefore, I choose to <strong>keep the
data sets separate</strong> and <strong>join them when
necessary</strong> (i.e. after picking out certain categories of
interest).</p>
</div>
<div id="exploratory-data-analysis" class="section level3">
<h3>Exploratory Data Analysis</h3>
<p>Notice that right now both data sets are in long format, I converted
them to wide for convenience.</p>
<pre class="r"><code>crime_df <- pivot_wider(crime_df, id_cols = c(REF_DATE, GEO, Violations),
names_from = Statistics, values_from = VALUE)
crime_df <- na.omit(crime_df)
census_df <- pivot_wider(census_df, id_cols = c(REF_DATE, GEO, `Income source`),
names_from = Statistics, values_from = VALUE)
census_df <- na.omit(census_df)</code></pre>
<p><em>Check the dimensions and headers and footers of the data</em></p>
<pre class="r"><code>dim(census_df)
dim(crime_df)
head(crime_df)
tail(crime_df)
head(census_df)
tail(census_df)</code></pre>
<p>The census data set has <strong>8 variables</strong> with
<strong>3613 observations</strong>, the crime data set has <strong>6
variables</strong> with <strong>9271 observations</strong>. By looking
at the headers and footers of both data sets, they seems to be imported
correctly and contains no missing values (in the displayed rows).</p>
<p><em>Check the variable types in the data</em></p>
<pre class="r"><code>str(census_df)
str(crime_df)
summary(census_df)
summary(crime_df)</code></pre>
<p>In both data frames, we see that the variable types are a mix of
integer, numeric and characters. All variable types correctly align with
the context of the variables. No major problems arises with the data at
this stage (i.e. a variable with all missing values.)</p>
<p><em>Take a closer look at some/all of the variables</em></p>
<p>For both data frame, we need <code>REF_DATE</code> and
<code>GEO</code> to correctly identify a province in Canada with a valid
year. For census data frame, we need to look at the values of the
different types of income (median, aggregate, etc). For the crime data
frame, we need to look at the recorded crime rate and actual number of
incidents to be within the reasonable range.</p>
<pre class="r"><code>table(census_df$REF_DATE)
table(census_df$GEO)
table(crime_df$REF_DATE)
table(crime_df$GEO)
summary(census_df$`Aggregate income`)
summary(census_df$`Average income (excluding zeros)`)
summary(census_df$`Median income (excluding zeros)`)
summary(crime_df$`Actual incidents`)
summary(crime_df$`Rate per 100,000 population`)</code></pre>
<p>Both data sets contains data from 1998 to 2021, on the 10 provinces
in Canada as desired because I cleaned the data sets this way. Other
variables being checked are within the reasonable range. The aggregate
income, average income and median income are all measured in 2021
constant dollars, aggregate income record numbers in millions. The crime
rates are measured as number of incidents per 100,000 population.</p>
<p><em>Validate with an external source</em></p>
<p>Notice that the minimum average income is <strong>677.8</strong>,
which seems to be much lower than then mean average income, even 10
times lower than the 1st quantile. Since it seems quite suspicious, we
need to validate it.</p>
<pre class="r"><code>census_df[which.min(census_df$`Average income (excluding zeros)`), ]</code></pre>
<p>This data is from <strong>Prince Edward Island</strong> in 2004, and
the income source is <strong>“other government transfers”</strong>. Upon
research, Government transfers refers to assistance from provincial and
municipal programs, Workers’ Compensation benefits, the GST/HST Credit
and provincial refundable tax credits such as the Quebec and
Newfoundland and Labrador sales tax credits. However, since many of the
above mentioned are made to their own category and excluded from “other
government transfers” in the data set, it make sense that the value is
low.</p>
</div>
</div>
<div id="preliminary-results" class="section level2">
<h2>Preliminary Results</h2>
<p><em>“Provide summary statistics in tabular from and
publication-quality figures, take a look at the kable function from
knitr to write nice tables in Rmarkdown.”</em></p>
<div id="looking-at-crime-data" class="section level3">
<h3>Looking at Crime Data</h3>
<p>Firstly, I examined the trend in crime rate across provinces and
crime types.</p>
<pre class="r"><code>filtered_data = crime_df %>% filter(Violations=="Total, all violations")
unique_x_values <- unique(crime_df$REF_DATE)
ggplot(filtered_data, aes(x = REF_DATE, y = `Rate per 100,000 population`, color = GEO)) +
geom_line() +
labs(x = "Year", y = "Rate per 100,000 population", title = "Rates of Total Crime by Province") +
scale_x_continuous(breaks = unique_x_values) +
scale_color_discrete(name = "Provinces") +
theme_linedraw() +
theme(legend.position = "right") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(face = "bold")) +
theme(plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm")) +
theme(axis.text = element_text(size = 7)) +
theme(legend.title = element_text(face = "bold"))</code></pre>
<p><img src="work_files/figure-html/unnamed-chunk-13-1.png" width="960" style="display: block; margin: auto;" />
The plot depicts the <strong>total crime rate</strong>, encompassing all
types of crimes, from 1998 to 2021, with each province represented by a
distinct color. The crime rate is measured per 100,000 population.
Notably, Saskatchewan consistently exhibits a significantly higher crime
rate compared to other provinces throughout the period of 1998 to 2021.
Conversely, Quebec and Ontario consistently demonstrate the lowest crime
rates. Across all provinces, there is a discernible <strong>decreasing
trend</strong> in crime rates over the years, with many provinces
experiencing peak crime rates in 2003-2004.</p>
<p>In addition to analyzing total crime, I delved into specific crime
categories often associated with poverty: <strong>break and
enter</strong>, <strong>robbery</strong>, and
<strong>prostitution</strong>. The three accompanying plots illustrate
their respective rates over the years. Overall, there is a decreasing
trend in the rates of all three crimes, with occasional exceptions such
as robbery rates in Manitoba. Notably, British Columbia stands out with
a significantly high rate of prostitution in 2004, doubling the number
reported in Saskatchewan, which held the second-highest rate that
year.</p>
<p><em>Note that the 3 plots below shares the same legend with the above
plots. Therefore the legend is omitted for better display purpose. Codes
are also not shown as they reuses the above code chunk. Crime rate is
measured per 100,000 population.</em></p>
<p><img src="work_files/figure-html/unnamed-chunk-15-1.png" width="1440" style="display: block; margin: auto;" /></p>
<p>Presented below is a table summarizing the average crime rate across
all crime types (excluding <code>Total, all violations</code>)
categorized by province and year. The cells are color-coded by value,
with lighter shades indicating higher values and darker shades
representing smaller values. Upon scrolling through the table, it
becomes evident that across all provinces, there is a discernible
<strong>decreasing trend in the average crime rate</strong>, as
evidenced by the darkening shades in each column. This observation
aligns with the findings and inferences drawn from the preceding
plots.</p>
<div
style="border: 1px solid #ddd; padding: 0px; overflow-y: scroll; height:500px; overflow-x: scroll; width:100%; ">
<table class="table table-striped table" style="color: black; margin-left: auto; margin-right: auto; color: black; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="border-bottom:hidden;padding-bottom:0; padding-left:3px;padding-right:3px;text-align: center; position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;" colspan="11">
<div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">
Table of Average Crime Rate Across All Types
</div>
</th>
</tr>
<tr>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Year
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Alberta
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
British Columbia
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Manitoba
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
New Brunswick
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Newfoundland and Labrador
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Nova Scotia
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Ontario
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Prince Edward Island
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Quebec
</th>
<th style="text-align:right;position: sticky; top:0; background-color: #FFFFFF;position: sticky; top:0; background-color: #FFFFFF;">
Saskatchewan
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;">
1998
</td>
<td style="text-align:right;color: white !important;background-color: rgba(70, 192, 111, 255) !important;">
990.7389
</td>
<td style="text-align:right;color: white !important;background-color: rgba(246, 230, 32, 255) !important;">
1310.2795
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 161, 135, 255) !important;">
1140.4897
</td>
<td style="text-align:right;color: white !important;background-color: rgba(178, 221, 45, 255) !important;">
746.1692
</td>
<td style="text-align:right;color: white !important;background-color: rgba(42, 176, 127, 255) !important;">
644.1653
</td>
<td style="text-align:right;color: white !important;background-color: rgba(192, 223, 37, 255) !important;">
875.0903
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
776.1408
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 153, 138, 255) !important;">
686.4670
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
702.7424
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 149, 139, 255) !important;">
1420.691
</td>
</tr>
<tr>
<td style="text-align:right;">
1999
</td>
<td style="text-align:right;color: white !important;background-color: rgba(80, 196, 106, 255) !important;">
996.9833
</td>
<td style="text-align:right;color: white !important;background-color: rgba(173, 220, 48, 255) !important;">
1243.8727
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 161, 136, 255) !important;">
1139.4081
</td>
<td style="text-align:right;color: white !important;background-color: rgba(192, 223, 37, 255) !important;">
751.8170
</td>
<td style="text-align:right;color: white !important;background-color: rgba(32, 146, 140, 255) !important;">
616.3186
</td>
<td style="text-align:right;color: white !important;background-color: rgba(216, 226, 25, 255) !important;">
890.2262
</td>
<td style="text-align:right;color: white !important;background-color: rgba(142, 214, 69, 255) !important;">
705.8405
</td>
<td style="text-align:right;color: white !important;background-color: rgba(88, 199, 101, 255) !important;">
786.3608
</td>
<td style="text-align:right;color: white !important;background-color: rgba(165, 219, 54, 255) !important;">
649.5519
</td>
<td style="text-align:right;color: white !important;background-color: rgba(37, 131, 142, 255) !important;">
1372.294
</td>
</tr>
<tr>
<td style="text-align:right;">
2000
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 160, 136, 255) !important;">
938.1432
</td>
<td style="text-align:right;color: white !important;background-color: rgba(129, 211, 77, 255) !important;">
1202.5849
</td>
<td style="text-align:right;color: white !important;background-color: rgba(32, 163, 134, 255) !important;">
1146.4238
</td>
<td style="text-align:right;color: white !important;background-color: rgba(119, 209, 83, 255) !important;">
719.6546
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 152, 139, 255) !important;">
621.9546
</td>
<td style="text-align:right;color: white !important;background-color: rgba(92, 200, 99, 255) !important;">
807.6322
</td>
<td style="text-align:right;color: white !important;background-color: rgba(127, 211, 78, 255) !important;">
695.1511
</td>
<td style="text-align:right;color: white !important;background-color: rgba(45, 178, 125, 255) !important;">
739.1711
</td>
<td style="text-align:right;color: white !important;background-color: rgba(173, 220, 48, 255) !important;">
653.3770
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 152, 139, 255) !important;">
1428.722
</td>
</tr>
<tr>
<td style="text-align:right;">
2001
</td>
<td style="text-align:right;color: white !important;background-color: rgba(58, 186, 118, 255) !important;">
979.9516
</td>
<td style="text-align:right;color: white !important;background-color: rgba(162, 218, 55, 255) !important;">
1233.4349
</td>
<td style="text-align:right;color: white !important;background-color: rgba(72, 193, 110, 255) !important;">
1213.3189
</td>
<td style="text-align:right;color: white !important;background-color: rgba(105, 205, 91, 255) !important;">
712.1708
</td>
<td style="text-align:right;color: white !important;background-color: rgba(32, 147, 140, 255) !important;">
617.2849
</td>
<td style="text-align:right;color: white !important;background-color: rgba(99, 203, 95, 255) !important;">
812.8562
</td>
<td style="text-align:right;color: white !important;background-color: rgba(94, 201, 98, 255) !important;">
672.0454
</td>
<td style="text-align:right;color: white !important;background-color: rgba(55, 184, 120, 255) !important;">
752.6111
</td>
<td style="text-align:right;color: white !important;background-color: rgba(142, 214, 69, 255) !important;">
634.2719
</td>
<td style="text-align:right;color: white !important;background-color: rgba(56, 185, 119, 255) !important;">
1518.370
</td>
</tr>
<tr>
<td style="text-align:right;">
2002
</td>
<td style="text-align:right;color: white !important;background-color: rgba(76, 194, 108, 255) !important;">
994.3392
</td>
<td style="text-align:right;color: white !important;background-color: rgba(176, 221, 47, 255) !important;">
1245.2322
</td>
<td style="text-align:right;color: white !important;background-color: rgba(63, 188, 115, 255) !important;">
1202.5438
</td>
<td style="text-align:right;color: white !important;background-color: rgba(134, 213, 73, 255) !important;">
726.1322
</td>
<td style="text-align:right;color: white !important;background-color: rgba(37, 171, 130, 255) !important;">
639.0897
</td>
<td style="text-align:right;color: white !important;background-color: rgba(103, 204, 92, 255) !important;">
816.2289
</td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 191, 112, 255) !important;">
651.8003
</td>
<td style="text-align:right;color: white !important;background-color: rgba(168, 219, 52, 255) !important;">
850.6072
</td>
<td style="text-align:right;color: white !important;background-color: rgba(115, 208, 86, 255) !important;">
617.0995
</td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 181, 123, 255) !important;">
1505.492
</td>
</tr>
<tr>
<td style="text-align:right;">
2003
</td>
<td style="text-align:right;color: white !important;background-color: rgba(194, 223, 35, 255) !important;">
1065.0392
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
1317.9670
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
1366.9247
</td>
<td style="text-align:right;color: white !important;background-color: rgba(205, 225, 29, 255) !important;">
758.0414
</td>
<td style="text-align:right;color: white !important;background-color: rgba(64, 189, 114, 255) !important;">
657.4530
</td>
<td style="text-align:right;color: white !important;background-color: rgba(221, 227, 24, 255) !important;">
893.6070
</td>
<td style="text-align:right;color: white !important;background-color: rgba(50, 182, 122, 255) !important;">
632.2778
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
915.8641
</td>
<td style="text-align:right;color: white !important;background-color: rgba(147, 215, 65, 255) !important;">
638.6003
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
1728.543
</td>
</tr>
<tr>
<td style="text-align:right;">
2004
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
1098.5319
</td>
<td style="text-align:right;color: white !important;background-color: rgba(251, 231, 35, 255) !important;">
1315.9251
</td>
<td style="text-align:right;color: white !important;background-color: rgba(231, 228, 25, 255) !important;">
1347.9319
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
780.0538
</td>
<td style="text-align:right;color: white !important;background-color: rgba(86, 198, 103, 255) !important;">
667.5708
</td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 255) !important;">
915.0119
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 161, 136, 255) !important;">
595.3678
</td>
<td style="text-align:right;color: white !important;background-color: rgba(223, 227, 24, 255) !important;">
893.0314
</td>
<td style="text-align:right;color: white !important;background-color: rgba(124, 210, 80, 255) !important;">
624.6361
</td>
<td style="text-align:right;color: white !important;background-color: rgba(208, 225, 28, 255) !important;">
1684.091
</td>
</tr>
<tr>
<td style="text-align:right;">
2005
</td>
<td style="text-align:right;color: white !important;background-color: rgba(221, 227, 24, 255) !important;">
1079.1975
</td>
<td style="text-align:right;color: white !important;background-color: rgba(192, 223, 37, 255) !important;">
1260.0459
</td>
<td style="text-align:right;color: white !important;background-color: rgba(142, 214, 69, 255) !important;">
1278.7128
</td>
<td style="text-align:right;color: white !important;background-color: rgba(70, 192, 111, 255) !important;">
692.0257
</td>
<td style="text-align:right;color: white !important;background-color: rgba(52, 182, 121, 255) !important;">
650.8968
</td>
<td style="text-align:right;color: white !important;background-color: rgba(168, 219, 52, 255) !important;">
859.9862
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 148, 140, 255) !important;">
574.3292
</td>
<td style="text-align:right;color: white !important;background-color: rgba(117, 208, 84, 255) !important;">
812.8373
</td>
<td style="text-align:right;color: white !important;background-color: rgba(112, 207, 87, 255) !important;">
615.6142
</td>
<td style="text-align:right;color: white !important;background-color: rgba(173, 220, 48, 255) !important;">
1649.431
</td>
</tr>
<tr>
<td style="text-align:right;">
2006
</td>
<td style="text-align:right;color: white !important;background-color: rgba(94, 201, 98, 255) !important;">
1007.2778
</td>
<td style="text-align:right;color: white !important;background-color: rgba(129, 211, 77, 255) !important;">
1203.3584
</td>
<td style="text-align:right;color: white !important;background-color: rgba(119, 209, 83, 255) !important;">
1260.0861
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 255) !important;">
648.6084
</td>
<td style="text-align:right;color: white !important;background-color: rgba(86, 198, 103, 255) !important;">
667.1994
</td>
<td style="text-align:right;color: white !important;background-color: rgba(162, 218, 55, 255) !important;">
856.8851
</td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 255) !important;">
589.4051
</td>
<td style="text-align:right;color: white !important;background-color: rgba(45, 178, 125, 255) !important;">
737.7965
</td>
<td style="text-align:right;color: white !important;background-color: rgba(103, 204, 92, 255) !important;">
609.9108
</td>
<td style="text-align:right;color: white !important;background-color: rgba(46, 179, 124, 255) !important;">
1502.769
</td>
</tr>
<tr>
<td style="text-align:right;">
2007
</td>
<td style="text-align:right;color: white !important;background-color: rgba(112, 207, 87, 255) !important;">
1018.9281
</td>
<td style="text-align:right;color: white !important;background-color: rgba(70, 192, 111, 255) !important;">
1137.0500
</td>
<td style="text-align:right;color: white !important;background-color: rgba(84, 197, 104, 255) !important;">
1226.7060
</td>
<td style="text-align:right;color: white !important;background-color: rgba(39, 126, 142, 255) !important;">
608.8173
</td>
<td style="text-align:right;color: white !important;background-color: rgba(170, 220, 50, 255) !important;">
697.5675
</td>
<td style="text-align:right;color: white !important;background-color: rgba(86, 198, 103, 255) !important;">
803.5808
</td>
<td style="text-align:right;color: white !important;background-color: rgba(34, 141, 141, 255) !important;">
560.8689