gpt4 book ai didi

r - R 中的动物园 na.approx

转载 作者:行者123 更新时间:2023-12-01 16:19:48 31 4
gpt4 key购买 nike

我尝试通过以下引用链接引用动物园中的 na.approx 来使用 times:

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

这是我的代码:

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)

这是错误消息:

> 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"

我该如何解决这个问题?

如果我将其替换为“时间”,则 tseq 会按秒而不是按分钟递增。为什么?

下面是数据文件:

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

编辑:我将代码编辑为 G.Grothendieck 发布的内容,因此正在运行以下代码:

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)

但是,当我运行以下代码时,出现以下错误:

> 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

我的错误在哪里:?

编辑:用统计函数替换 str 并遇到错误:

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

编辑:我意识到我的错误是因为我的函数 testFun 返回了一个 Zoo 对象,而 apply.daily 需要一个 xts 对象。

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"))

我收到以下错误:

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

但是,如果我修改 testFun 代码以返回 xts 对象,则会收到错误。这是函数的修订版:

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

它产生的错误是:

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

任何人都可以向我建议如何让 testFun 返回 xts 对象而不是 Zoo 对象,以便我可以使用 apply.daily。

最佳答案

试试这个。

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)

关于r - R 中的动物园 na.approx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057530/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com