我有一个包含25个变量的数据框df。
我想使用预测包为每个变量预测一天的价值。
df # original df
df2 <- df2 <- apply(df, 2, function (x) forecast(tbats(ts(x,frequency=365)), h=1))
$GOOG.Volume
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.142466 1654547 920711.4 2765229 650538 3544211
$GOOG.Adjusted
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.142466 530.7542 521.654 540.0065 516.8974 544.9667
$Avg_vol_10
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.142466 1889756 1761678 2027142 1697429 2103866
.....
> df2$GOOG.Volume
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
3.142466 1654547 920711.4 2765229 650538 3544211
> df2$GOOG.Volume$mean
Time Series:
Start = c(3, 53)
End = c(3, 53)
Frequency = 365
[1] 1654547
attr(,"biasadj")
[1] FALSE
如何将所有这1天的预测平均值添加到数据框df中。
谢谢
这是我当前的数据框,
sample df 我想在此数据框的末尾添加每列的第一个预测的平均值。
链接到数据框。 https://drive.google.com/open?id=1MkAHU-VcVpjQelsMOLaNoHPFwH2mbzig
最终df
col_a col_a col_a col_a col_a
all present values,
last row, we appended (rbind ) with forecasted means
predictions of all columns
pred_cola pred_colb pred_colc pred_cold
This is my requirement.
答案 0 :(得分:0)
这是您要做什么的一个最小示例:
# sample data, replace this with yours
df <- data.table(google=runif(n = 10, 10,100),
apple=runif(n=10, 10, 200),
shell=runif(n=10, 20, 400))
# apply forecast function
df2 <- apply(df, 2, function (x) forecast(tbats(ts(x,frequency=365)), h=1))
# get mean values in a separate vector
stock_cols <- colnames(df)
get_mean_values <- sapply(stock_cols, function(x) df2[[x]]$mean, USE.NAMES = F)
# convert mean values vector to data frame
get_mean_df <- t(data.frame(get_mean_values))
colnames(get_mean_df) <- stock_cols
row.names(get_mean_df) <- NULL
## rbind values in the original dataframe
df <- rbind(df, get_mean_df)
print(df)
google apple shell
1: 99.52935 170.18530 387.77822
2: 46.12997 41.67531 265.41992
3: 90.60817 155.16369 94.59444
4: 88.91726 108.59824 155.72855
5: 36.68363 61.81021 214.94687
6: 21.93327 75.20860 355.53876
7: 33.93198 143.96431 78.01367
8: 10.51548 122.39171 107.57541
9: 83.76416 99.18202 81.93566
10: 25.71955 73.91156 253.45181
11: 56.22397 106.50459 206.37342 ## this rows shows the mean value.