options(digits = 2)
set.seed(1)
n <- 10
x <- 1:n
y <- round(1:n + rnorm(n, mean = 1, sd = 2.5), 2)
e <- rnorm(n, mean = 0, sd = 1)
yhat <- yhat.out <- round(y + e, 2)
yhat.out[8] <- yhat.out[8] - 10 # 大外し
d <- data.frame(y, yhat, yhat.out)
library(DT)
datatable(d, colnames = c("観測値", "予測値", "予測値(大外し含む)"))
matplot(x = x, y = y, type = "n", ylim = range(c(y, yhat, yhat.out)))
matlines(x = x, y = y, type = "o", pch = 1, col = 1)
matlines(x = x, y = yhat, type = "o", pch = 2, col = 2)
matlines(x = x, y = yhat.out, type = "o", pch = 3, col = 3)
legend("topleft", pch = 1:3, col = 1:3,
legend = c("観測値", "予測値", "予測値(大外し含む)"))
get.accuracy <- function(yhat, y)
{
data.frame(MBE = mean(yhat - y),
MAE = mean(abs(yhat - y)),
MAPE = mean(abs((yhat - y) / y)) * 100,
RMSE = sqrt(mean((yhat - y)^2)))
}
(a <- get.accuracy(yhat, y))
## MBE MAE MAPE RMSE
## 1 0.25 0.83 46 1
(a.out <- get.accuracy(yhat.out, y)) # 大外し含む予測精度
## MBE MAE MAPE RMSE
## 1 -0.75 1.6 53 3
a.out / a
## MBE MAE MAPE RMSE
## 1 -3 2 1.2 2.9
a$MBE
## [1] 0.25
MBEが正の値と負の値が混在する為以下の状況が考えられる ・モデルが過大評価と過小評価を繰り返している ・バランスが取れていて平均値が小さい場合 ・データの分布に偏りがある