-
Notifications
You must be signed in to change notification settings - Fork 1
/
Imputation.html
796 lines (696 loc) · 27.8 KB
/
Imputation.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Data</title>
<script src="site_libs/header-attrs-2.16/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/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<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>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.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 {
content: "";
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: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.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 -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<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">Home</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="Introduction.html">Introduction</a>
</li>
<li>
<a href="Imputation.html">Data</a>
</li>
<li>
<a href="EDA.html">EDA</a>
</li>
<li>
<a href="analysis.html">Prelimary Analysis</a>
</li>
<li>
<a href="Linear_Regression_Model.html">Regression</a>
</li>
<li>
<a href="Conclusion.html">Conclusion</a>
</li>
<li>
<a href="https://7lx8uo-txbrook.shinyapps.io/map_dashboard/#section-life-expectancy">Shiny App</a>
</li>
<li>
<a href="Project-report.html">Report</a>
</li>
<li>
<a href="mailto:sx2337@cumc.columbia.edu?subject=About%20your%20website">
<span class="fa fa-envelope fa-lg"></span>
</a>
</li>
<li>
<a href="https://github.com/ShunXie/SX.github.io/">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Data</h1>
</div>
<div id="background-information-of-the-datasets" class="section level1">
<h1>Background Information of the datasets</h1>
<div id="life-expectancy-dataset" class="section level2">
<h2>Life Expectancy Dataset</h2>
<p>We choose WHO Life Expectancy Dataset as our main dataset in our
study. The data can be accessed <a
href="https://www.kaggle.com/datasets/mmattson/who-national-life-expectancy?resource=download">here</a>.
The code is read as following:</p>
<pre class="r"><code>suppressMessages(library(tidyverse))
suppressMessages(library(dbplyr))
options(tibble.print_min = 5)
#Get life expectancy data
LifeExpectancy = read_csv("data/who_life_exp.csv",col_types = cols(.default = col_guess())) %>%
janitor::clean_names()</code></pre>
<p>Life expectancy datasets contains a total of <code>183</code>
countries with year ranging from <code>2000</code> to <code>2016</code>.
There are a total of <code>32</code> variables. Out of which, we
consider the variables and description is listed below:</p>
<p>*<code>country</code>: country name</p>
<p>*<code>country_code</code>: Three letter of the country id</p>
<p>*<code>region</code>: The region of the country</p>
<p>*<code>year</code>: year of all values stored</p>
<p>*<code>life_expect</code>: Life expectancy at birth measured in year.
It is continuous variable.</p>
<p>*<code>life_exp60</code>: Life expectancy at age 60 measured in year.
It is continuous variable.</p>
<p>*<code>adult_mortality</code>: Adult Mortality Rates of both sexes
(probability of dying between 15 and 60 years per 1000 population)</p>
<p>*<code>infant_mort</code>: Death rate up to age 1</p>
<p>*<code>age1_4mort</code>: Death rate between ages 1 and 4</p>
<p>*<code>alcohol</code>: Alcohol, recorded per capita (15+) consumption
(in litres of pure alcohol)</p>
<p>*<code>bmi</code>: Mean BMI (kg/<span
class="math inline">\(m^2\)</span>) (18+) (age-standardized
estimate)</p>
<p>*<code>age5_19thinness</code>: Prevalence of thinness among children
and adolescents. This is measured in a crude estimate percentage for
children with BMI < (median - 2 s.d.)</p>
<p>*<code>age5_19obesity</code>: Prevalence of obesity among children
and adolescents. This is measured in a crude estimate percentage for
children with BMI < (median - 2 s.d.)</p>
<p>*<code>hepatitis</code>: Hepatitis B (HepB) immunization coverage
among 1-year-olds (%)</p>
<p>*<code>measles</code>: Measles-containing-vaccine first-dose (MCV1)
immunization coverage among 1-year-olds (%)</p>
<p>*<code>polio</code>: Polio (Pol3) immunization coverage among
1-year-olds (%)</p>
<p>*<code>diphtheria</code>: Diphtheria tetanus toxoid and pertussis
(DTP3) immunization coverage among 1-year-olds (%)</p>
<p>*<code>basic_water</code>: Population using at least basic
drinking-water services</p>
<p>*<code>doctors</code>: Number of medical doctors (per 10,000)</p>
<p>*<code>hospitals</code>: Total density of hospitals per 100 000
population</p>
<p>*<code>gni_capita</code>: Gross national income per capita in
dollars. This is measured from GHO server</p>
<p>*<code>gghe_d</code>: Domestic general government health expenditure
(GGHE-D) as percentage of gross domestic product (GDP). This is measured
from GHO server</p>
<p>*<code>che_gdp</code>: Current health expenditure (CHE) as percentage
of gross domestic product (GDP) (%)</p>
<p>*<code>une_pop</code>: Population (thousands)</p>
<p>*<code>une_infant</code>: Mortality rate, infant (per 1,000 live
births). This is measured from GHO server</p>
<p>*<code>une_life</code>: (Our response variable) Life expectancy at
birth, total (years). This is measured from GHO server. It is contains
less missing value than life expectancy.</p>
<p>*<code>une_hiv</code>: Prevalence of HIV, total (% of population ages
15-49). This is measured from GHO server</p>
<p>*<code>une_gni</code>: GNI per capita, PPP (current international $).
This is measured from UNESCO server</p>
<p>*<code>une_poverty</code>: Government expenditure on education as a
percentage of GDP (%). This is measured from UNESCO server</p>
<p>*<code>une_edu_spend</code>: Adult literacy rate, population 15+
years, both sexes (%). This is measured from UNESCO server</p>
<p>*<code>une_literacy</code>: Mean years of schooling (ISCED 1 or
higher), population 25+ years, both sexes. This is measured from UNESCO
server</p>
<p><br> <br></p>
</div>
<div id="country-code-dataset" class="section level2">
<h2>Country Code Dataset</h2>
<p>Country code dataset contains the status of countries, such as the
information of the independence status and its capital city. The data is
extracted from <a
href="https://github.com/datasets/country-codes/blob/master/data/country-codes.csv">here</a>.
We include the variable specifies whether the country is a developed or
developing country. The data is read in the following code and is merged
by the code.</p>
<pre class="r"><code>country_code =
read_csv("data/country-codes.csv", show_col_types = FALSE) %>%
select(`ISO3166-1-Alpha-3`, `Developed / Developing Countries`)
merged_data_PM = merge(LifeExpectancy, country_code, by.x = "country_code", by.y = "ISO3166-1-Alpha-3" )</code></pre>
<p><br> <br></p>
</div>
<div id="pm2.5-dataset" class="section level2">
<h2>PM2.5 dataset</h2>
<p>PM2.5 dataset is a dataset that specifies the percentage of total
population exposed to levels exceeding WHO guideline value. Such value
exists over year 1960 to 2021 but most of the value before 2010 and
after 2017 are missing. The dataset can be accessed <a
href="https://data.worldbank.org/indicator/EN.ATM.PM25.MC.M3">here</a>.
The following code read and clean the dataset.</p>
<p>merge the data with dataset contain pm 2.5 information:</p>
<pre class="r"><code>#read pm2.5 data
PM_dataset =
read_csv("data/pm2.5.csv", show_col_types = FALSE, skip = 4)
PM_dataset_clean =
PM_dataset %>%
janitor::clean_names() %>%
select(-(x1960:x1999),-(x2017:x2021)) %>%
pivot_longer(
x2000:x2016,
names_to = "year",
names_prefix = "x",
values_to = "PM_value"
) %>%
select(country_code, year, PM_value) %>%
mutate(year = as.numeric(year))</code></pre>
<p>The following code is used to merge with all dataset.</p>
<pre class="r"><code>#merged data with developed/ developing countries
merged_data_d =
merged_data_PM %>%
left_join(PM_dataset_clean, by = c("country_code","year"))</code></pre>
<p><br> <br></p>
</div>
<div id="income-level-data" class="section level2">
<h2>Income level data</h2>
<p>Income level data is a dataset specifies the income status accessed
<a
href="https://databank.worldbank.org/source/world-development-indicators">here</a>.
It is a factor variable with four levels, namely high income, low
income, lower middle income and upper middle income. The dataset is
cleaned and merged in the following code.</p>
<pre class="r"><code>#read income level data
Income_dataset = read_csv("data/income_level.csv", show_col_types = FALSE) %>%
janitor::clean_names() %>%
select(country_code,income_group)
#merged data with income level
merged_data_i = merged_data_d %>%
left_join( Income_dataset, by = c("country_code")) %>%
select(country_code:une_school,PM_value,income_group,`Developed / Developing Countries`)</code></pre>
<p><br> <br></p>
</div>
<div id="health-expenditure" class="section level2">
<h2>Health expenditure</h2>
<p>Health expenditure dataset measures the health expenditure from
government with respect to different countries. The dataset is retrieved
from <a
href="https://ourworldindata.org/grapher/public-health-expenditure-share-gdp-owid?country=SWE~FRA~DEU~JPN~GBR~BEL~ESP~AUS~NZL~CAN~USA">here</a>.
The dataset is measured in percentage of GDP or dollar value per capita.
We choose percentage of GDP as our unit because the dataset measured in
percentage of GDP has more complete values. Here is the coding for read,
clean and merge the Health expenditure dataset to original dataset.</p>
<pre class="r"><code>health_exp = read_csv("data/government_compulsory_health expenditure_from_1970_2020.csv") %>%
janitor::clean_names() %>%
filter(measure=="PC_GDP") %>%
filter(subject=="TOT") %>%
mutate("country_code"=location,
"year" = time) %>%
select(country_code,year, value)
merged_data_h =
merged_data_i %>%
left_join(health_exp, by = c("country_code","year")) %>%
mutate("health_exp" = value) %>%
select(country_code:une_school,PM_value,income_group,health_exp,`Developed / Developing Countries`)</code></pre>
<p><br> <br></p>
</div>
<div id="parliament-information" class="section level2">
<h2>Parliament Information</h2>
<p>Parliament information data is a dataset that measured the percentage
of women in national parliament around 2015~2018 for different
countries. Since different countries have different election dates, and
there is rare data before 2015, we choose the dataset that is around
2015~2018. Then, we adjust the date to year 2015, and add the percentage
of women in lower house of national parliament to the last column of the
original dataset. The data origins from <a
href="https://data.ipu.org/women-ranking?month=1&year=2019">here</a>
and the following code cleans and merge the data:</p>
<pre class="r"><code>women_in_parliament <-
read_csv("data/around_2019_women_percent_in_national_parliaments.csv", skip = 5) %>%
janitor::clean_names() %>%
select(x2, elections_3, percent_w_6) %>%
mutate("country" = x2,
"percent_w_in_lower_house" = percent_w_6,
"year" = as.double(2015)) %>%
select(country, year, percent_w_in_lower_house)
merged_data =
merged_data_h %>%
left_join(women_in_parliament, by = c("country","year")) %>%
select(country_code:une_school,PM_value,health_exp,income_group,`Developed / Developing Countries`, percent_w_in_lower_house)</code></pre>
<p>Store the data</p>
<pre class="r"><code>write.csv(merged_data , file = "data/Merged_expectation.csv")</code></pre>
<p><br> <br></p>
</div>
</div>
<div id="impute" class="section level1">
<h1>Impute</h1>
<p>For the simplicity and accuracy purpose, imputation using weighted k
nearest neighbour algorithm (w-kNN) is considered in my study. It is
built on simple k nearest neighbour(kNN) but unlike kNN, it considers
the weight of each dimension so that it circumvents the problem of
neglecting the correlation between missing variables and other variables
according to <a
href="https://ieeexplore.ieee.org/abstract/document/5199781">Ling et.
al.</a>. Additionally, such a non-parametric method does not make any
assumption on the distribution of the input vector hence it is suitable
for correlation analysis by <a
href="https://iopscience.iop.org/article/10.1088/1742-6596/1025/1/012114/meta">mukid’s
paper</a>. In my method, we choose Euclidean distance as my measurement
for the similarity between authorities as it performs better than the
other metrics, namely <a
href="https://ieeexplore.ieee.org/abstract/document/6873626">Manhattan
distance</a>. Thus, any missing value <span
class="math inline">\(x_{i,j}\)</span> at time t can be imputed using an
inverse of Euclidean distance as a weighting factor, where the subscript
i corresponds to the <span class="math inline">\(i_{th}\)</span> country
and subscript j represents the <span
class="math inline">\(j_{th}\)</span> sub-indicator (or the <span
class="math inline">\(j_{th}\)</span> variable in the dataset). \</p>
<p>To obtain the imputation for missing value <span
class="math inline">\(x_{i,j}\)</span>, euclidean distances between all
underlying pairs of country <span
class="math inline">\((a_i,a_k)\)</span> at year t without missing value
need to be calculated. The pair with the largest euclidean distance
$d_t(a_i,a_k) $, however, will not be considered, and instead it is
treated as the denominator for standardization of other available
euclidean distance values. For a pair of country <span
class="math inline">\((a_i,a_k)\)</span> at year t, the euclidean
distance $d_t(a_i,a_k) $ can be obtained by the following formula: <span
class="math display">\[
\begin{equation}
\mathbf{d}_t(a_i,a_k)=||a_i,a_k||=\sqrt{\sum_{j=1}^J w(a_{ij}-a_{kj})^2}
\end{equation}
\]</span> where J is the total number of variables or non missing data
and w is the weighting function by <a
href="https://ieeexplore.ieee.org/abstract/document/5199781">Ling et.
al.</a>. As my available data may have different length due to different
number of missing values, I choose <span
class="math inline">\(w=\frac{1}{J}\)</span> as my weight in the
Euclidean distance. Then, I can normalize the Euclidean distance by
dividing the largest euclidean distance in the available data set for
country i. Without loss of generality, let <span
class="math inline">\(\mathbf{d}_t(a_i,a_l)\)</span> be the maximum
euclidean distance for country i. Then for <span
class="math inline">\(k\neq l\)</span>: <span class="math display">\[
\begin{equation}
\mathbf{D}_t(a_i,a_k)=\frac{\mathbf{d}_t(a_i,a_k)}{\mathbf{d}_t(a_i,a_{l})}
\end{equation}
\]</span></p>
<p>To balance the size of the imputed value for missing value <span
class="math inline">\(x_{i,j}\)</span>, I consider the weight as the
inverse of Euclidean distance. Such an approach is validated in <a
href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3777229">Laumann
et. al’s work</a> where they believe the imputed value will be similar
to countries with sub-indicator that has small Euclidean distance. With
all other things being equal, countries are replaced by authorities and
sub-indicators are variables in my data. Hence, having computed
euclidean distances between all possible pairs and years, the missing
value is measured as:</p>
<p><span class="math display">\[\begin{equation}
x^t_{i,j}=\frac{1}{K}\sum_k \frac{1}{|\mathbf{D}_t(a_i,a_k)|}x^t_{k,j}
\end{equation}\]</span> where K is the total number of countries that
have values at year t for variable j.</p>
<p>This is nicely packed in filling package in R. In our case, we choose
k to be 4. This is according to the income level of countries. More
detail is justified in clustering part in our analysis.</p>
<pre class="r"><code>raw_mat =
merged_data %>%
select(-country, -country_code, -region,-income_group, -`Developed / Developing Countries`,percent_w_in_lower_house) %>%
as.matrix()
imputed_mat = filling::fill.KNNimpute(raw_mat, k = 2)</code></pre>
<pre class="r"><code>#colnames_exp = colnames(LifeExpectancy)
Imputed_expectancy = merged_data
for (i in 1:(length(merged_data)-6)) {
Imputed_expectancy[i+3][[1]] = imputed_mat[[1]][,i]
}
head(Imputed_expectancy %>% select(-percent_w_in_lower_house))</code></pre>
<pre><code>## country_code country region year life_expect life_exp60
## 1 AFG Afghanistan Eastern Mediterranean 2000 55.89618 15.14620
## 2 AFG Afghanistan Eastern Mediterranean 2001 56.52523 15.20886
## 3 AFG Afghanistan Eastern Mediterranean 2002 57.43997 15.24703
## 4 AFG Afghanistan Eastern Mediterranean 2003 57.97100 15.35566
## 5 AFG Afghanistan Eastern Mediterranean 2004 58.42978 15.44257
## 6 AFG Afghanistan Eastern Mediterranean 2005 58.90924 15.52284
## adult_mortality infant_mort age1_4mort alcohol bmi age5_19thinness
## 1 316.0496 0.098245 0.011050 0.4562055 21.7 20.6
## 2 307.2416 0.095925 0.010625 3.5850006 21.8 20.4
## 3 292.3430 0.093330 0.010130 2.8226608 21.9 20.2
## 4 286.4569 0.090470 0.009655 1.4059016 22.0 20.0
## 5 281.8943 0.087595 0.009210 1.2471070 22.1 19.8
## 6 277.1813 0.084630 0.008785 0.0162300 22.2 19.6
## age5_19obesity hepatitis measles polio diphtheria basic_water doctors
## 1 0.6 76.13455 27 24 24 27.77190 0.8403400
## 2 0.7 71.38447 37 35 33 27.79726 1.8990000
## 3 0.8 59.58910 35 36 36 29.90076 0.8168644
## 4 0.9 45.72630 39 41 41 32.00507 0.7011373
## 5 1.0 72.57173 48 50 50 34.12623 0.4960309
## 6 1.1 62.91274 50 58 58 36.26526 0.8882858
## hospitals gni_capita gghe_d che_gdp une_pop une_infant une_life une_hiv
## 1 0.4254508 802.1334 0.7777427 4.704074 20779.95 90.6 55.841 0.1
## 2 0.4229862 1060.0000 0.3869379 7.475627 21606.99 88.0 56.308 0.1
## 3 0.4191697 870.0000 0.0841800 9.443390 22600.77 85.4 56.784 0.1
## 4 2.0438096 920.0000 0.6509600 8.941260 23680.87 82.8 57.271 0.1
## 5 0.4081089 920.0000 0.5429300 9.808470 24726.68 80.1 57.772 0.1
## 6 0.4073613 1020.0000 0.5291800 9.948290 25654.28 77.4 58.290 0.1
## une_gni une_poverty une_edu_spend une_literacy une_school PM_value
## 1 931.0142 75.35382 3.831208 23.86542 1.486406 100
## 2 4217.6150 50.62891 3.657184 47.14927 1.458467 100
## 3 1084.1678 50.52512 2.576543 30.36625 2.665628 100
## 4 1431.1906 56.79214 2.701828 38.24146 2.370850 100
## 5 1524.5696 57.40101 3.567654 38.10863 2.240961 100
## 6 1157.3764 14.05812 1.623665 77.07397 2.163486 100
## health_exp income_group Developed / Developing Countries
## 1 4.262374 Low income Developing
## 2 4.170745 Low income Developing
## 3 4.134256 Low income Developing
## 4 4.057298 Low income Developing
## 5 4.060042 Low income Developing
## 6 4.066611 Low income Developing</code></pre>
<pre class="r"><code>write.csv(Imputed_expectancy , file = "data/Imputed_expectation.csv")</code></pre>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// temporarily add toc-ignore selector to headers for the consistency with Pandoc
$('.unlisted.unnumbered').addClass('toc-ignore')
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>