【问题标题】:R source file for acf function为ACF函数R的源文件
【发布时间】:2018-07-18 07:27:45
【问题描述】:

R 统计库中的acf 函数包含以下行

.Call(C_acf, x, lag.max, type == "correlation")

但我在机器上的任何地方都找不到文件C_acf(在https://github.com/SurajGupta/r-sourcehttps://github.com/wch/r-source 也找不到)。

在这些问题上应用建议并没有帮助:

https://stats.stackexchange.com/questions/254227/manual-calculation-of-acf-of-a-time-series-in-r-close-but-not-quite

How to see the source code of R .Internal or .Primitive function?

该文件似乎不在人们说要查看的任何地方。如何找到 C_acf?

【问题讨论】:

  • Line 143 here?我认为 C_ 被删除了,就像 Ben Bolker 的 answer here on the question you link 中的 pnorm 一样
  • 啊哈!但是你是怎么知道要查看文件 filter.c 的呢?
  • 我在 github 存储库中搜索了“acf”,在左侧的语言侧栏中,我将结果过滤为 C。有 5 个结果,经检查,第一个是正确的。

标签: c r


【解决方案1】:

此方法将帮助识别类为CallRoutineNativeSymbolInfo的编译函数的源代码。

找到调用例程的命名空间

getAnywhere(C_acf)
# namespace:stats

下载您的基本 R 版本,因为 stats 是基本 R 的一部分。

download.file(url = "https://cran.r-project.org/src/base/R-3/R-3.0.0.tar.gz", destfile = "./R-3.0.0.tar.gz")
untar(tarfile = "./R-3.0.0.tar.gz", exdir = "./")

处理目录路径

old_dir <- getwd()
setwd("./R-3.0.0/src/library/stats/src/")

在源文件中找到单词acf。您必须浏览结果列表并确定确切的功能。最简单的方法是查看函数名称及其参数。

myresults <- sapply( list.files("./"), function(x) grep("acf", readLines(x), value = TRUE))
myresults <- myresults[lengths(myresults) != 0]
myresults[2] 
# $filter.c
# [1] "acf0(double *x, int n, int ns, int nl, int correlation, double *acf)"
# [2] "\t\tacf[lag + d1*u + d2*v] = (nu > 0) ? sum/(nu + lag) : NA_REAL;"     
# [3] "\t    se[u] = sqrt(acf[0 + d1*u + d2*u]);"                            
# [4] "\t\tacf[0 + d1*u + d2*u] = 1.0;"                                       
# [5] "\t\t\tacf[lag + d1*u + d2*v] /= se[u]*se[v];"                           
# [6] "SEXP acf(SEXP x, SEXP lmax, SEXP sCor)"                              
# [7] "    acf0(REAL(x), nx, ns, lagmax, cor, REAL(ans));"  

重置旧目录路径

setwd(old_dir)

参考:

【讨论】:

    猜你喜欢
    • 2017-12-14
    • 2020-03-11
    • 1970-01-01
    • 2014-08-21
    • 2021-01-08
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多