財務省のサイトより日本国債の金利データを取得し、XTS形式で扱えるように変換する。すなわち、第一フィールドの日付データが和暦(Sで昭和を、Hで平成を表している)に、第二フィールド以降の金利データを小数点以下3桁固定のフォーマットに変換する。
system ( "wget http://www.mof.go.jp/jgbs/reference/interest_rate/data/jgbcm_all.csv ");
$file=". /jgbcm_all.csv";
$outfile="> . /jgb_seireki.csv";
open ( IN, $file) or die "$!";
open ( OUT, $outfile) or die "$!";
print ( "# start\n");
print OUT "Date,oneY,twoY,threeY,fourY,fiveY,sixY,sevenY,eightY,nineY\n"; # output head line
$line_num = 0;
while (< IN>) {
if ( $line_num > 1) { # skip 1st and 2nd lines.
$gengou =substr ( $_, 0,1); # pick up the first character of the line.
@buff = split( /, /, $_); # split the line by comma.
@data = split(/\./, $buff[ 0]); # split the first field by period.
$data[ 0];
$year =substr ( $data[ 0], 1, length( $data[ 0])); # pick up the numerical part of wareki data.
# print OUT "$gengou";
if ( $gengou eq 'S') { # if the first character = S, data belongs to showa .
$year=$year+1925; # add 1925 to adjust asshowa period started in 1926.
}
else { # just for the case of heisei until 2019.
$year=$year+1988; # adjust forheisei period.
}
for ($count = 1; $count < 10; $count++) {
$interest[ $count] = sprintf ( "%. 3f", $buff[ $count]); # zero padding each interest data.
}
$output = $interest[ 1].",". $interest[ 2].",". $interest[ 3].",". $interest[ 4].",". $interest[ 5].",". $interest[ 6].",". $interest[ 7].",". $interest[ 8].",". $interest[ 9];
print OUT "$year-$data[1]-$data[2],$output\n";
}
# print OUT "$year-$data[1]-$data[2],$buff[2],$buff[9]\n";}
$line_num++;
# print
}
close ( IN);
close ( OUT);
system ( "rm jgbcm_all.csv")
data comes ascsv format andfilename is jgbcm_all.csvexecute wget command.skip 1st and 2nd linesconvert data( see 7.).execute system( "perl jgb . pl ");jgb_xts <- as. xts ( read. zoo( read.csv( "jgb_seireki.csv")));don't forget to remove jgbcm_all.csv!data comes as "S49.10.3, 10.388,9.378,8.839,8.520,8.354,8.298,8.244,8.120,8.203"- 1st field is
date . S49.10.3 must be converted to 1974-10-3. - H for
heisei , S forshowa . other fields contain interest data.must do zero padding.
$file="
$outfile="
$line_num = 0;
$gengou =
@buff = split
@data = split(/\./, $buff
$data
$year =
# print OUT "$gengou";
$year=$year+1925; # add 1925 to adjust as
}
$year=$year+1988; # adjust for
}
$interest
}
$output = $interest
}
# print OUT "$year-$data[1]-$data[2],$buff[2],$buff[9]\n";}
$line_num++;
}
0 件のコメント:
コメントを投稿