绘制 table 并为列中的最小值着色
Plotting a table and colouring minimum values in columns
我有如下数据框。
RMSE MAE MPE MAPE
ARIMA(1, 1, 0)(1, 1, 1)23 540.4890 362.6894 -1.2617083 8.273222
ARIMA(1, 0, 0)(1, 1, 1)23 550.7088 373.4725 -0.9303624 8.523295
ARIMA(1, 1, 0)(2, 1, 1)23 539.1094 360.4556 -1.2885436 8.220159
ARIMA(2, 0, 0)(1, 1, 1)23 547.4179 366.4258 -0.9816881 8.374072
ARIMA(1, 0, 0)(2, 1, 1)23 548.6216 372.1675 -0.9249135 8.494618
我想将其绘制为 table 并希望为每行中具有最小值的颜色着色。这样我就可以看到哪个模型在错误值方面给出了更好的结果。我找不到一个简单的方法来做到这一点。此 table 将通过 R.Net 上传到网站。我尝试了这个 example 但我什至没有通过第一部分。
# load Systematic Investor Toolbox
setInternet2(TRUE)
source(gzcon(url('https://github.com/systematicinvestor
数据:
structure(list(RMSE = c(540.489, 550.7088, 539.1094, 547.4179,
548.6216), MAE = c(362.6894, 373.4725, 360.4556, 366.4258, 372.1675
), MPE = c(-1.2617083, -0.9303624, -1.2885436, -0.9816881, -0.9249135
), MAPE = c(8.273222, 8.523295, 8.220159, 8.374072, 8.494618)), .Names =
c("RMSE",
"MAE", "MPE", "MAPE"), class = "data.frame", row.names = c("ARIMA(1,1,0)(1,1,1)23",
"ARIMA(1,0,0)(1,1,1)23", "ARIMA(1,1,0)(2,1,1)23", "ARIMA(2,0,0)(1,1,1)23",
"ARIMA(1,0,0)(2,1,1)23"))
让dat
成为你的数据框,你可以这样做:
## colour setting for each row / model
col_set <- 1:5
## do matrix plot; extend `xlim` to the right to reserve space for legend
matplot(y = t(dat), xaxt = "n", pch = 19, col = col_set,
xlab = "", ylab = "Error", xlim = c(1, ncol(dat)+2L))
axis(1L, seq_len(ncol(dat)), colnames(dat))
## add legend at top right corner
legend("topright", legend = rownames(dat), pch = 19, col = col_set)
## highlight the column minimum
points(seq_len(ncol(dat)), unlist(lapply(dat, min)), cex = 2, pch = 19,
col = col_set[unlist(lapply(dat, which.min))])
如您所见,在不重新缩放的情况下,图形显示效果很差。我们现在将所有列缩放到 [0, 1]
并制作绘图。
dat <- "rownames<-"(do.call(cbind.data.frame, lapply(dat, function (u) (u - min(u)) / diff(range(u)))),
rownames(dat))
# RMSE MAE MPE MAPE
#ARIMA(1,1,0)(1,1,1)23 0.1189372 0.1716077 0.07379835 0.1750468
#ARIMA(1,0,0)(1,1,1)23 1.0000000 1.0000000 0.98501527 1.0000000
#ARIMA(1,1,0)(2,1,1)23 0.0000000 0.0000000 0.00000000 0.0000000
#ARIMA(2,0,0)(1,1,1)23 0.7162870 0.4586499 0.84386716 0.5077358
#ARIMA(1,0,0)(2,1,1)23 0.8200597 0.8997457 1.00000000 0.9053989
然后使用前面的代码,我们有
我有如下数据框。
RMSE MAE MPE MAPE
ARIMA(1, 1, 0)(1, 1, 1)23 540.4890 362.6894 -1.2617083 8.273222
ARIMA(1, 0, 0)(1, 1, 1)23 550.7088 373.4725 -0.9303624 8.523295
ARIMA(1, 1, 0)(2, 1, 1)23 539.1094 360.4556 -1.2885436 8.220159
ARIMA(2, 0, 0)(1, 1, 1)23 547.4179 366.4258 -0.9816881 8.374072
ARIMA(1, 0, 0)(2, 1, 1)23 548.6216 372.1675 -0.9249135 8.494618
我想将其绘制为 table 并希望为每行中具有最小值的颜色着色。这样我就可以看到哪个模型在错误值方面给出了更好的结果。我找不到一个简单的方法来做到这一点。此 table 将通过 R.Net 上传到网站。我尝试了这个 example 但我什至没有通过第一部分。
# load Systematic Investor Toolbox
setInternet2(TRUE)
source(gzcon(url('https://github.com/systematicinvestor
数据:
structure(list(RMSE = c(540.489, 550.7088, 539.1094, 547.4179,
548.6216), MAE = c(362.6894, 373.4725, 360.4556, 366.4258, 372.1675
), MPE = c(-1.2617083, -0.9303624, -1.2885436, -0.9816881, -0.9249135
), MAPE = c(8.273222, 8.523295, 8.220159, 8.374072, 8.494618)), .Names =
c("RMSE",
"MAE", "MPE", "MAPE"), class = "data.frame", row.names = c("ARIMA(1,1,0)(1,1,1)23",
"ARIMA(1,0,0)(1,1,1)23", "ARIMA(1,1,0)(2,1,1)23", "ARIMA(2,0,0)(1,1,1)23",
"ARIMA(1,0,0)(2,1,1)23"))
让dat
成为你的数据框,你可以这样做:
## colour setting for each row / model
col_set <- 1:5
## do matrix plot; extend `xlim` to the right to reserve space for legend
matplot(y = t(dat), xaxt = "n", pch = 19, col = col_set,
xlab = "", ylab = "Error", xlim = c(1, ncol(dat)+2L))
axis(1L, seq_len(ncol(dat)), colnames(dat))
## add legend at top right corner
legend("topright", legend = rownames(dat), pch = 19, col = col_set)
## highlight the column minimum
points(seq_len(ncol(dat)), unlist(lapply(dat, min)), cex = 2, pch = 19,
col = col_set[unlist(lapply(dat, which.min))])
如您所见,在不重新缩放的情况下,图形显示效果很差。我们现在将所有列缩放到 [0, 1]
并制作绘图。
dat <- "rownames<-"(do.call(cbind.data.frame, lapply(dat, function (u) (u - min(u)) / diff(range(u)))),
rownames(dat))
# RMSE MAE MPE MAPE
#ARIMA(1,1,0)(1,1,1)23 0.1189372 0.1716077 0.07379835 0.1750468
#ARIMA(1,0,0)(1,1,1)23 1.0000000 1.0000000 0.98501527 1.0000000
#ARIMA(1,1,0)(2,1,1)23 0.0000000 0.0000000 0.00000000 0.0000000
#ARIMA(2,0,0)(1,1,1)23 0.7162870 0.4586499 0.84386716 0.5077358
#ARIMA(1,0,0)(2,1,1)23 0.8200597 0.8997457 1.00000000 0.9053989
然后使用前面的代码,我们有