Content
tail(gdp)
Date NominalGDP
19 2012年 494.9572
20 2013年 503.1756
21 2014年 513.8760
22 2015年 531.3198
23 2016年 535.9864
24 2017年 545.1219
# 次の4年分を欠損値とします。
missingRange <- c(11:14)
gdp[missingRange,]
Date NominalGDP
11 2004年 520.9654
12 2005年 524.1328
13 2006年 526.8797
14 2007年 531.6882
library(ggplot2)
g <- ggplot(data = gdp,mapping = aes(x = Date,y = NominalGDP)) + geom_point(size = 2) + geom_line(size = 0.1,group = 'NominalGDP') + theme_bw()
g <- g + annotate(geom = "rect",xmin = head(missingRange,1),xmax = tail(missingRange,1),ymin = -Inf,ymax = Inf,alpha = 0.1,fill = 'blue')
g <- g + annotate(geom = "text",x = mean(missingRange),y = Inf,label = '\nMissing',vjust = 1,hjust = 0.5)
g <- g + theme(axis.title.x = element_text(size = 0),axis.title.y = element_text(size = 0),axis.text.x = element_text(angle =90))
g
欠損値補間はパッケージ{imputeTS}を利用します。またアルゴリズムは1)linear interpolation、2)spline interpolation、3)Stineman interpolationの3種類とします。
library(imputeTS);library(tidyr)
gdp$missing <- gdp$NominalGDP
gdp$missing[missingRange] <- NA
gdp$liner <- na.interpolation(x = gdp$missing,option = 'linear')
gdp$spline <- na.interpolation(x = gdp$missing,option = 'spline')
gdp$stine <- na.interpolation(x = gdp$missing,option = 'stine')
gdptidy <- gather(data = gdp,key = 'key',value = 'value',colnames(gdp)[-c(1,2)],convert = T)
gdptidy$key <- factor(gdptidy$key,unique(gdptidy$key))
g <- ggplot(data = gdptidy,mapping = aes(x = Date,y = value,col = key,group = key)) + geom_point(size = 2) + theme_bw()
g <- g + geom_line(size = 0.1) + facet_wrap(.~key)
g <- g + theme(axis.title.x = element_text(size = 0),axis.title.y = element_text(size = 0),axis.text.x = element_text(angle =90),legend.position="none")
g <- g + annotate(geom = "rect",xmin = head(missingRange,1),xmax = tail(missingRange,1),ymin = -Inf,ymax = Inf,alpha = 0.1,fill = 'blue')
g
Date NominalGDP missing liner spline stine
1 1994年 501.5377 501.5377 501.5377 501.5377 501.5377
2 1995年 512.5417 512.5417 512.5417 512.5417 512.5417
3 1996年 525.8069 525.8069 525.8069 525.8069 525.8069
4 1997年 534.1425 534.1425 534.1425 534.1425 534.1425
5 1998年 527.8769 527.8769 527.8769 527.8769 527.8769
6 1999年 519.6518 519.6518 519.6518 519.6518 519.6518
7 2000年 526.7060 526.7060 526.7060 526.7060 526.7060
8 2001年 523.0050 523.0050 523.0050 523.0050 523.0050
9 2002年 515.9862 515.9862 515.9862 515.9862 515.9862
10 2003年 515.4007 515.4007 515.4007 515.4007 515.4007
11 2004年 520.9654 NA 516.4637 524.6289 515.8251
12 2005年 524.1328 NA 517.5267 537.8239 517.2226
13 2006年 526.8797 NA 518.5897 546.9362 518.8801
14 2007年 531.6882 NA 519.6527 543.9166 520.2083
15 2008年 520.7157 520.7157 520.7157 520.7157 520.7157
16 2009年 489.5010 489.5010 489.5010 489.5010 489.5010
17 2010年 500.3539 500.3539 500.3539 500.3539 500.3539
18 2011年 491.4085 491.4085 491.4085 491.4085 491.4085
19 2012年 494.9572 494.9572 494.9572 494.9572 494.9572
20 2013年 503.1756 503.1756 503.1756 503.1756 503.1756
21 2014年 513.8760 513.8760 513.8760 513.8760 513.8760
22 2015年 531.3198 531.3198 531.3198 531.3198 531.3198
23 2016年 535.9864 535.9864 535.9864 535.9864 535.9864
24 2017年 545.1219 545.1219 545.1219 545.1219 545.1219
結論:気軽に補間してはいけません。
データ分析のみならず人生において大抵のことは自分に都合良く準備されていません。今ではすっかり使い古された常套句になりますが『人生は配られたカードで勝負し続けるしかない』のです。無い事を嘆いていても時間の無駄、欠けている部分は能動的に補間していきましょう。と言うことでここではRを利用したデータ欠損値補間の例を確認します。なおランダムに生成したデータでは味気ないので日本の名目GDP(兆円)をサンプルとし欠損値補間と同時に失われた10年をチャートで確認しましょう。また補間アルゴリズム毎の結果の違いを際立出せるために変曲点を挟んだ範囲を欠損値とします。