codeedoc
codeedoc

Reputation: 494

Convert a matrix to an xts object

I am new to R and need to use the function getnfac from the PANICr package. And it seems that the function only takes an xts object as its first argument. However, after I went through some reading I still don't understand what an xts object is. Could anyone please tell me how I can convert a matrix into an xts object?

Below I use return matrix as the first argument. Therefore I just need to convert return to an xts object.

getnfac(return,143,"BIC3")
Error in getnfac(return, 143, "BIC3") : 
  x must be an xts object so lags and differences are taken properly

Upvotes: 1

Views: 4325

Answers (2)

AkselA
AkselA

Reputation: 8846

xts is an extensible time series object, essentially a regular ts object (or more correctly a zoo object) with some bits added.
The 'extensible' part of the name refers to how you can add attributes of your own choice.

While a matrix can be converted into a multivariate time series quite easily

m <- matrix(1:16, 4)
m.ts <- ts(m)
index(m.ts)

An xts requires its index (a vector describing at what time each sample was taken) to be in a date or time format

library(xts)
m <- matrix(1:16, 4)
d <- as.Date(1:nrow(m))
m.xts <- xts(m, order.by=d)
index(m.xts)

If your data is sampled at evenly spaced intervals a dummy index like the one above is probably ok. If not, you'll need to supply a vector corresponding to the sampling times.

Upvotes: 5

Lovetoken
Lovetoken

Reputation: 458

In my opinion, the first argument to the getnfac() function should be matrix containing the data.

In addition to the above answers, You can convert matrix format using coredata() about xts object.

Upvotes: 0

Related Questions