Syntax error with tsoutliers package using Nile da

2019-08-25 06:41发布

问题:

I'm trying to locate outliers in a time series using the tsoutliers package.

I'm using the classic Nile dataset (which you can find here: https://vincentarelbundock.github.io/Rdatasets/datasets.html) and I'm unsucessfully getting the tso() function to work.

My code is:

nile.outliers <- tso(Nile,types = c("AO","LS","TC"))

However, I get this syntax error, or what I assume is a syntax error:

Error in tso0(x = y, xreg = xreg, cval = cval, delta = delta, n.start = n.start, : trying to get slot "y" from an object (class "data.frame") that is not an S4 object

If anyone can help me figure out this problem that would be amazing! Thanks!

回答1:

A quibble: that's not a syntax error (which would come from R), it's a run-time error from the tso0 function in the tsoutliers package.

But from your description of where to get the data, it looks as though you are passing in a data frame, when tso wants a "ts" object. You don't need to download the data, it's built in to R; so you remove the bad one you created, and use the built-in one:

rm(Nile)
nile.outliers <- tso(Nile,types = c("AO","LS","TC"))