ARIMA Modelling - r

I am trying to fit an ARIMA model to my time series.
I am trying to get the best model for my time series as follows,
best.aic<-Inf
for(p in 0:6){
for(d in 0:6){
for(q in 0:6){
fit<-arima(nasdaq_ts,order=c(p,d,q))
fit.aic<- fit$aic
if (fit.aic < best.aic) {
best.aic<-fit.aic
best.fit<-fit
best.order<-c(p,d,q)
}
}
}
}
But I am getting an error which says as follows
Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, :
initial value in 'vmmin' is not finite
I cannot understand the above error or what is causing it?
Can someone please help me here
My time series looks as follows,

Are you familiar with the auto.arima function? It contains parameters that allow you to specify the model space to search, as well as the type of search to perform.

Try this code:-
nasdaqfinal.aic <- Inf
nasdaqfinal.order <- c(0,0,0)
for (p in 0:6) for (d in 0:6) for (q in 0:6) {
nasdaqcurrent.aic <- AIC(arima(data, order=c(p, d, q)))
if (nasdaqcurrent.aic < nasdaqfinal.aic) {
nasdaqfinal.aic <- nasdaqcurrent.aic
nasdaqfinal.order <- c(p, d, q)
nasdaqfinal.arima <- arima(data, order=nasdaqfinal.order)
}
}

Related

