# tseriesパッケージを読み込む library(tseries) # 要素数 n <- 10 # rexp(n, rate): 指数分布に従うランダム数を生成する # n: 生成する乱数の数 # rate: 比率のベクトル r <- rexp(n, rate = 0.1) cat("r = ", r, "\n") # cumsum(): ベクトルの累積和 t <- cumsum(r) cat("t = ", t, "\n") # rnorm(n): 正規分布に従う乱数をn個生成する v <- rnorm(n) cat("v = ", v, "\n") # ベクトルtの各要素にPOSIXct("2017-04-28 00:00:00", tz="JST")を加える cat("POSIXct(2017-04-28 00:00:00, tz=Japan) = ", as.POSIXct("2017-04-28 00:00:00", tz="Japan"), "\n") t <- t + as.POSIXct("2017-04-28 00:00:00", tz="Japan") cat("t = ", t, "\n") # irts(time, value): Irregular time-series objectを生成する # time: 時間のベクトル, POSIXctクラスも使える # value: 値のベクトル x <- irts(t, v) cat("x = \n") print(x) # plot png('test0-plot.png') plot(x) dev.off() # xの範囲を00:00:00から00:05:00に拡大 lim = as.POSIXct(c("2017-04-28 00:00:00", "2017-04-28 00:05:00"), tz="Japan") cat("lim = ", lim, "\n") png('test0-plot1.png') plot(x, xlim=lim) dev.off()実行結果
$ Rscript test0.R r = 19.13138 28.15198 7.95869 5.508244 5.016306 26.64211 3.880155 0.1472298 0.8519172 13.92242 t = 19.13138 47.28335 55.24204 60.75029 65.76659 92.4087 96.28885 96.43608 97.288 111.2104 v = 1.670187 0.5241287 0.1358097 0.4557246 0.2969836 -1.170975 -0.09803701 1.608483 -1.257771 0.7104526 POSIXct(2017-04-28 00:00:00, tz=Japan) = 1493305200 t = 1493305219 1493305247 1493305255 1493305261 1493305266 1493305292 1493305296 1493305296 1493305297 1493305311 x = 2017-04-27 15:00:19 GMT 1.67 2017-04-27 15:00:47 GMT 0.5241 2017-04-27 15:00:55 GMT 0.1358 2017-04-27 15:01:00 GMT 0.4557 2017-04-27 15:01:05 GMT 0.297 2017-04-27 15:01:32 GMT -1.171 2017-04-27 15:01:36 GMT -0.09804 2017-04-27 15:01:36 GMT 1.608 2017-04-27 15:01:37 GMT -1.258 2017-04-27 15:01:51 GMT 0.7105 null device 1 lim = 1493305200 1493305500 null device 1test0-plot.png
test0-plot1.png
CSVファイルの読み書き
# tseriesパッケージを読み込む library(tseries) # 要素数 n <- 10 # rexp(n, rate): 指数分布に従うランダム数を生成する # n: 生成する乱数の数 # rate: 比率のベクトル r <- rexp(n, rate = 0.1) cat("r = ", r, "\n") # cumsum(): ベクトルの累積和 t <- cumsum(r) cat("t = ", t, "\n") # rnorm(n): 正規分布に従う乱数をn個生成する v <- rnorm(n) cat("v = ", v, "\n") # ベクトルtの各要素にPOSIXct("2017-04-28 00:00:00", tz="JST")を加える cat("POSIXct(2017-04-28 00:00:00, tz=Japan) = ", as.POSIXct("2017-04-28 00:00:00", tz="Japan"), "\n") t <- t + as.POSIXct("2017-04-28 00:00:00", tz="Japan") cat("t = ", t, "\n") # irts(time, value): Irregular time-series objectを生成する # time: 時間のベクトル, POSIXctクラスも使える # value: 値のベクトル x <- irts(t, v) cat("x = \n") print(x) # データを書き出す write.irts(x, file='test1.csv', sep=',') # 書き出したcsvデータを読み込む x1 <- read.table(file='test1.csv', sep=',', header=F, col.names=c("datetime", "value")) x1$datetime <- as.POSIXct(x1$datetime, tz="GMT") # 文字列をPOSIXct形式に変換 print(x1$datetime) print(x1$value) x1 <- irts(x1$datetime, x1$value) cat("x1 = \n") print(x1) # plotする lim = as.POSIXct(c("2017-04-28 00:00:00", "2017-04-28 00:05:00"), tz="Japan") cat("lim = ", lim, "\n") png('test1-plot.png') plot(x1, xlim=lim) dev.off()実行結果
$ Rscript test1.R r = 0.7161429 1.793284 0.2478615 2.984724 12.45114 0.01925959 3.400675 7.554745 1.237016 0.7123535 t = 0.7161429 2.509427 2.757288 5.742012 18.19315 18.21241 21.61309 29.16783 30.40485 31.1172 v = 0.6099315 0.8728334 0.4375745 0.07694671 -0.0349662 -0.9421087 -0.1829869 -1.074823 0.8262679 2.159355 POSIXct(2017-04-28 00:00:00, tz=Japan) = 1493305200 t = 1493305201 1493305203 1493305203 1493305206 1493305218 1493305218 1493305222 1493305229 1493305230 1493305231 x = 2017-04-27 15:00:00 GMT 0.6099 2017-04-27 15:00:02 GMT 0.8728 2017-04-27 15:00:02 GMT 0.4376 2017-04-27 15:00:05 GMT 0.07695 2017-04-27 15:00:18 GMT -0.03497 2017-04-27 15:00:18 GMT -0.9421 2017-04-27 15:00:21 GMT -0.183 2017-04-27 15:00:29 GMT -1.075 2017-04-27 15:00:30 GMT 0.8263 2017-04-27 15:00:31 GMT 2.159 [1] "2017-04-27 15:00:00 GMT" "2017-04-27 15:00:02 GMT" [3] "2017-04-27 15:00:02 GMT" "2017-04-27 15:00:05 GMT" [5] "2017-04-27 15:00:18 GMT" "2017-04-27 15:00:18 GMT" [7] "2017-04-27 15:00:21 GMT" "2017-04-27 15:00:29 GMT" [9] "2017-04-27 15:00:30 GMT" "2017-04-27 15:00:31 GMT" [1] 0.60990 0.87280 0.43760 0.07695 -0.03497 -0.94210 -0.18300 -1.07500 [9] 0.82630 2.15900 x1 = 2017-04-27 15:00:00 GMT 0.6099 2017-04-27 15:00:02 GMT 0.8728 2017-04-27 15:00:02 GMT 0.4376 2017-04-27 15:00:05 GMT 0.07695 2017-04-27 15:00:18 GMT -0.03497 2017-04-27 15:00:18 GMT -0.9421 2017-04-27 15:00:21 GMT -0.183 2017-04-27 15:00:29 GMT -1.075 2017-04-27 15:00:30 GMT 0.8263 2017-04-27 15:00:31 GMT 2.159 lim = 1493305200 1493305500 null device 1test1-plot.png
0 件のコメント:
コメントを投稿