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;
Related
I'm working on an assignment with the following prompt:
1b) Create a function which will take a vector of numeric values, and an integer n as arguments. The function will perform nonparametric bootstrapping on this data, n times, calculating the most-probable value of the data set. The function should return the output of the boot command.
This is the function I have written:
non.p.bootstrap <- function(vector, n) {
mode.stat <- function(my.data,i) return(my.mode(my.data[i]))
b <- boot(data = vector, statistic = mode.stat, R = n)
return(b)
}
The function my.mode is one I have written which outputs the mode of a vector of continuous data.
Edit: Here is the my mode function I created as well
my.mode <- function(vector) {
hist <-hist(vector, breaks = length(vector), plot = FALSE)
densest <- max(hist$density)
mode <- hist$mid[hist$density == densest]
return(mode)
}
However, when I try to run my bootstrapping function on a vector of 1000 data points, I get an error message.
I have a list of model objects called allAR1. For each model object, I need to use the tsdiag function to produce the diagnostics plot and then save that plot to a folder.
I am trying to use a combination of jpeg(), lapply and dev.off() to apply tsdiag to each model and then save the resulting plot as an image file. The problem is that this only seems to save the diagnostic plot for the first model in the allAR1 list, whereas I would like to save the diagnostic plots for all models in allAR1.
Here is my code and a reproducible example:
library(tseries)
data(nino)
nino = list(nino3 = nino3, nino4 = nino3.4)
ar <- function(dat, idx, order, m) {
paes = arima(dat, order = order)
bic = paes$loglik + m*log(length(dat))
res = residuals(paes)
all = list(paes = paes,
bic = bic,
res = res)
assign(idx, all)
return(all)
}
allAR1 = mapply(ar, dat = nino, idx = names(nino),
MoreArgs = list(order = c(1,0,0), m = 1),
SIMPLIFY = F)
allpaes = lapply(allpaes, function(x) x$paes)
jpeg(sprintf("C:/Users/owner/Documents/%s.jpeg", names(nino)))
lapply(allAR1, tsdiag, gof.lag = 1000)
dev.off()
I have also tried lapply(allAR1, function(x) {jpeg(sprintf("C:/Users/owner/Documents/%s.jpeg", names(nino))); tsdiag(x$paes, 1000); dev.off()}). However, this gives me the same result as the code above.
Any help would be greatly appreciated as I am not sure where I am going wrong.
Here's a code snippet to get you started:
library(tseries)
#from tsdiag help page
fit <- arima(lh, c(1,0,0))
#make an arbitrary list of model fits
models <- list(m1 = fit, m2 = fit)
lapply(1:length(models), function(x){
jpeg(paste0(names(models)[x], ".jpeg"))
tsdiag(models[[x]])
dev.off()
})
I wrote a for loop to test different settings for an ordination function in R (package "vegan", called by "phyloseq"). I have several subsets of my data within a list (sample_subset_list) and therefore, testing different parameters for all these subsets results in many combinations.
The ordination function contains the optional argument formula and I would like to perform my ordinations with and without a formula. I assume NULL would be the correct way to not use the formula parameter? But how do I pass NULL when using a for loop (or apply etc)?
Using the phyloseq example data:
library(phyloseq)
data(GlobalPatterns)
ps <- GlobalPatterns
ps1 <- filter_taxa(ps, function (x) {sum(x > 0) > 10}, prune = TRUE)
ps2 <- filter_taxa(ps, function (x) {sum(x > 0) > 20}, prune = TRUE)
sample_subset_list <- list()
sample_subset_list <- c(ps1, ps2)
I tried:
formula <- c("~ SampleType", NULL)
> formula
[1] "~ SampleType"
ordination_list <- list()
for (current_formula in formula) {
tmp <- lapply(sample_subset_list,
ordinate,
method = "CCA",
formula = as.formula(current_formula))
ordination_list[[paste(current_formula)]] <- tmp
}
this way, formula only consists of "~ SampleType". If I put NULL into ticks, it gets wrongly interpreted as formula:
formula <- c("~ SampleType", "NULL")
Error in parse(text = x, keep.source = FALSE)
What is right way to solve this?
Regarding Lyzander's answer:
# make sure to use (as suggested)
formula <- list("~ SampleType", NULL)
# and not
formula <- list()
formula <- c("~ SampleType", NULL)
You can use a list instead:
formula <- list("~ my_constraint", NULL)
# for (i in formula) print(i)
#[1] "~ my_constraint"
#NULL
If your function takes NULL as an argument for a function you should also do:
ordination_list <- list()
for (current_formula in formula) {
tmp <- lapply(sample_subset_list,
ordinate,
method = "CCA",
formula = if (is.null(current_formula)) NULL else as.formula(current_formula))
ordination_list[[length(ordination_list) + 1]] <- tmp
}
This error looks common but I've can't seem to get my head round this.
I've been given the following code (on a course but it's (the code) not graded) as a shortcut to doing LDA. Apparently it works on some computers but not mine. I've upgraded R and R studio and also the MASS library. Any ideas?
The error I get is:
Error in eval(expr, envir, enclos) : object 'training' not found
The code is
lda.valid <- function(formula,data,...,train.fraction=0.75){
grouping <- model.response(model.frame(formula,data))
tbl <- table(grouping,lda(formula,data,...,CV=TRUE)$class)
CV <- sum(diag(tbl))/sum(tbl)
n <- nrow(data)
training <- sample(1:n,n*train.fraction)
lda.training <- lda(formula,data,...,subset=training)
lda.pred <- predict(lda.training,data[-training,])
tbl <- table(grouping[-training],lda.pred$class)
VAL <- sum(diag(tbl))/sum(tbl)
c(CV=CV,VAL=VAL)
}
I run the following and get the error. Is it related to the "..." (ellipsis)
lda.valid(Species~.,data=iris,prior=c(1/3,1/3,1/3),train.fraction=0.5)
I was looking at the trycatch stuff to catch the error but don't see how I can print the stacktrace.
Any hints or suggestions. I probably don't understand the stacktrace at this point.
The error occurs where you call lda.training <- lda(...). This seems to be related to internals of the lda() function, and it's not clear to me why this happens.
However, the intent of this code seems to perform the lda using a only a training subset of the data.
This is easy enough to specify directly by subsetting the data in advance. So I suggest replacing the offending line with
lda.training <- lda(formula, data[training, ], ...)
Thus the complete function is:
library(MASS)
lda.valid <- function(formula, data, ..., train.fraction = 0.75){
grouping <- model.response(model.frame(formula, data))
tbl <- table(grouping, lda(formula, data, ..., CV = TRUE)$class)
CV <- sum(diag(tbl))/sum(tbl)
n <- nrow(data)
training <- sample(1:n, n*train.fraction)
lda.training <- lda(formula, data[training, ], ...) # <<<--- Changed
lda.pred <- predict(lda.training, data[-training, ])
tbl <- table(grouping[-training], lda.pred$class)
VAL <- sum(diag(tbl))/sum(tbl)
c(CV = CV, VAL = VAL)
}
lda.valid(Species~., data = iris, prior = c(1/3, 1/3, 1/3), train.fraction = 0.5)
This results in:
> lda.valid(Species~., data = iris, prior = c(1/3, 1/3, 1/3), train.fraction = 0.5)
CV VAL
0.98 0.96
I'm trying to make a lot of time series forecast using the HoltWinters function in R.
For this purpose, I use a for loop and inside I call to the function, and save the prediction in a data.frame.
The problem is that some results of the HoltWinters function gives errors, specifically optimization errors:
Error en HoltWinters(TS[[i]]) : optimization failure
This error break the loop.
So what I need is something like "try": if it can make the HoltWinters function, it saves the prediction, otherwise it save the error.
The code below replicates the problem:
data <- list()
data[[1]] <- rnorm(36)
data[[2]] <-
c(
24,24,28,24,28,22,18,20,19,22,28,28,28,26,24,
20,24,20,18,17,21,21,21,28,26,32,26,22,20,20,
20,22,24,24,20,26
)
data[[3]] <- rnorm(36)
TS <- list()
Outputs <- list()
for (i in 1:3) {
TS[[i]] <- ts(data[[i]], start = 1, frequency = 12)
Function <- HoltWinters(TS[[i]])
TSpredict <- predict(Function, n.ahead = 1)[1]
Outputs[[i]] <-
data.frame(LastReal = TS[[i]][length(TS[[i]])], Forecast = TSpredict)
}
Where i <- 2 The problem is generated.
What I need is that in this example the "Outputs" list is as follows:
> Outputs
[[1]]
LastReal Forecast
1 0.5657129 -2.274507
[[2]]
LastReal Forecast
1 error error
[[3]]
LastReal Forecast
1 0.4039783 -0.9556881
Thanks in advance.
I ran into this same problem with HoltWinters the other day and took Roman's advice by using tryCatch. It's not the most intuitive to implement based on the documentation, but I found this link very helpful for understanding it: How to write trycatch in R
My solution built off of the sample there.
data <- list()
data[[1]] <- rnorm(36)
data[[2]] <- c(
24,24,28,24,28,22,18,20,19,22,28,28,
28,26,24,20,24,20,18,17,21,21,21,28,
26,32,26,22,20,20,20,22,24,24,20,26
)
data[[3]] <- rnorm(36)
TS <- list()
Outputs <- list()
result <- list()
for (i in 1:3) {
Outputs[[i]] <- tryCatch({
#You can enter messages to see where the loop is
#message(paste("Computing", i))
TS[[i]] <- ts(data[[i]], start = 1, frequency = 12)
Function <- HoltWinters(TS[[i]])
TSpredict <- predict(Function, n.ahead = 1)[1]
result[[i]] <-
data.frame(LastReal = TS[[i]][length(TS[[i]])], Forecast = TSpredict)
},
error = function(cond) {
#message(paste("ERROR: Cannot process for time series:", i))
msg <- data.frame(LastReal = "error", Forecast = "error")
return(msg)
})
}
And for the Outputs
> Outputs
[[1]]
LastReal Forecast
1 0.4733632 0.5469373
[[2]]
LastReal Forecast
1 error error
[[3]]
LastReal Forecast
1 0.8984626 -0.5168826
You can use other error handling parameters such as finally and warning to deal with other exceptions that may arise.