2010年10月14日木曜日

Rcode_20101014_2

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
2010/10/14,「生産年齢人口が減るとデフレ? その2」に使用したコードです。


#作業ディレクトリ指定
setwd("~/workdir/R")
#worldbankのデータを使用
#データ読み込み.元データは1999-2009年(差分をとるため)
x<-read.csv("pop_inf_data.csv")

#人口増加率
pop_growth <-x[(x[,2]==x[(11*3+1),2]),]
#ラベルとジンバブエを除く処理
pop_growth <-pop_growth[c(2:nrow(pop_growth)),c(4:(ncol(pop_growth)-1)) ]
#10年間(2000-2009)の平均をとる
m_pop_growth    <-mean(pop_growth,na.rm='true')

#~14歳人口比率
pop_u14_rate <-x[(x[,2]==x[1,2]),]
pop_u14_rate <-pop_u14_rate/pop_u14_rate[c(1,1:(nrow(pop_u14_rate)-1)),] -1
pop_u14_rate <-pop_u14_rate[c(2:nrow(pop_u14_rate)),c(4:(ncol(pop_u14_rate)-1)) ]
#~14歳人口成長率を計算
pop_u14_growth  <-pop_growth + (1 + pop_growth) * pop_u14_rate
m_pop_u14_growth <-mean(pop_u14_growth ,na.rm='true')

#15-64歳人口
pop_15_64_rate <-x[(x[,2]==x[(11+1),2]),]
pop_15_64_rate <-pop_15_64_rate/pop_15_64_rate[c(1,1:(nrow(pop_15_64_rate)-1)),] -1
pop_15_64_rate <-pop_15_64_rate[c(2:nrow(pop_15_64_rate)),c(4:(ncol(pop_15_64_rate)-1)) ]
pop_15_64_growth  <-pop_growth + (1 + pop_growth) * pop_15_64_rate
m_pop_15_64_growth <-mean(pop_15_64_growth ,na.rm='true')

#65歳~人口
pop_o65_rate <-x[(x[,2]==x[(11*2+1),2]),]
pop_o65_rate <-pop_o65_rate/pop_o65_rate[c(1,1:(nrow(pop_o65_rate)-1)),] -1
pop_o65_rate <-pop_o65_rate[c(2:nrow(pop_o65_rate)),c(4:(ncol(pop_o65_rate)-1)) ]
pop_o65_growth  <-pop_growth + (1 + pop_growth) * pop_o65_rate
m_pop_o65_growth <-mean(pop_o65_growth ,na.rm='true')

#物価上昇率(消費者物価)
inflation <-x[(x[,2]==x[(11*4+1),2]),]
inflation <-inflation[c(2:nrow(inflation)),c(4:(ncol(inflation)-1)) ]
m_inflation<-mean(inflation,na.rm='true')

#物価上昇率(GDPデフレータ)
deflator <-x[(x[,2]==x[(11*5+1),2]),]
deflator <-deflator[c(2:nrow(deflator)),c(4:(ncol(deflator)-1)) ]
m_deflator<-mean(deflator,na.rm='true')

#data結合
data_all <-cbind(m_pop_growth,m_pop_u14_growth ,m_pop_15_64_growth ,m_pop_o65_growth ,m_inflation,m_deflator)

#欠損値の除去(相関係数算出のため)
data_all_1<-na.omit(data_all)

#相関係数(cor_pe=ピアソンの積率相関係数, spearman=スピアマンの順位相関係数)
cor_pe <- cor(data_all_1,method="pearson")
cor_sp <- cor(data_all_1,method="spearman")

#ファイル出力
#ライブラリxtableを使用
library(xtable)
#aに相関係数表(html-table)を代入
a<-xtable(cor_pe)
#出力先指定
sink("pearson.html")
#pearson.html にhtmlコードを出力
print(a,"html")
#出力先を戻す
sink()


b<-xtable(cor_sp)
sink("spearman.html")
print(b,"html")
sink()

0 件のコメント:

コメントを投稿