简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM