闪亮的实施

时间:2018-02-16 20:45:07

标签: shiny lapply

您好我的问题是关于用户与我的代码的交互性,我使用伟大的预测混合包开发了这个简单的时间序列循环,这里是rmd for it

奇怪的语言是葡萄牙语抱歉,我懒得翻译整个事情,无论如何都应该没关系。

```{r Primeira Vez, clique no play, include=FALSE, include=FALSE}
setwd("~/R")
install.packages("forecastHybrid")
setwd("~/R")
library(forecastHybrid)
```




 ```{r Inputs}
# A dataset variable in the global enviroment
Data<- SemZero 

#How to save a witout dummies regression
ComoSalvar<- "Exemplo2.csv"

#How to save a with dummies regression
ComoSalvarReg<- "ExemploparaDummies.csv"

#Where to save them.
OndeSalvar <- "~/R"

#Month,Year, and Day variable for the ts
Mes<- 9
Ano<- 2012
Dia<- 1

#Frequency
Freq<- 12

#Forecast Period.
Forecast = 12

#Confidance intervals
IC<- c(0)


#Variables in the Data dataset that will be used for the lapply regression, usually I would read an excel file with headers.
VStart = 1
VFinish = 2

#Simple regressor dataset, they can be matrix as well as line since de data.fram combines everyone example data.frame(OddMonths,Christmas,WasTrumpPresident?,RainInThatSeason)
Regressores<- data.frame(0)
```


    ```{r Logic for rolling test and data simplification}
setwd(OndeSalvar)

if(IC[1] > 0) pi<-TRUE else
  pi<- FALSE


if (nrow(Data) >= Freq*3 + Forecast*2) {
  Weights <- "cv.error"   
  Multiplicador<-floor((nrow(Data)-Forecast*2)/Freq) } else 
  if (nrow(Data) <= Freq*3 + Forecast*2) {
    Weights <- "cv.error"   
    Multiplicador<-3 } else 
    if (nrow(Data) >= Freq*2.5 + Forecast*2){
      Weights <-  "cv.error"
      Multiplicador = 2.5} else
        Weights <- "equal"



```


```{r The bunk of the regression process}
  if (nrow(Regressores) < nrow(Data)) {
  my_forecast1 <- try({function(x){
    print(x)
    print(summary(x))
    names(x)
    x[is.na(x)] <- 0; x
    if(sum(abs(x)) < Freq){
      Model<- "aenst"} else
        if(mean(x[1:Freq]) == 0)Model<- "aenst" else Model<- "aenstf"
  x<- ts(x, start = c(Ano, Mes,Dia), frequency = Freq)
  hm<-hybridModel(x, models = Model, lambda = NULL, 
  a.args = list(trace = FALSE,test = "kpss", ic ="aicc", max.P = 2, max.p = 9,max.q=9,max.Q = 2,max.d = 2,max.D = 2,start.p = 9,start.P = 2,start.Q = 2,start.q = 9,allowdrift = TRUE,allowmean = TRUE

  #Se tiver tempo apague o # abaixo para uma maior qualidade no modelo arima. 
                           #,stepwise = FALSE,parallel = TRUE,num.cores = NULL 
  ),

  e.args = list(ic = "aicc"),

  n.args = list(repeats = nrow(Data)),

  s.args = NULL,

  t.args = NULL,

  weights = Weights,
  errorMethod = "RMSE",cvHorizon = Forecast,windowSize = frequency(x)*Multiplicador, horizonAverage = FALSE,
  verbose = TRUE)

lapply(seq_along(x), function(i) paste(names(x)[[i]], x[[i]]))
  fcast1<- forecast(hm,h = Forecast,level = IC,PI = pi)
  return(fcast1)

  }})
Listas<- lapply(Data[,VStart:VFinish], try(my_forecast1))

if (pi == FALSE) ListaResultado<- as.data.frame(lapply(Listas, '[[', 'mean')) else
  ListaResultado <- Listas

write.csv(ListaResultado , file = ComoSalvar)

} else {
  my_forecastreg <- function(x){
    print(x)
    print(summary(x))
    names(x)
    x[is.na(x)] <- 0; x
    x<- ts(x, start = c(Ano, Mes,Dia), frequency = Freq)
    hmreg <- hybridModel(x ,models = "ans",
                         a.args = list(xreg = Regressores[1:nrow(Data),],trace = TRUE,test = "kpss", ic ="aicc", max.P = 2, max.p = 9,max.q=9,max.Q = 2,max.d = 2,max.D = 2,start.p = 9,start.P = 2,start.Q = 2,start.q = 9,allowdrift = TRUE,allowmean = TRUE
                                       #Se tiver tempo apague o # abaixo para uma maior qualidade no modelo arima. 
                                       #,stepwise = FALSE,parallel = TRUE,num.cores = NULL            

                         ),
                         n.args = list(xreg = Regressores[1:nrow(Data),], repeats= nrow(Regressores)),
                         s.args = list(xreg = Regressores[1:nrow(Data),], method = "arima"))


    fcast2<- forecast(hmreg,h = Forecast,level = IC,PI = pi, xreg = Regressores[nrow(Data):(nrow(Data)+Forecast-1),])

    return(fcast2)

  }

  Listas2<- lapply(Data[,VStart:VFinish], try(my_forecastreg))

  if (pi == FALSE) ListaResultado2<- as.data.frame(lapply(Listas2, '[[', 'mean')) else
    ListaResultado2 <- Listas2

  write.csv(ListaResultado2 , file = ComoSalvarReg)
}
```

我想开发一些东西来获取用户输入并运行回归,我的最终用户通常不喜欢R Markdown文件看起来多么丑陋,我正在研究使用闪亮,但我不知道一些细节

如果我将整个内容成功上传到闪亮,谁会运行此回归?我的电脑,服务器,用户,我都不知道?

用户输入是否可以进入用户自己的全局环境,以便整个过程可以保持严格的离线过程(使用Shiny作为替代此输入块的美化应用程序?)

有人可以举一个类似的Shiny应用程序的例子吗?

用户可以将.xlsm读入闪亮的服务器,还是使用他的全局环境为闪亮的应用程序定义数据以用作输入?

小偷包也可以在这个lapply函数上实现,作为一种提高预测质量的方法,我当然会从模型中删除stlm和theta选项,因为它们在各种模拟中表现得相当差用玩具套装执行,stlm在交叉验证时发生了一些观察,并且theta模型不起作用。

有人可以教我如何在函数内部错误忽略变量并继续将函数应用于下一个变量吗?或者将模型更改为问题较少的问题,我的解决方案是尝试捕获模型崩溃并在发生之前丢弃theta模型的这些情况,但这只是对潜在问题的丑陋黑客攻击。 此外,如果您在代码中看到一些丑陋的东西,请随意批评。

0 个答案:

没有答案