Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?

4,202 views
Skip to first unread message

Prasanna

unread,
Jun 18, 2017, 11:49:43 AM6/18/17
to tsdyn
Hi,
I am unable to perform any type of analysis with univariate models, such as selectSETAR or even linear function in tsDyn. I get the error "Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?" even when I have not declared the vector as a time-series object. My work is on time series objects, but below I have generated price variables without declaring them to be time-series. Even then I get the same error message. Below are the codes, and error messages including the output from traceback(). What did I do wrong here? 

#generate price variables
NormalPrices = as.data.frame(matrix(rnorm(300*2, mean=50, sd=10), ncol=2))
colnames(NormalPrices) = paste("Price", 1:2, sep="")
mod1 = lm(Price1~Price2, data = NormalPrices)
ehat = residuals(mod1)

#apply the linear function to the residuals
a<-mapply(linear, m=1:15, MoreArgs = list(x=ehat), SIMPLIFY=FALSE)
sapply(a,AIC)
sapply(a,BIC)

Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?

#apply the selectSETAR function to the residuals
TAR.lags <- selectSETAR(ehat,include="const", common="include",               
                        model="TAR",  type="ADF", max.iter=500,
                        m=15, thDelay=1,    
                        nthresh=2, trim=0.15, criterion="AIC", plot=T)

Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?

> traceback()
8: stop(cnd)
7: .abort(text)
6: glubort(fmt_args(args), ..., .envir = .envir)
5: bad_args("x", "must be a vector, not a ts object, do you want `stats::lag()`?")
4: lag(x, lags[1])
3: embedd(x, lags = c((0:(m - 1)) * (-d), steps))
2: nlar.struct(x = x, m = m, d = d, steps = steps, series = series)
1: selectSETAR(ols.res22, include = "const", common = "include", 
       model = "TAR", type = "ADF", max.iter = 500, m = 15, thDelay = 1, 
       nthresh = 2, trim = 0.15, criterion = "AIC", plot = T)

Matthieu S

unread,
Jun 18, 2017, 2:53:48 PM6/18/17
to ts...@googlegroups.com
Hi

Your code actually works on my session. It could be a conflict due to other packages loaded? Which versions of R and tsDyn are you using?

Matthieu

--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tsdyn+unsubscribe@googlegroups.com.
To post to this group, send email to ts...@googlegroups.com.
Visit this group at https://groups.google.com/group/tsdyn.
For more options, visit https://groups.google.com/d/optout.

Alexander Haider

unread,
Mar 12, 2018, 1:02:43 PM3/12/18
to tsdyn
Hi,

I am not sure if the problem was solved, but I thought I will still answer it. I had the same problem. In my case the problem was that I used the dplyr package for data manipulation which masks the lag() function from stats and then you get the error message. A simple workaround (of course not the only one) would be not to load the dplyr function with library() and use dplyr::whatever_function instead in your code.

I hope that helps.
-Alex

Matthieu S

unread,
Mar 12, 2018, 8:20:50 PM3/12/18
to ts...@googlegroups.com
Hi Alex

Thanks for the feedback on this. I should indeed try to correct this in the package itself (I think I failed to import lag from stats).

In the meanwhile, I think you should be fine doing:
library(tidyverse)
lag <- stats::lag

Best,

Matthieu


--
Reply all
Reply to author
Forward
0 new messages