如何通过 R 中的“.R”文件中的“@importFrom”导入超过 1 个函数?
How to import more than 1 function via "@importFrom" in an ".R" file in R?
我想在我的包的“.R”文件中使用 vars::VAR 和 vars::roots 函数(即 StabilityPlot.R [VAR(p) 横跨 p 的稳定性];函数代码如下)。
考虑到氧合,我该如何进行?
我想到了以下几点:
#' @importFrom vars VAR roots
或
#' @importFrom vars VAR
#' @importFrom vars roots
或
#' @importFrom(vars, roots, VAR)
我应该使用哪一个?还是以上都错了?有什么线索吗?谢谢
StabilityPlot <- function(data, p=5, type = c("const","trend","both","none"))
{
out <- list()
data <- as.data.frame(data)
maxmodulusofinverseroots <- matrix(, nrow = p, ncol =1)
#........................................................................
if (type==c("const")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "const"), modulus=TRUE))})), ncol=1)
}
if (type==c("trend")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "trend"), modulus=TRUE))})), ncol=1)
}
if (type==c("both")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "both"), modulus=TRUE))})), ncol=1)
}
if (type==c("none")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "both"), modulus=TRUE))})), ncol=1)
}
out$maxmoduluses <- maxmodulusofinverseroots
#........................................................................
if (type==c("const")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:const", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("trend")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:trend", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("both")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:both", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("none")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:none", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
abline(1,0)
out
}
您的前两个建议都应该有效。您可以查看 roxygen2 手册的副本 here。第 3 页有您问题的答案。
我想在我的包的“.R”文件中使用 vars::VAR 和 vars::roots 函数(即 StabilityPlot.R [VAR(p) 横跨 p 的稳定性];函数代码如下)。
考虑到氧合,我该如何进行?
我想到了以下几点:
#' @importFrom vars VAR roots
或
#' @importFrom vars VAR
#' @importFrom vars roots
或
#' @importFrom(vars, roots, VAR)
我应该使用哪一个?还是以上都错了?有什么线索吗?谢谢
StabilityPlot <- function(data, p=5, type = c("const","trend","both","none"))
{
out <- list()
data <- as.data.frame(data)
maxmodulusofinverseroots <- matrix(, nrow = p, ncol =1)
#........................................................................
if (type==c("const")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "const"), modulus=TRUE))})), ncol=1)
}
if (type==c("trend")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "trend"), modulus=TRUE))})), ncol=1)
}
if (type==c("both")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "both"), modulus=TRUE))})), ncol=1)
}
if (type==c("none")) {
maxmodulusofinverseroots[,1] <- matrix(unlist(lapply(as.matrix(1:p),function(x){max(roots(VAR(mydata, p=x, type = "both"), modulus=TRUE))})), ncol=1)
}
out$maxmoduluses <- maxmodulusofinverseroots
#........................................................................
if (type==c("const")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:const", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("trend")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:trend", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("both")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:both", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
if (type==c("none")) {
plot(ts(maxmodulusofinverseroots), plot.type="single", xlab="p (order of VAR)", main="Stability of VAR(p) accross p; det.regr:none", ylab="MaxModOfInvRootsOfARchrPoly", col=c("black"), lty=1:2)
}
abline(1,0)
out
}
您的前两个建议都应该有效。您可以查看 roxygen2 手册的副本 here。第 3 页有您问题的答案。