I try to run MLE, and get the Error in if (!all(lower[isfixed] <= fixed[isfixed] & fixed[isfixed] <= : missing value where TRUE/FALSE needed

I try to run MLE, and get the
Error in if (!all(lower[isfixed] <= fixed[isfixed] & fixed[isfixed] <= :
missing value where TRUE/FALSE needed constantly,
The negative likelihood function
likelihood.normal <- function (mu,sigma,y ){
pdf_yt <- dnorm(y, mu, sigma, log= FALSE)
-sum(log(pdf_yt))
}
The MLE command
library(stats4)
est.normal<-c(est$mean, est$sd)
bound.lower <-est.normal*0.5 # set the lower bound for the method "L-BFGS-B"
bound.upper <-est.normal*2.0 # set the upper bound for the method "L-BFGS-B"
est.mle<-mle(minuslogl =likelihood.normal, start= list(mu = est$mean, sigma = est$sd),method="L-BFGS-B",fixed = list(y= return.log), lower=bound.lower, upper= bound.upper)
If the fixed parameter is removed, the issue is gone. But I need the fixed parameter.
I too came across this error, and I believe it to be due to package dependency issues within the stats4::mle package.
When I submitted the project, I took a functional approach instead. Create a function that generates a function of the parameters with the data already preset as in the following:
logLikelihood.Norm.Function <- function (y) {
logLikelihood.ParamFunc <- function (mu, sigma) {
n <- length(y)
pdf_yt <- dnorm(y, mu, sigma, log = FALSE)
log.likelihood.value <- -sum(log(pdf_yt))
return(log.likelihood.value)
}
return(logLikelihood.ParamFunc)
}

Time Series Analysis in R Using ARIMA model on loop to get the optimal model

i've been working on this program to get the optimal ARIMA model for my time series data points.
I created a loop in which the different models can be evaluated by replacing the value of parameters on the arima() function with the for () loop value replacing the parameter with the value of the loop variable.
This is with the purpose to get the arima model with the minimum AIC or BIC. The model starts running fine, but when i leave it for a while, it displays this error, and i don't know what it means.
i'll leave the code and the error...
Thanks for your help, hope someone can help me! :)
minimo<-5000
prueba<-0
arima_1<-auto.arima(ts_m_rojo)
for (p in 0:3) {
for (d in 0:3) {
for (q in 0:3) {
for (P in 0:1) {
for (D in 0:1) {
for (Q in 0:1) {
for (l in 7:7) {
prueba<-arima_1$aic
if (minimo>prueba) {
minimo<-prueba
print(paste("ARIMA(",p,",",d,",",q,")",","," (",P,",",D,",",Q,")","[",l,"]"))
print(arima_1)
acf(arima_1$residuals, xaxp = c(0, 120, 4), lag.max=120, main = "")
pacf(arima_1$residuals, xaxp = c(0, 120, 4), lag.max=120, main = "")
}
arima_1 <- arima (ts_m_rojo, order=c(p,d,q) ,
seasonal= list(order=c (P,D, Q) ,period=l))
}
}
}
}
}
}
}
Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, :
non-finite finite-difference value [1]
In addition: Warning message:
In log(s2) : NaNs produced

How to set the limit to the parameters in optim function?

Excuse me. In optim function, how can I set the boundary for the par[1], par[2], par[3] under MLE?
I have tried the code below, it does not work and my method is not L-BFGS-B either.
get dataset
getSymbols("GOOG", from = "2008-06-30", to = "2018-06-30", src = "yahoo")
get the GOOG daily return
goog.daily <- abs(df.goog$daily.returns)
take absolute value
goog.daily <- abs(df.goog$daily.returns)
MOGPD Negative Likelihood
neg_lik <- function(par, data, u) {
xi <- par[1]
sigma <- par[2]
delta <- par[3]
llog <- rep(0, length(data))
for (i in 1:length(data)) {
if (data[i] <= u) { llog[i] <- 0 }
else {
llog[i] <- -log(delta) + ((1+xi)/xi) * log(1+xi*(data[i] - u)/sigma) +
log(sigma) + 2*log(1-(1-delta)*(1+xi*(data[i] - u)/sigma)^(-1/xi))
}
}
return(sum(llog))
}
estimate parameter MOGPD
optim <- optim(c(0.5, 1, 1), neg_lik,
lower=c(0, 0, 0), upper = c(100, 100, 100),
data = goog.daily, u = thresh)
tl;dr add method="L-BFGS-B", as follows:
opt_res <- optim(c(0.5,1,1),neg_lik,
lower=c(0,0,0),
upper=c(100,100,100),
method="L-BFGS-B",
data=goog.daily,u=thresh)
(it's not recommended to call your result "optim"; it will generally work but occasionally will cause lots of confusion)
If you want to impose constraints on the parameters, you have to use method="L-BFGS-B";
the lower and upper arguments only apply in this case. (There are R packages that provide other constrained optimization choices, e.g. nloptr.) From ?optim:
... includes an option for
box-constrained optimization ...
("an" in this case meaning "only one"; emphasis added), and
lower, upper: Bounds on the variables for the ‘"L-BFGS-B"’ method, or
bounds in which to search for method ‘"Brent"’.
(Brent's method is only for single-parameter optimization). This implies (although does not state it explicitly) that these arguments only work for method="L-BFGS-B".
Also, when you run your model with lower and/or upper set you get a warning:
Warning message:
In optim(...) :
bounds can only be used with method L-BFGS-B (or Brent)

Error in meanf - unused argument

Having problems with a function I wrote in R using the forecast package. This is the function:
generateARIMAForecasts <- function(inputTSDecompList, inputArimaOrder, fcstHrzn, cnst, drft){
tmpSTL <- NULL;
fcasting <- NULL;
tsfcastList <- NULL;
counter <- 1;
while(counter <= length(inputTSDecompList)){
#select the TS decompositions
tmpSTL <- inputTSDecompList[counter]$TimeSeriesDecomposition;
#add the lattice plot to the list of plots
if(cnst == TRUE & drft == TRUE){
fcasting <- forecast(tmpSTL, h=fcstHrzn,
forecastfunction=function(x,h,level, ...){
fit <- Arima(x, order=inputArimaOrder, include.constant = TRUE, include.drift = TRUE)
return(forecast(fit,h=fcstHrzn,level=level, ...))});
}
fcastCoefs <- fcasting$model$coef;
fcstValues <- fcasting;
fcastSummary <- summary(fcasting);
#add the forecast results to the forecast list
tsfcastList[[counter]] <- list(FinancialInstitution=LVTSFITimeSeriesList[counter]$LVTSFITimeSeriesList$FinancialInstitution,
ForecastCoefficients=fcastCoefs,
ForecastedSeries=fcstValues,
ForecastSummary=fcastSummary);
counter <- counter+1;
}
return(tsfcastList);
}
The function takes a list of STL decomposed series, and generates Arima forecasts for each of the individual stl decomposed time series in the input list.
I have run the forecast generation manually by hardcoding for individual elements and it works. However when I try to do it using the function I get the following error
Error in meanf(object, h = h, level = level, fan = fan, lambda = lambda, :
unused argument (forecastfunction = function (x, h, level, ...)
{
fit <- Arima(x, order = inputArimaOrder, include.constant = TRUE, include.drift = TRUE)
return(forecast(fit, h = fcstHrzn, level = level, ...))
})
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Could someone advise please?
Hi after a few more hours manually debugging each line in the RStudio console, I figured it out the issue was my call
tmpSTL <- inputTSDecompList[counter]$TimeSeriesDecomposition;
This returned NULL because I had created the inputTSDecompList as a 2-D list using
tsDecomList[[counter]] <- list(FinancialInstitution=inputTSList[counter]$LVTSFITimeSeriesList$FinancialInstitution, TimeSeriesDecomposition=tsDecom);
So I should have been calling
tmpSTL <- inputTSDecompList[[counter]]$TimeSeriesDecomposition;

Solving an integral in R gives error "The integral is probably divergent"

I am trying to solve an integral in R. However, I am getting an error when I am trying to solve for that integral.
The equation that I am trying to solve is as follows:
$$ C_m = \frac{{abs{x}}e^{2x}}{\pi^{1/2}}\int_0^t t^{-3/2}e^{-x^2/t-t}dt $$
The code that I am using is as follows:
a <- seq(from=-10, by=0.5,length=100)
## Create a function to compute integration
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
integrated <- integrate(integrand, lower=0, upper=upper)$value
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }
b<- sapply(a, Cfun, upper=1)
The error that I am getting is as follows:
Error in integrate(integrand, lower = 0, upper = upper) :
the integral is probably divergent
Does this mean I cannot solve the integral ?
Any possible ways to fix this problem will be highly appreciated.
Thanks.
You could wrap the call to Cfun in a try statement
# note using `lapply` so errors don't coerce the result to character
b <- lapply(a, function(x,...) try(Cfun(x, ...), silent = TRUE), upper = 1)
If you wanted to replace the errors with NA values and print a warning that the integration threw an error
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
int <- try(integrate(integrand, lower=0, upper=upper), silent = TRUE)
if(inherits(int ,'try-error')){
warning(as.vector(int))
integrated <- NA_real_
} else {
integrated <- int$value
}
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }
Edit (facepalm moment)
Your error arises because your function is not defined when t=0 (you divide by t within the integrand).
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
# deal with xx=0
if(isTRUE(all.equal(XX, 0)){
warning('The integrand is not defined at XX = 0')
return(NA_real_)
}
# deal with other integration errors
int <- try(integrate(integrand, lower=0, upper=upper), silent = TRUE)
if(inherits(int ,'try-error')){
warning(as.vector(int))
integrated <- NA_real_
} else {
integrated <- int$value
}
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }

Resources