【发布时间】:2022-01-03 16:20:46
【问题描述】:
我需要使用两个数据框获取相关系数。
- 第一个数据帧
## ML
generate1 <- seq(ymd_h("2021-11-01-00"), ymd_h("2021-11-01-03"), by = "hours")
datex1 <- date(generate1)
generate2 <- seq(ymd_h("2021-11-02-00"), ymd_h("2021-11-02-03"), by = "hours")
datex2 <- date(generate2)
hourx <- hour(generate1)
method <- c(rep("ARIMA",8),rep("LSTM",8))
books <- c(390,154,154,153,352,170,229,124,458,224,196,485,492,235,139,116)
shirts <- c(312,397,119,357,464,444,453,155,484,454,282,288,141,262,148,258)
shoes <- c(306,274,480,330,143,190,213,477,141,323,316,473,269,149,333,145)
hats <- c(107,101,363,436,282,377,435,381,427,102,100,471,475,134,479,250)
data.predicted <- data.frame(datex = c(datex1,datex2,datex1,datex2),
hour = rep(hourx,4), method = method,
books, shirts, shoes, hats)
#data.predicted
# datex hour method books shirts shoes hats
#1 2021-11-01 0 ARIMA 390 312 306 107
#2 2021-11-01 1 ARIMA 154 397 274 101
#3 2021-11-01 2 ARIMA 154 119 480 363
#4 2021-11-01 3 ARIMA 153 357 330 436
#5 2021-11-02 0 ARIMA 352 464 143 282
#6 2021-11-02 1 ARIMA 170 444 190 377
#7 2021-11-02 2 ARIMA 229 453 213 435
#8 2021-11-02 3 ARIMA 124 155 477 381
#9 2021-11-01 0 LSTM 458 484 141 427
#10 2021-11-01 1 LSTM 224 454 323 102
#11 2021-11-01 2 LSTM 196 282 316 100
#12 2021-11-01 3 LSTM 485 288 473 471
#13 2021-11-02 0 LSTM 492 141 269 475
#14 2021-11-02 1 LSTM 235 262 149 134
#15 2021-11-02 2 LSTM 139 148 333 479
#16 2021-11-02 3 LSTM 116 258 145 250
- 第二个数据帧
## real
generate <- seq(ymd_h("2021-11-01-00"), ymd_h("2021-11-01-03"), by = "hours")
datex <- date(generate)
hourx <- hour(generate)
books <- c(220,120,150,114)
shirts <- c(319,400,130,360)
shoes <- c(300,280,300,330)
hats <- c(120,140,370,400)
data.real <- data.frame(datex, hourx, books, shirts, shoes, hats)
#data.real
# datex hourx books shirts shoes hats
#1 2021-11-01 0 220 319 300 120
#2 2021-11-01 1 120 400 280 140
#3 2021-11-01 2 150 130 300 370
#4 2021-11-01 3 114 360 330 400
我想得到像这个数据框这样的结果。相关性是基于真实数据,如果真实数据只有1天数据,则调整预测数据。
## Result
metrics <-c("books","books","shirts","shirts","shoes","shoes","hats","hats")
method <-c("ARIMA","LSTM","ARIMA","LSTM","ARIMA","LSTM","ARIMA","LSTM")
correlation <- c(0.946898292,0.294308358,0.999957355,0.535718183,
0.167424749,0.547561054,0.993560612,0.085661117)
result.cor <- data.frame(metrics, method, correlation)
#result.cor
# metrics method correlation
#1 books ARIMA 0.94689829
#2 books LSTM 0.29430836
#3 shirts ARIMA 0.99995736
#4 shirts LSTM 0.53571818
#5 shoes ARIMA 0.16742475
#6 shoes LSTM 0.54756105
#7 hats ARIMA 0.99356061
#8 hats LSTM 0.08566112
我们可以看到 ARIMA 的值为 0.94689829,它来自
ARIMA.pred <- subset(data.predicted, method == "ARIMA" & datex == "2021-11-01")
#ARIMA.pred
# datex hour method books shirts shoes hats
#1 2021-11-01 0 ARIMA 390 312 306 107
#2 2021-11-01 1 ARIMA 154 397 274 101
#3 2021-11-01 2 ARIMA 154 119 480 363
#4 2021-11-01 3 ARIMA 153 357 330 436
data.real$books
#220 120 150 114
cor(ARIMA.pred$books, data.real$books)
#0.9468983
我如何创建函数来简化并获得结果?。
【问题讨论】: