首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R不能找到函数"bt.matching.find“

R不能找到函数"bt.matching.find“
EN

Stack Overflow用户
提问于 2019-07-25 01:01:24
回答 1查看 205关注 0票数 0

我在从托管在Github上的SIT工具箱实现函数bt.matching.find时遇到了一个问题。按照here描述的步骤下载工具箱后,我尝试复制此blog中描述的代码

代码语言:javascript
复制
library(SIT.dates)
library(SIT)
objt <- bt.matching.find(Cl(data), normalize.fn = normalize.mean, dist.fn = 'dist.euclidean', plot=T)

R没有找到该函数,所以我尝试使用空格来访问该函数

代码语言:javascript
复制
objt <- SIT:::bt.matching.find(Cl(data), normalize.fn = normalize.mean, dist.fn = 'dist.euclidean', plot=T)

但这次我得到了一个奇怪的错误,它与函数中的任何参数都没有关系

代码语言:javascript
复制
Error in last(data, n.reference) : could not find function "last"

我使用函数getAnywhere对函数bt.matching.find进行了研究,得到的结果如下

代码语言:javascript
复制
getAnywhere("bt.matching.find")
A single object matching ‘bt.matching.find’ was found
It was found in the following places
  namespace:SIT
with value

function (data, n.query = 90, n.reference = 252 * 10, n.match = 10, 
    normalize.fn = normalize.mean.sd, dist.fn = dist.euclidean, 
    plot = FALSE, plot.dist = FALSE, layout = NULL, main = NULL) 
{
    data = last(data, n.reference)
    reference = coredata(data)
    n = len(reference)
    query = reference[(n - n.query + 1):n]
    reference = reference[1:(n - n.query)]
    main = paste(main, join(format(range(index(data)[(n - n.query + 
        1):n]), "%d%b%Y"), " - "))
    n.query = len(query)
    n.reference = len(reference)
    dist.fn.name = ""
    if (is.character(dist.fn)) {
        dist.fn.name = paste("with", dist.fn)
        dist.fn = get(dist.fn)
    }
    dist = rep(NA, n.reference)
    query.normalized = match.fun(normalize.fn)(query)
    for (i in n.query:n.reference) {
        window = reference[(i - n.query + 1):i]
        window.normalized = match.fun(normalize.fn)(window)
        dist[i] = match.fun(dist.fn)(rbind(query.normalized, 
            window.normalized))
        if (i%%100 == 0) 
            cat(i, "\n")
    }
    min.index = c()
    temp = dist
    temp[temp > mean(dist, na.rm = T)] = NA
    for (i in 1:n.match) {
            if (any(!is.na(temp))) {
            index = which.min(temp)
            min.index[i] = index
            temp[max(0, index - 2 * n.query):min(n.reference, 
                (index + n.query))] = NA
        }
    }
    n.match = len(min.index)
    if (plot) {
        dates = index(data)[1:len(dist)]
        if (is.null(layout)) {
            if (plot.dist) 
                layout(1:2)
            else layout(1)
        }
        par(mar = c(2, 4, 2, 2))
        if (plot.dist) {
            plot(dates, dist, type = "l", col = "gray", main = paste("Top 
Historical Matches for", 
                main, dist.fn.name), ylab = "Distance", xlab = "")
            abline(h = mean(dist, na.rm = T), col = "darkgray", 
                lwd = 2)
            points(dates[min.index], dist[min.index], pch = 22, 
                col = "red", bg = "red")
            text(dates[min.index], dist[min.index], 1:n.match, 
                adj = c(1, 1), col = "black", xpd = TRUE)
        }
        plota(data, type = "l", col = "gray", LeftMargin = 1, 
            main = iif(!plot.dist, paste("Top Historical Matches for", 
                main), NULL))
        plota.lines(last(data, 90), col = "blue")
        for (i in 1:n.match) {
            plota.lines(data[(min.index[i] - n.query + 1):min.index[i]], 
                col = "red")
        }
        text(index4xts(data)[min.index - n.query/2], reference[min.index - 
            n.query/2], 1:n.match, adj = c(1, -1), col = "black", 
            xpd = TRUE)
        plota.legend(paste("Pattern: ", main, ",Match Number"), 
            "blue,red")
    }
    return(list(min.index = min.index, dist = dist[min.index], 
        query = query, reference = reference, dates = index(data), 
        main = main))
}
<bytecode: 0x000000e7e11c8a00>
<environment: namespace:SIT>

我尝试使用backports包调用该函数

代码语言:javascript
复制
library(backports)
.onLoad <- function(libname, pkgname) {
  backports::import(SIT, "bt.matching.find", force = TRUE)
}

但这也不起作用

为什么R不能访问该函数?这可能是因为这个包是在旧版本下构建的吗?

更多信息

环境

代码语言:javascript
复制
sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
EN

回答 1

Stack Overflow用户

发布于 2019-07-30 17:49:15

这个问题在包的开发人员的帮助下得到了解决,对于任何对使用代码感兴趣的人来说,这里是应该进行的调整

代码语言:javascript
复制
library(SIT)
library(quantmod)
tickers = 'SPY'

data = getSymbols(tickers, src = 'yahoo', from = '1950-01-01', auto.assign = F)


obj = SIT:::bt.matching.find(Cl(data), normalize.fn = SIT:::normalize.mean, dist.fn = 'dist.euclidean', plot=T)

matches = SIT:::bt.matching.overlay(obj, plot.index=1:90, plot=T)

layout(1:2)
matches = SIT:::bt.matching.overlay(obj, plot=T, layout=T)
SIT:::bt.matching.overlay.table(obj, matches, plot=T, layout=T)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57187965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档