我想应用用户定义的函数augenSpike通过存储在环境中的一组代码,但不知何故,它不工作。如果有人可以帮助这个入门水平R用户,我会非常感激......使用适用于R与用户定义的功能
library(quantmod)
slideapply <- function(x, n, FUN=sd) {
v <- c(rep(NA, length(x)))
for (i in n:length(x)) {
v[i] <- FUN(x[(i-n+1):i])
}
return(v)
}
augenSpike <- function(x, n=20) {
prchg <- c(NA, diff(x))
lgchg <- c(NA, diff(log(x)))
stdevlgchg <- slideapply(lgchg, n, sd)
stdpr <- x * stdevlgchg
#shuffle things up one
stdpr <- c(NA, stdpr[-length(stdpr)])
spike <- prchg/stdpr
return(spike)
}
myenv <- new.env()
# environment used to store tickers
tickers <- c("PBR", "AAPL", "MSFT", "GOOG")
getSymbols(tickers, env= myenv)
sp <-tickers['2013/2014']
asp <- augenSpike(as.vector(Cl(sp)))
sp$spike <- asp
## Create a vector of colors selected based on whether x is <0 or >0
## (FALSE + 1 -> 1 -> "blue"; TRUE + 1 -> 2 -> "red")
cols <- c("blue", "red") [(sp$spike > 0) + 1]
barplot(sp['2013-2014']$spike, col= cols, main="Augen Price Spike", xlab="Time Daily",ylab="Price Spike in Std Dev")
abline(h = 2, col = "red")
abline(h = 0, col = "black")
abline(h = -2, col = "red")
你能提供这个问题的降低的例子吗? – weitzner
我想通过myenv中存储的代码重复幻灯片应用程序和AugenSpike函数。问题是,我不知道如何正确使用eapply函数和我的代价。谢谢 –