-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamelia.html
813 lines (742 loc) · 32.1 KB
/
amelia.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Missing Data Imputation</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.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>
<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;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</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">
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;
}
.table th:not([align]) {
text-align: left;
}
</style>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
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;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.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: #ffffff;
}
.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>
// 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 it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
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;
}
.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-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">IS709 - Introduction to Data Science</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Tutorials
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="installation.html">R/RStudio Installation</a>
</li>
<li>
<a href="azure_notebooks.html">Azure Notebooks</a>
</li>
<li>
<a href="intro_to_r.html">Introduction to R</a>
</li>
<li>
<a href="amelia.html">Missing Data Imputation</a>
</li>
<li>
<a href="discretization.html">Dimensionality Reduction and Discretization</a>
</li>
<li>
<a href="classification.html">Classification</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://mehmetaliakyol.com/">
<span class="fa fa-question fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Missing Data Imputation</h1>
</div>
<p>First, we need to load the required packages and the data.</p>
<pre class="r"><code>#install.packages("Amelia")
#install.packages("VIM")
require(Amelia)
require(VIM)
data(freetrade)
summary(freetrade)</code></pre>
<pre><code>## year country tariff polity
## Min. :1981 Length:171 Min. : 7.10 Min. :-8.000
## 1st Qu.:1985 Class :character 1st Qu.: 16.30 1st Qu.:-2.000
## Median :1990 Mode :character Median : 25.20 Median : 5.000
## Mean :1990 Mean : 31.65 Mean : 2.905
## 3rd Qu.:1995 3rd Qu.: 40.80 3rd Qu.: 8.000
## Max. :1999 Max. :100.00 Max. : 9.000
## NA's :58 NA's :2
## pop gdp.pc intresmi signed
## Min. : 14105080 Min. : 149.5 Min. :0.9036 Min. :0.0000
## 1st Qu.: 19676715 1st Qu.: 420.1 1st Qu.:2.2231 1st Qu.:0.0000
## Median : 52799040 Median : 814.3 Median :3.1815 Median :0.0000
## Mean :149904501 Mean : 1867.3 Mean :3.3752 Mean :0.1548
## 3rd Qu.:120888400 3rd Qu.: 2462.9 3rd Qu.:4.4063 3rd Qu.:0.0000
## Max. :997515200 Max. :12086.2 Max. :7.9346 Max. :1.0000
## NA's :13 NA's :3
## fiveop usheg
## Min. :12.30 Min. :0.2558
## 1st Qu.:12.50 1st Qu.:0.2623
## Median :12.60 Median :0.2756
## Mean :12.74 Mean :0.2764
## 3rd Qu.:13.20 3rd Qu.:0.2887
## Max. :13.20 Max. :0.3083
## NA's :18</code></pre>
<div id="missingness-analysis" class="section level2">
<h2>Missingness Analysis</h2>
<p>We first need to see if the missingness is at random. To see that, we visualize the missing variables.</p>
<p>Missingness map plot of <code>Amelia</code> package visualizes the missing values of the dataset.</p>
<pre class="r"><code>missmap(freetrade, rank.order = F)</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-2-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>We could also use <code>aggr</code> function of <code>VIM</code> package to visualize both the total number of missing data in each variable and to see if they have any co-occuring missingness.</p>
<pre class="r"><code>aggr(freetrade)</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-3-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>Or we could take a look at the missingness patterns of two variables using bar charts of <code>VIM</code> package. Let’s assume we want to see if missingness in <code>tariff</code> variable has anything to do with the <code>year</code> variable.</p>
<pre class="r"><code>barMiss(freetrade[,c("year","tariff")])</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-4-1.png" width="672" style="display: block; margin: auto;" /></p>
<pre><code>##
## Click in in the left margin to switch to the previous variable or in the right margin to switch to the next variable.
## To regain use of the VIM GUI and the R console, click anywhere else in the graphics window.</code></pre>
<p>Looks like some years had more missing value than others. If you want to make sure that this relation is properly utilized, you may want to separate your dataset by years and impute accordingly, however while doing that you may be left with too few data in years with too many missing data.</p>
<p>Let’s take a look at the relation between <code>country</code> variable and <code>tariff</code> variable:</p>
<pre class="r"><code>barMiss(freetrade[,c("country","tariff")])</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-5-1.png" width="672" style="display: block; margin: auto;" /></p>
<pre><code>##
## Click in in the left margin to switch to the previous variable or in the right margin to switch to the next variable.
## To regain use of the VIM GUI and the R console, click anywhere else in the graphics window.</code></pre>
<p>The same relationship can be explored using histograms instead of bar charts for numeric variables:</p>
<pre class="r"><code>histMiss(freetrade[,c("gdp.pc","tariff")])</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-6-1.png" width="672" style="display: block; margin: auto;" /></p>
<pre><code>##
## Click in in the left margin to switch to the previous variable or in the right margin to switch to the next variable.
## To regain use of the VIM GUI and the R console, click anywhere else in the graphics window.</code></pre>
<p>In this case, there seems to be a uniform missingness of <code>tariff</code> values with respect to <code>gdp.pc</code> variable. If you look at the proportion of missing data and non-missing data, the proportion is roughly the same in each bin up to <code>gdp.pc</code> value 6000.</p>
<p>Or we can use two-variable scatter plot matrix to see the relations of all the numeric variables in one window. First we, need to subset numeric variables:</p>
<pre class="r"><code>nums <- sapply(freetrade, is.numeric)
marginmatrix(freetrade[,nums])</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-7-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>Or similarly, we could use single scatterplots of two variables:</p>
<pre class="r"><code>marginplot(freetrade[,c("intresmi", "tariff")])</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-8-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>The red numbers (58 & 13) show missing values in respective variables. The dark red number (4) tells you, how many cases were missing for both variables. The red dots show the missing points. These two variables don?t have many co-occurrences of missing cases and the missingness is quite evenly dispersed so there seems to be no relation regarding missing values. The values seem to be missing at random.</p>
</div>
<div id="listwise-deletion" class="section level2">
<h2>Listwise Deletion</h2>
<p>To perform listwise deletion, we use <code>na.omit</code> function. This function, as the name suggests, omits the rows with <code>NA</code> values.</p>
<pre class="r"><code>f_lw <- na.omit(freetrade)
summary(f_lw)</code></pre>
<pre><code>## year country tariff polity
## Min. :1981 Length:96 Min. : 8.70 Min. :-8.00
## 1st Qu.:1986 Class :character 1st Qu.: 19.27 1st Qu.:-2.00
## Median :1990 Mode :character Median : 27.15 Median : 5.00
## Mean :1990 Mean : 33.89 Mean : 2.99
## 3rd Qu.:1993 3rd Qu.: 41.40 3rd Qu.: 8.00
## Max. :1997 Max. :100.00 Max. : 9.00
## pop gdp.pc intresmi signed
## Min. : 14105080 Min. : 149.5 Min. :0.9036 Min. :0.0000
## 1st Qu.: 21236308 1st Qu.: 445.8 1st Qu.:2.0371 1st Qu.:0.0000
## Median : 57408690 Median : 901.4 Median :3.0996 Median :0.0000
## Mean :156749853 Mean : 1893.4 Mean :3.1874 Mean :0.1875
## 3rd Qu.:114282510 3rd Qu.: 2410.2 3rd Qu.:4.3325 3rd Qu.:0.0000
## Max. :962377664 Max. :11925.3 Max. :7.9346 Max. :1.0000
## fiveop usheg
## Min. :12.30 Min. :0.2558
## 1st Qu.:12.50 1st Qu.:0.2623
## Median :12.70 Median :0.2734
## Mean :12.79 Mean :0.2730
## 3rd Qu.:13.20 3rd Qu.:0.2813
## Max. :13.20 Max. :0.2988</code></pre>
</div>
<div id="missing-data-imputation-with-meanmedian" class="section level2">
<h2>Missing Data Imputation with Mean/Median</h2>
<p>With this method, missing values are replaced with the mean/median of this variable. Even though this is most widely used imputation method, it is usually inappropriate due to the fact that it reduces the variance of the variable.</p>
<p>Our adjusted R^2 increased. Obviously those points were affecting the model.</p>
<pre class="r"><code>f_mv <- freetrade
for (i in 1:ncol(f_mv)){
miss <- f_mv[,i]
if (is.numeric(miss)){
missing <- is.na(miss)
miss[missing] <- mean(miss, na.rm = T)
}
f_mv[,i]<-miss
}
summary(f_mv)</code></pre>
<pre><code>## year country tariff polity
## Min. :1981 Length:171 Min. : 7.10 Min. :-8.000
## 1st Qu.:1985 Class :character 1st Qu.: 21.35 1st Qu.:-2.000
## Median :1990 Mode :character Median : 31.65 Median : 5.000
## Mean :1990 Mean : 31.65 Mean : 2.905
## 3rd Qu.:1995 3rd Qu.: 31.65 3rd Qu.: 8.000
## Max. :1999 Max. :100.00 Max. : 9.000
## pop gdp.pc intresmi signed
## Min. : 14105080 Min. : 149.5 Min. :0.9036 Min. :0.0000
## 1st Qu.: 19676715 1st Qu.: 420.1 1st Qu.:2.2681 1st Qu.:0.0000
## Median : 52799040 Median : 814.3 Median :3.2611 Median :0.0000
## Mean :149904501 Mean : 1867.3 Mean :3.3752 Mean :0.1548
## 3rd Qu.:120888400 3rd Qu.: 2462.9 3rd Qu.:4.2750 3rd Qu.:0.0000
## Max. :997515200 Max. :12086.2 Max. :7.9346 Max. :1.0000
## fiveop usheg
## Min. :12.30 Min. :0.2558
## 1st Qu.:12.50 1st Qu.:0.2623
## Median :12.70 Median :0.2756
## Mean :12.74 Mean :0.2764
## 3rd Qu.:13.20 3rd Qu.:0.2887
## Max. :13.20 Max. :0.3083</code></pre>
</div>
<div id="missing-data-imputation-using-random-values-from-the-same-dataset" class="section level2">
<h2>Missing Data Imputation using Random Values from the Same Dataset</h2>
<p>With this method, we replace missing values in the variable with randomly sampled non-missing values of the same variable. Even though this is an easy fix, this randomization is not an appropriate solution as it does not reflect the nature of data.</p>
<pre class="r"><code>f_rv <- freetrade
for (i in 1:ncol(f_rv)){
miss <- f_rv[,i]
missing <- is.na(miss)
n.missing <- sum(missing)
miss.obs <- miss[!missing]
miss[missing]<-sample(miss.obs, n.missing, replace = T)
f_rv[,i]<-miss
}
summary(f_rv)</code></pre>
<pre><code>## year country tariff polity
## Min. :1981 Length:171 Min. : 7.10 Min. :-8.00
## 1st Qu.:1985 Class :character 1st Qu.: 15.80 1st Qu.:-2.00
## Median :1990 Mode :character Median : 25.00 Median : 5.00
## Mean :1990 Mean : 31.65 Mean : 2.93
## 3rd Qu.:1995 3rd Qu.: 40.90 3rd Qu.: 8.00
## Max. :1999 Max. :100.00 Max. : 9.00
## pop gdp.pc intresmi signed
## Min. : 14105080 Min. : 149.5 Min. :0.9036 Min. :0.0000
## 1st Qu.: 19676715 1st Qu.: 420.1 1st Qu.:2.2457 1st Qu.:0.0000
## Median : 52799040 Median : 814.3 Median :3.2307 Median :0.0000
## Mean :149904501 Mean : 1867.3 Mean :3.4485 Mean :0.1637
## 3rd Qu.:120888400 3rd Qu.: 2462.9 3rd Qu.:4.4413 3rd Qu.:0.0000
## Max. :997515200 Max. :12086.2 Max. :7.9346 Max. :1.0000
## fiveop usheg
## Min. :12.30 Min. :0.2558
## 1st Qu.:12.50 1st Qu.:0.2623
## Median :12.60 Median :0.2756
## Mean :12.75 Mean :0.2764
## 3rd Qu.:13.20 3rd Qu.:0.2887
## Max. :13.20 Max. :0.3083</code></pre>
</div>
<div id="multiple-imputation-missing-data-imputation-using-amelia" class="section level2">
<h2>Multiple Imputation: Missing Data Imputation using Amelia</h2>
<p>Amelia assumes your data is distributed with a multivariate normal distribution and imputes values based on that assumption by bootrapping (randomly selecting and modelling) values from your data. The advantage of Amelia is that is uses the whole data to estimate your missing values. However, if your data does not have a normal distribution, you may need to transform your variables.</p>
<p>For instance, if the variable with missing values follows a log-normal distribution, you should use log transform of the variable for imputation. And after imputation, you need to transform it back to its original state. You can transform your variable by taking its logarithm initially and you can change it back by exponentiating after you have finished imputation.</p>
<pre class="r"><code>data(freetrade)
a.out <- amelia(freetrade, m = 5, ts = "year", cs = "country",
noms = "signed", ords = "polity")</code></pre>
<pre><code>## -- Imputation 1 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
##
## -- Imputation 2 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
##
## -- Imputation 3 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
##
## -- Imputation 4 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12
##
## -- Imputation 5 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14</code></pre>
<p>We have performed 5 imputations (m = 5) using <code>Amelia</code>. <code>ts</code>, stands for “time series” and it represents your time stamp variable while <code>cs</code> stands for “cross section” and it represents the cross sectional variable. Determining <code>ts</code> and <code>cs</code> variables tells amelia to omit those variables from imputation. <code>noms</code> expression stands for nominal variables while <code>ords</code> expression lets you state ordinal variables.</p>
<pre class="r"><code>summary(a.out)</code></pre>
<pre><code>##
## Amelia output with 5 imputed datasets.
## Return code: 1
## Message: Normal EM convergence.
##
## Chain Lengths:
## --------------
## Imputation 1: 13
## Imputation 2: 15
## Imputation 3: 18
## Imputation 4: 12
## Imputation 5: 14
##
## Rows after Listwise Deletion: 96
## Rows after Imputation: 171
## Patterns of missingness in the data: 8
##
## Fraction Missing for original variables:
## -----------------------------------------
##
## Fraction Missing
## year 0.00000000
## country 0.00000000
## tariff 0.33918129
## polity 0.01169591
## pop 0.00000000
## gdp.pc 0.00000000
## intresmi 0.07602339
## signed 0.01754386
## fiveop 0.10526316
## usheg 0.00000000</code></pre>
<p>Similarly, <code>idvars</code> are known as “identification variables” that are used to identify the data point in a database. For instance “SSN (social security number)” is an <code>idvar</code>. <code>Amelia</code> omits <code>idvars</code> from imputation but it still gives you the original values of the omitted variables in the imputed dataset.</p>
<pre class="r"><code>a.out2 <- amelia(freetrade, m = 5, idvars = c("year", "country"),
noms = "signed", ords = "polity")</code></pre>
<pre><code>## -- Imputation 1 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
##
## -- Imputation 2 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
##
## -- Imputation 3 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
##
## -- Imputation 4 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 21 22
##
## -- Imputation 5 --
##
## 1 2 3 4 5 6 7 8 9 10</code></pre>
<pre class="r"><code>summary(a.out2)</code></pre>
<pre><code>##
## Amelia output with 5 imputed datasets.
## Return code: 1
## Message: Normal EM convergence.
##
## Chain Lengths:
## --------------
## Imputation 1: 16
## Imputation 2: 13
## Imputation 3: 13
## Imputation 4: 22
## Imputation 5: 10
##
## Rows after Listwise Deletion: 96
## Rows after Imputation: 171
## Patterns of missingness in the data: 8
##
## Fraction Missing for original variables:
## -----------------------------------------
##
## Fraction Missing
## year 0.00000000
## country 0.00000000
## tariff 0.33918129
## polity 0.01169591
## pop 0.00000000
## gdp.pc 0.00000000
## intresmi 0.07602339
## signed 0.01754386
## fiveop 0.10526316
## usheg 0.00000000</code></pre>
<p>After obtaining 5 imputed datasets, we average it for the variable to obtain an average imputed dataset. I have only taken the average of <code>tariff</code> variable, but you need to average all imputed variables to form a complete dataset.</p>
<pre class="r"><code>trf <- cbind(a.out$imputations$imp1$tariff,
a.out$imputations$imp2$tariff,
a.out$imputations$imp3$tariff,
a.out$imputations$imp4$tariff,
a.out$imputations$imp5$tariff)
tariff <- rowMeans(trf)
data(freetrade)
#You can also parallelize this using parallel = "multicore" flag.
a.out3 <- amelia(freetrade, m = 5, idvars = c("year", "country"),
noms = "signed", ords = "polity",
parallel = "multicore")</code></pre>
<pre><code>## -- Imputation 1 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
##
## -- Imputation 2 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
##
## -- Imputation 3 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13
##
## -- Imputation 4 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
##
##
## -- Imputation 5 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15</code></pre>
<pre class="r"><code>summary(freetrade$tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 7.10 16.30 25.20 31.65 40.80 100.00 58</code></pre>
<pre class="r"><code>summary(tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 7.10 19.04 27.90 32.30 40.82 100.00</code></pre>
</div>
<div id="single-imputation-missing-data-imputation-using-amelia" class="section level2">
<h2>Single Imputation: Missing Data Imputation using Amelia</h2>
<p>If we want to use the entire dataset for imputation instead of bootstrap EM, we can set the flag <code>boot.type = ?none?</code>. Since we have used the entire set, there is no point in using multiple imputation as the estimates will be the same everytime so we use <code>m=1</code>.</p>
<pre class="r"><code>data(freetrade)
a.out.single <- amelia(freetrade, m = 1, ts = "year", cs = "country", noms = "signed", ords = "polity", boot.type = "none")</code></pre>
<pre><code>## -- Imputation 1 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14</code></pre>
<pre class="r"><code>summary(a.out.single)</code></pre>
<pre><code>##
## Amelia output with 1 imputed datasets.
## Return code: 1
## Message: Normal EM convergence.
##
## Chain Lengths:
## --------------
## Imputation 1: 14
##
## Rows after Listwise Deletion: 96
## Rows after Imputation: 171
## Patterns of missingness in the data: 8
##
## Fraction Missing for original variables:
## -----------------------------------------
##
## Fraction Missing
## year 0.00000000
## country 0.00000000
## tariff 0.33918129
## polity 0.01169591
## pop 0.00000000
## gdp.pc 0.00000000
## intresmi 0.07602339
## signed 0.01754386
## fiveop 0.10526316
## usheg 0.00000000</code></pre>
<p>Since we have one imputed dataset, we can simply assign this to the variable.</p>
<pre class="r"><code>tariff <- a.out.single$imputations$imp1$tariff
summary(tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -9.595 17.226 27.800 33.770 46.050 102.888</code></pre>
<pre class="r"><code>summary(freetrade$tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 7.10 16.30 25.20 31.65 40.80 100.00 58</code></pre>
<p>Suppose that we already know that <code>tariff</code> values can only have a minimum value of 0 and a maximum value of 100, we can impose those limits using <code>Amelia</code>:</p>
<pre class="r"><code>data(freetrade)
bds <- matrix(c(3, 0, 100), nrow = 1, ncol = 3) #c(variable_no, min, max)
a.out.bds <- amelia(freetrade, m = 1, ts = "year", cs = "country", noms = "signed", ords = "polity", boot.type = "none", bounds = bds)</code></pre>
<pre><code>## -- Imputation 1 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14</code></pre>
<pre class="r"><code>summary(a.out.bds)</code></pre>
<pre><code>##
## Amelia output with 1 imputed datasets.
## Return code: 1
## Message: Normal EM convergence.
##
## Chain Lengths:
## --------------
## Imputation 1: 14
##
## Rows after Listwise Deletion: 96
## Rows after Imputation: 171
## Patterns of missingness in the data: 8
##
## Fraction Missing for original variables:
## -----------------------------------------
##
## Fraction Missing
## year 0.00000000
## country 0.00000000
## tariff 0.33918129
## polity 0.01169591
## pop 0.00000000
## gdp.pc 0.00000000
## intresmi 0.07602339
## signed 0.01754386
## fiveop 0.10526316
## usheg 0.00000000</code></pre>
<p>Again, since we have one imputed dataset, we can simply assign this to the variable.</p>
<pre class="r"><code>tariff2 <- a.out$imputations$imp1$tariff
summary(tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -9.595 17.226 27.800 33.770 46.050 102.888</code></pre>
<pre class="r"><code>summary(tariff)</code></pre>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -9.595 17.226 27.800 33.770 46.050 102.888</code></pre>
</div>
<div id="diagnostics" class="section level2">
<h2>Diagnostics</h2>
<p>Previously, we performed imputations with and without boundary restrictions. To see if they are different from each other, let’s perform some error diagnostics on the Malaysian subset of <code>tariff</code> variable:</p>
<pre class="r"><code>par(mfrow = c(1,2))
tscsPlot(a.out, cs = "Malaysia", main = "No logical bounds", var ="tariff", ylim = c(-10,60))
tscsPlot(a.out.bds, cs = "Malaysia", main = "With logical bounds", var ="tariff", ylim = c(-10,60))</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-27-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>The change in graph is visible. The red dots indicate the imputed values and the lines indicate the error margin of imputations. When we take a look at the last two imputations, the error margins are much smaller.</p>
<p>Let’s take a look at the densities of observed and imputed values. You need to check this for each variable that you used missing value imputation on.</p>
<pre class="r"><code>par(mfrow = c(2,2))
compare.density(a.out, var = "tariff",main="Tariff density")
compare.density(a.out, var = "polity",main="Polity density")
compare.density(a.out, var = "intresmi",main="Intresmi density")
compare.density(a.out, var = "fiveop",main="Fiveop density")</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-28-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>The imputations we performed seem to have captured the general behavior of the variable with some deviation. We need to perform overimputation to see if the imputed values for the variables are acceptable.</p>
<p>Overimputation:</p>
<pre class="r"><code>par(mfrow = c(2,2))
overimpute(a.out, var = "tariff",main="Tariff Overimputation")
overimpute(a.out, var = "polity",main="Polity Overimputation")
overimpute(a.out, var = "intresmi",main="Intresmi Overimputation")
overimpute(a.out, var = "fiveop",main="Fiveop Overimputation")</code></pre>
<p><img src="amelia_files/figure-html/unnamed-chunk-29-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>In an overimputation graph, the mean imputations are given as dots and the vertical lines of dots represent the 90% confidence interval of that imputation. The black colored orthagonal line is the diagnostics line. If the mean imputation or the 90% confidence interval line of imputation overlaps with the diagnostics line, this means the imputation is within acceptable range and it can be used. If most of the imputations are acceptable, then the dataset can be used. The color of the line (as coded in the legend) represents the fraction of missing observations in the pattern of missingness for that observation.</p>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').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 -->
<!-- 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>