Question

I am trying to use times by taking reference for na.approx in zoo from the following reference link:

http://www.oga-lab.net/RGM2/func.php?rd_id=zoo:na.approx

here's my code:

library(tseries)
library(xts)
library(quantmod)
library(ggplot2)
x = read.table("test.dat", header = FALSE, sep="\t", skip=0)

dt<-sprintf("%s %04d",x$A2,x$A4)
dt<-as.POSIXlt(dt,format="%Y-%m-%d %H%M")

y <- data.frame(dt,x$A5)
colnames(y) <- c("date","price")
z <- xts(y[,2],y[,1])
core <- to.minutes(z, OHLC=TRUE, drop.time=FALSE)
colnames(core) <- c("Open","High","Low","Close")
tseq <- seq(start(core),end(core), by = times("00:01:00"))
core <- na.approx(core, xout = tseq)

this is the error message:

> tseq <- seq(start(core),end(core), by = times("00:01:00"))
Error in seq.POSIXt(start(core), end(core), by = times("00:01:00")) : 
  could not find function "times"

how can I fix that?

If I replace it by 'time', then tseq gets incremented by the second rather than by the minute. Why?

below is the data file:

M11 2011-03-10  0   104 365 T   N   N   1
M11 2011-03-10  0   113 365 T   N   N   1
M11 2011-03-10  0   113 365 T   N   N   2
M11 2011-03-10  0   113 365 T   N   N   1
M11 2011-03-10  0   113 365 T   N   N   1
M11 2011-03-10  0   114 360 T   N   N   1
M11 2011-03-10  0   114 360 T   N   N   10
M11 2011-03-10  0   114 360 T   N   N   4
M11 2011-03-10  0   114 360 T   N   N   20
M11 2011-03-10  0   114 360 T   N   N   10
M11 2011-03-10  0   114 360 T   N   N   5
M11 2011-03-10  0   114 360 T   N   N   1
M11 2011-03-10  0   114 360 T   N   N   4
M11 2011-03-10  0   114 360 T   N   N   2
M11 2011-03-10  0   115 355 T   N   N   8
M11 2011-03-10  0   115 355 T   N   N   12
M11 2011-03-10  0   115 355 T   N   N   4
M11 2011-03-10  0   115 355 T   N   N   12
M11 2011-03-10  0   115 355 T   N   N   5
M11 2011-03-10  0   115 355 T   N   N   9
M11 2011-03-10  0   115 355 T   N   N   1
M11 2011-03-10  0   115 355 T   N   N   3
M11 2011-03-10  0   115 355 T   N   N   1
M11 2011-03-10  0   115 355 T   N   N   1
M11 2011-03-10  0   115 355 T   N   N   1
M11 2011-03-10  0   115 350 T   N   N   1
M11 2011-03-10  0   115 350 T   N   N   1
M11 2011-03-10  0   115 345 T   N   N   2
M11 2011-03-10  0   115 345 T   N   N   2
M11 2011-03-10  0   118 345 T   N   N   1
M11 2011-03-10  0   118 345 T   N   N   1
M11 2011-03-10  0   118 345 T   N   N   3
M11 2011-03-10  0   118 345 T   N   N   2
M11 2011-03-10  0   119 345 T   N   N   1
M11 2011-03-10  0   119 345 T   N   N   1
M11 2011-03-10  0   120 345 T   N   N   2
M11 2011-03-10  0   122 350 T   N   N   1
M11 2011-03-10  0   124 355 T   N   N   1
M11 2011-03-10  0   126 355 T   N   N   1

EDIt: I edited my code to what G.Grothendieck posted and so am running the below:

DF <- read.table(text = Lines)
tt <- as.POSIXct(sprintf("%s %04d", DF[[2]], DF[[4]]), format = "%Y-%m-%d %H%M")
x <- xts(DF[[5]], tt)
xm <- to.minutes(x)
tseq <- seq(start(xm), end(xm), by = 60)
xm.x <- na.approx(xm, xout = tseq)

But then, when I run the following code, I get the below error:

> apply.daily(xm.x,str(xm.x))
An ‘xts’ object from 2011-03-10 to 2011-06-08 containing:
  Data: num [1:129541, 1:4] 350 350 350 350 350 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "x.Open" "x.High" "x.Low" "x.Close"
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
 NULL
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'FUN' of mode 'function' was not found

Where is my error:?

EDIT: replaced str with a statistical function and running into errors:

testFun <- function(d) {
    d <- data.frame(d, rt=seq(1-nrow(d),0));
    statfit <- lm(Close ~ poly(rt,4),d)
    smry<-summary(statfit)
    r_sq<-smry$r.squared
    s <- coef(statfit)
    ctr<-'X'
    nrows<-nrow(d)
    price<-d$Close[nrows]
    sroot <- polyroot(c(s[2],2*s[3],3*s[4],4*s[5]))
    sfun <- function(x) {  ret = 2*s[3]+6*s[4]*x+12*s[5]*x*x;ret;}   
    ret_val <- data.frame(ctr,r_sq,price)
    ret_val <- as. matrix(ret_val)
    ret_val
}
        debug(testFun)

    wfun <- function(w) {

  tr <- try(rollapply(w,width=20,FUN=testFun,by.column=FALSE, align="right"), silent = TRUE)
}                               



 #      is.weekend <- function(x) {
   #    w <- as.POSIXct(x)
    #   w %in% c(0,6)
    #}
    #apply.daily(xm[!is.weekend(index(xm))],wfun)
