eapply用于用户定义的功能

时间:2014-01-26 05:21:05

标签: r function quantmod

我想通过eapply将用户定义的功能用于存储代码的环境,但不确定我做错了什么......

    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)

# the above works fine, but below is the problem    

    asp <- eapply(augenSpike(as.vector(Cl(myenv), FUN=augenSpike))
    sp$spike <- asp

# I get the message: 
## unexpected symbol in:
## "asp <- eapply(augenSpike(as.vector(Cl(myenv), FUN=augenSpike))
## sp"

0 个答案:

没有答案