MYa*_*208 5 r devtools package s4 roxygen2
我从An R Companion to Applied Regression - Chapter 8 中获取了以下代码。除了R用S4. 当我构建文档时,我得到了lreg5-class.Rd而不是lreg5.Rd并且无法得到lreg5功能。这是我第一次尝试构建一个R包。
#' An S4 class to Logistic Regression.
#'
#' @export
#'
#' @slot coefficients Coefficients
#' @slot var Variance Covariance Matrix
#' @slot deviance Deviance
#' @slot predictors Predictors of the model
#' @slot iterations No of iterations for convergence
setClass(
Class = "lreg5"
, slots = list(
coefficients="numeric"
, var="matrix"
, deviance="numeric"
, predictors="character"
, iterations="numeric"
)
)
lreg5 <-
function(X, y, predictors=colnames(X), max.iter=10,
tol=1E-6, constant=TRUE, ...) {
if (!is.numeric(X) || !is.matrix(X))
stop("X must be a numeric matrix")
if (!is.numeric(y) || !all(y == 0 | y == 1))
stop("y must contain only 0s and 1s")
if (nrow(X) != length(y))
stop("X and y contain different numbers of observations")
if (constant) {
X <- cbind(1, X)
colnames(X)[1] <- "Constant"
}
b <- b.last <- rep(0, ncol(X))
it <- 1
while (it <= max.iter){
p <- as.vector(1/(1 + exp(-X %*% b)))
var.b <- solve(crossprod(X, p * (1 - p) * X))
b <- b + var.b %*% crossprod(X, y - p)
if (max(abs(b - b.last)/(abs(b.last) + 0.01*tol)) < tol) break
b.last <- b
it <- it + 1
}
if (it > max.iter) warning("maximum iterations exceeded")
dev <- -2*sum(y*log(p) + (1 - y)*log(1 - p))
result <- new("lreg5", coefficients=as.vector(b), var=var.b,
deviance=dev, predictors=predictors, iterations=it)
result
}
setMethod("show", signature(object="lreg5"),
definition=function(object) {
coef <- object@coefficients
names(coef) <- object@predictors
print(coef)
}
)
setMethod("summary", signature(object="lreg5"),
definition=function(object, ...) {
b <- object@coefficients
se <- sqrt(diag(object@var))
z <- b/se
table <- cbind(b, se, z, 2*(1-pnorm(abs(z))))
colnames(table) <- c("Estimate", "Std.Err", "Z value", "Pr(>z)")
rownames(table) <- object@predictors
printCoefmat(table)
cat("\nDeviance =", object@deviance,"\n")
}
)
Run Code Online (Sandbox Code Playgroud)
# Step 0: Packages you will need
library(devtools)
library(roxygen2)
# Step 1: Create your package directory
setwd("WD")
create("PackageName")
# Step 2: Add functions
# Step 3: Add documentation
# Step 4: Process your documentation
setwd("./PackageName")
devtools::document()
# Step 5: Install!
setwd("..")
#load_all("PackageName")
devtools::install("PackageName")
# Stp 6: Load the Package!
library(PackageName)
help(PackageName)
Run Code Online (Sandbox Code Playgroud)
一些提示:
roxygen2集成了所有构建工具(包括 )。查看RStudio 文档以获取更多信息。roxygen2第三,您在此处显示的代码中没有命令。你至少需要做一点努力。roxygen 系统允许您在 .R 文件本身中注释您的函数,并根据这些注释生成帮助文件。但如果没有评论,你就不会产生太多成果。更具体一点:
您可以使用 roxygen 标记定义哪些信息将发送到哪个 .Rd 文件@rdname。如果我想将一些内容分组到一个文件中,但将信息分散到不同的 .Rd 文件中,我会使用此选项。另一种选择是@describeIn最近推出的。后一个标签允许您指定必须在哪个 .Rd 文件中描述某个函数/类/...。
有关更多信息,请参阅有关使用以下命令创建 .Rd 文件的小插图:roxygen2
我强烈建议您查看不同的小插图以获得一些想法。使用文档记录 S4 类和方法roxygen2并不像人们期望的那么简单,但它绝对是可行的。例如,请查看 Bioconductor 包unifiedWMWqPCR。我已经研究过这个并用于roxygen2基于 S4 的包。源码可以从以下链接下载:
http://www.bioconductor.org/packages/release/bioc/html/unifiedWMWqPCR.html