我正在使用带有内置数据和示例(Nowcasting)的临近广播。我不知道如何找到2013-2016年期间的均方根预测误差。另外,如何找到Backcasting(是指预测过去一段时间内尚未发布的变量的值)。
这是我的代码:
library(nowcasting)
data(NYFED)
NYFED$legend$SeriesName
base <- NYFED$base
blocks <- NYFED$blocks$blocks
trans <- NYFED$legend$Transformation
frequency <- NYFED$legend$Frequency
delay <- ts(matrix(c(30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,60)))
data <- Bpanel(base = base, trans = trans, NA.replace = FALSE, na.prop = 1)
nowEM <- nowcast(formula = GDPC1~., data = data, r = 1, p = 1, method = "EM",
blocks = blocks, frequency = frequency)
nowcast.plot(nowEM, type = "factors")
windows()
nowcast.plot(nowEM, type = "fcst")
fcst_dates <- seq.Date(from = as.Date("2013-03-01"),to = as.Date("2017-12-01"),
by = "quarter")
fcst_results <- NULL
for(date in fcst_dates){
vintage <- PRTDB(data, delay = delay, vintage = date)
nowEM <- nowcast(formula = GDPC1~., data = vintage, r = 1, p = 1, method = "EM",
blocks = blocks, frequency = frequency)
fcst_results <- c(fcst_results,tail(nowEM$yfcst[,3],1))
}
我的尝试是
# First made the data frame for forecasted values which consist of actual value(y),in(predicted) and out ( beyond the sample periods)
df <- as.data.frame(nowEM$yfcst)
# Select period from 2013q1 to 2016q4
data1316 <- df[113:128,]
# extracted actual series y
y <- data1316$y
# change the column name
colnames(data1316)[2]<-"Predicted"
yhad <- data1316$Predicted
residuals = y - yhad
RMSE = sqrt(mean(residuals^2))
#Another way FE is the forcast error
FE = yhad - y
RMSE = sqrt(mean(FE^2))
请让我知道是否正确