具有外生变量的VaR模型在SHILINE中不起作用 [英] VAR model with exogenous variable doesn't work in Shiny
本文介绍了具有外生变量的VaR模型在SHILINE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(shiny)
library(vars)
#--- Create Exogenous Variable 'periods'
data(Canada)
canTS <- Canada
periods <- as.matrix(data.frame(period=ifelse(index(canTS)>1996, 1, 0 ) ) )
#--- Fit the Model
fit1 <- VAR(Canada, p = 2, type = "none", exogen=periods)
coef(fit1)[[1]]
#-- Make Prediction
period2 <- as.matrix(data.frame(period = rep(1, 12)) ) # Future Exogen Values = 1
predict(fit1, n.ahead=12, dumvar=period2)
rm(list=ls(all=TRUE)) # remove objects
当使用带有滑块输入(指定预测范围)的闪亮应用程序时会出现问题。我们可以创建一个简单的闪亮应用程序,如下所示:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "nAhead",
label = "Forecast Period",
min = 1,
max = 12,
value = 6)
),
mainPanel(
verbatimTextOutput("Model")
)
)
)
server <- function(input, output) {
#--- Create Exogenous Variable 'periods'
data(Canada)
canTS <- Canada
periods <- as.matrix(data.frame(period=ifelse(index(canTS)>1996, 1, 0 ) ) )
#--- Fit the Model
fit1 <- VAR(Canada, p = 2, type = "none", exogen=periods)
#-- Make Prediction
output$Model <- renderPrint({
periods2 <- as.matrix(data.frame(period = rep(1, input$nAhead)) ) # forecasting window
predict(fit1, n.ahead=input$nAhead, dumvar=periods2)
})
}
shinyApp(ui, server)
我收到以下错误消息:"未找到对象句点"。我不明白为什么predict
函数不接受dumvar
提供的新数据矩阵。你知道该怎么做吗?谢谢。
推荐答案
行后
periods <- as.matrix(data.frame(period=ifelse(index(canTS)>1996, 1, 0 ) ) )
只需添加以下行
assign("periods",periods, envir = .GlobalEnv)
那么它起作用了
这篇关于具有外生变量的VaR模型在SHILINE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文