Function

コメント･参照等
• 時系列データの基本統計量を求める関数

FUN = f_get_key_statistics_of_tsdata.r
function (df0, col_date = 1, col_data = 2, date_format = "%Y年%m月", round_digits = 2, diff_lag = 1, diff_diff = 1, boxtest_lag = 1, nullsymbol = " ")
{
df1 <- na.omit(df0[, c(col_date, col_data)])
df1[, 1] <- format(df1[, 1], date_format)
df <- f_add_lag_series(df0 = df1, col_date = 1, col_data = 2, lag = diff_lag, diff = diff_diff)
x <- df[, 2]
minlag <- df[which(min(df[, 3], na.rm = T) == df[, 3]), ]
maxlag <- df[which(max(df[, 3], na.rm = T) == df[, 3]), ]
statdf <- data.frame()
for (iii in seq(15)) {
statdf[iii, 1] <- iii
if (iii == 1) {
statdf[iii, 2] <- "名称"
statdf[iii, 3] <- colnames(df)[2]
statdf[iii, 4] <- nullsymbol
}
if (iii == 2) {
statdf[iii, 2] <- "期間"
statdf[iii, 3] <- paste0(range(df[, 1]), collapse = " ~ ")
statdf[iii, 4] <- nullsymbol
}
if (iii == 3) {
statdf[iii, 2] <- "直近"
statdf[iii, 3] <- tail(df, 1)[, 2]
statdf[iii, 4] <- tail(df, 1)[, 1]
}
if (iii == 4) {
statdf[iii, 2] <- "最高値"
statdf[iii, 3] <- max(x)
statdf[iii, 4] <- paste0(df[max(x) == df[, 2], 1], collapse = ",")
}
if (iii == 5) {
statdf[iii, 2] <- "最低値"
statdf[iii, 3] <- min(x)
statdf[iii, 4] <- paste0(df[min(x) == df[, 2], 1], collapse = ",")
}
if (iii == 6) {
statdf[iii, 2] <- "算術平均値"
statdf[iii, 3] <- round(mean(x), round_digits)
statdf[iii, 4] <- nullsymbol
}
if (iii == 7) {
statdf[iii, 2] <- "中央値"
statdf[iii, 3] <- median(x)
statdf[iii, 4] <- nullsymbol
}
if (iii == 8) {
statdf[iii, 2] <- "不偏分散"
statdf[iii, 3] <- round(var(x), round_digits)
statdf[iii, 4] <- nullsymbol
}
if (iii == 9) {
statdf[iii, 2] <- "不偏標準偏差"
statdf[iii, 3] <- round(sd(x), round_digits)
statdf[iii, 4] <- nullsymbol
}
if (iii == 10) {
statdf[iii, 3] <- round(adf.test(x = x)$p.value, round_digits) statdf[iii, 4] <- nullsymbol } if (iii == 11) { statdf[iii, 2] <- "一階差分系列ADF検定p値" statdf[iii, 3] <- round(adf.test(x = diff(x))$p.value, round_digits)
statdf[iii, 4] <- nullsymbol
}
if (iii == 12) {
statdf[iii, 2] <- paste0("最大上昇:lag=", diff_lag)
statdf[iii, 3] <- round(maxlag[, 3], round_digits)
statdf[iii, 4] <- paste0(maxlag[, 1], collapse = ",")
}
if (iii == 13) {
statdf[iii, 2] <- paste0("最大下落:lag=", diff_lag)
statdf[iii, 3] <- round(minlag[, 3], round_digits)
statdf[iii, 4] <- paste0(minlag[, 1], collapse = ",")
}
if (iii == 14) {
statdf[iii, 2] <- "原系列Ljung-Box検定p値"
statdf[iii, 3] <- Box.test(x, lag = boxtest_lag, type = "Ljung-Box")$p.value statdf[iii, 4] <- nullsymbol } if (iii == 15) { statdf[iii, 2] <- "一階差分系列Ljung-Box検定p値" statdf[iii, 3] <- Box.test(diff(x), lag = boxtest_lag, type = "Ljung-Box")$p.value
statdf[iii, 4] <- nullsymbol
}
}
colnames(statdf) <- c("No.", "項目", "結果等", "特記")
return(statdf)
}