apply.daily(xm,wfun)

debug: d <- data.frame(d, rt = seq(1 - nrow(d), 0))
debug: statfit <- lm(Close ~ poly(rt, 4), d)
debug: smry <- summary(statfit)
debug: r_sq <- smry$r.squared
debug: s <- coef(statfit)
debug: ctr <- "X"
debug: nrows <- nrow(d)
debug: price <- d$Close[nrows]
debug: sroot <- polyroot(c(s[2], 2 * s[3], 3 * s[4], 4 * s[5]))
debug: sfun <- function(x) {
    ret = 2 * p[3] + 6 * p[4] * x + 12 * p[5] * x * x
    ret
}
debug: ret_val <- data.frame(ctr, r_sq, price)
debug: ret_val <- as.matrix(ret_val)
debug: ret_val
exiting from: FUN(data[posns, ], ...)
Error in coredata.xts(x) : currently unsupported data type
Calls: print ... coredata.xts -> structure -> coredata -> coredata.xts -> .Call
Execution halted

EDIT: I realize my error was because my function testFun returned a zoo object and apply.daily requires an xts object.

tr <- try(rollapply(w,width=20,FUN=testFun,by.column=FALSE, align="right"), silent = TRUE)
tr
str(tr)

2011-04-07 14:59:00 2011-04-07 15:00:00 2011-04-07 15:01:00 2011-04-07 15:02:00
               TRUE                TRUE                TRUE                TRUE
2011-04-07 15:03:00 2011-04-07 15:04:00 2011-04-07 15:05:00 2011-04-07 15:06:00
               TRUE                TRUE                TRUE                TRUE
2011-04-07 15:07:00 2011-04-07 15:08:00 2011-04-07 15:09:00 2011-04-07 15:10:00
               TRUE                TRUE                TRUE                TRUE
2011-04-07 15:11:00 2011-04-07 15:12:00 2011-04-07 15:13:00 2011-04-07 15:14:00
               TRUE                TRUE                TRUE                TRUE
‘zoo’ series from 2011-03-10 00:19:00 to 2011-04-07 15:14:00
  Data: logi [1:41156] TRUE TRUE TRUE TRUE TRUE TRUE ...
  Index:  POSIXct[1:41156], format: "2011-03-10 00:19:00" "2011-03-10 00:20:00" ...
function (x)
{
    inherits(x, "xts") && is.numeric(.index(x)) && !is.null(indexClass(x))
}
<environment: namespace:xts>

result<-apply.daily(xm,FUN=rollapply(xm,width=20,FUN=testFun,by.column=FALSE, align="right"))

I get the following error:

Error in get(as.character(FUN), mode = "function", envir = envir) :
  object 'FUN' of mode 'function' was not found
Calls: apply.daily -> period.apply -> match.fun -> get
Execution halted

However, if I revise the testFun code to return xts object, I get an error. This is the revision to the function:

ret_val <- data.frame(ctr,r_sq,price)
        ret_val <- as. matrix(ret_val)
ret_val<-as.xts(ret_val)
        ret_val
    }

The error it produces is:

Error in as.POSIXlt.character(x, tz, ...) :
  character string is not in a standard unambiguous format
Calls: rollapply ... as.POSIXct.default -> as.POSIXct -> as.POSIXlt -> as.POSIXlt.character
Execution halted

Can anyone suggest to me how I get my testFun to return an xts object instead of a zoo object so I can use apply.daily.

Was it helpful?

Solution

Try this.

library(quantmod)

Lines <- "M11 2011-03-10  0   104 365 T   N   N   1
...other lines from post...
M11 2011-03-10  0   126 355 T   N   N   1"

DF <- read.table(text = Lines)
tt <- as.POSIXct(sprintf("%s %04d", DF[[2]], DF[[4]]), format = "%Y-%m-%d %H%M")
x <- xts(DF[[5]], tt)
xm <- to.minutes(x)
tseq <- seq(start(xm), end(xm), by = 60)
xm.x <- na.approx(xm, xout = tseq)

OTHER TIPS

Here's the fix. Basically, you should use by ="mins" in your seq formulation. And always remember which time object class you use. You can't mix POSIXlt and POSIXct

my.data <- read.table(text="Date,Open,High,Low,Close
2011-08-11 03:00:00,421.20,421.20,421.20,421.20
2011-08-11 03:02:00,421.50,421.40,421.20,422.50
2011-08-11 03:05:00,421.60,421.60,421.50,421.50", header=TRUE, as.is=TRUE,sep = ",")

library(xts)
core <-xts(my.data[,-1],as.POSIXlt(my.data[,1],format="%Y-%m-%d %H:%M:%S") )
tseq <- as.POSIXlt(seq(start(core),end(core), by ="mins"))
core <- na.approx(core, xout = tseq)
core

                        Open     High   Low    Close
2011-08-11 03:00:00 421.2000 421.2000 421.2 421.2000
2011-08-11 03:01:00 421.3500 421.3000 421.2 421.8500
2011-08-11 03:02:00 421.5000 421.4000 421.2 422.5000
2011-08-11 03:03:00 421.5333 421.4667 421.3 422.1667
2011-08-11 03:04:00 421.5667 421.5333 421.4 421.8333
2011-08-11 03:05:00 421.6000 421.6000 421.5 421.5000
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top