[R] FOMULATING TIME SERIES DATA FROM DATA FRAME
Joshua Ulrich
josh.m.ulrich at gmail.com
Sun Jul 17 13:39:03 CEST 2011
On Sat, Jul 16, 2011 at 9:35 PM, Peter Maclean <pmaclean2011 at yahoo.com> wrote:
> I am estimating Value at Risk using PerfomanceAnalytics package. The variables are stored in a data frame. I formated the data variables using zoo() and as.xtx() but it is not working. The working example is below.
> ##########################################################
> reguire(zoo)
> require(PerformanceAnalytics)
> reguire(xts)
>
> year<- c(1991-12-30, 1992-12-30, 1993-12-30, 1994-12-30)
> R1 <- c(12, 9, 8, 13)
> R2 <- c(5,2,9, 13)
> d <- data.frame(cbind(year, R1,R2))
>
Your year object has class numeric, not Date; and therefore the "-" is
being evaluated as subtraction, leaving you with:
year
# [1] 1949 1950 1951 1952
I suspect your real situation is this:
year <- c("1991-12-30", "1992-12-30", "1993-12-30", "1994-12-30")
R1 <- c(12, 9, 8, 13)
R2 <- c(5,2,9, 13)
d <- data.frame(year, R1,R2)
str(d)
# 'data.frame': 4 obs. of 3 variables:
# $ year: Factor w/ 4 levels "1991-12-30","1992-12-30",..: 1 2 3 4
# $ R1 : num 12 9 8 13
# $ R2 : num 5 2 9 13
Note that the year column still needs to be converted to a Date class.
d$year <- as.Date(d$year)
Now the xts constructor will work.
x <- xts(d[,-1], order.by=d$year, frequency=1)
> d<- zoo(d,year)
> VaR(d)
> #Error in checkData(R, method = "xts", ...)
>
> #When I use
> d <- as.xts(d,order.by =year, frequency = 1)
>
> #Error in xts(coredata(x), order.by = order.by, frequency = frequency, :
> # order.by requires an appropriate time-based object
>
>
> Peter Maclean
> Department of Economics
> UDSM
>
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
More information about the R-help
mailing list