R package development when functions are written in S4 and using roxygen2

I took the following code from R Companion to Applied Regression - Chapter 8 . Everything works fine except for the code Rwritten in S4. When I create the documentation, I receive lreg5-class.Rd, and not, lreg5.Rdand cannot receive the function lreg5. This is my first attempt to build a package R, so please bear with me. I know that I must miss something obvious. Any help would be greatly appreciated. Thanks


Lreg5 function


#' 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")
        }
    )

Packaging


# 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)
+4
source share
2 answers

A few tips:

  • , . roxygen .Rd.
  • , , . RStudio, ( roxygen2). RStudio .
  • -, , , roxygen2. . roxygen .R . .

:

, .Rd roxygen @rdname. , , .RD . - @describeIn, . , .Rd //.

. .Rd roxygen2

, . S4 roxygen2 , , . , , , Bioconductor unifiedWMWqPCR. roxygen2 S4. :

http://www.bioconductor.org/packages/release/bioc/html/unifiedWMWqPCR.html

+4

@Joris , , , . , lreg5. , , . . , roxygen2 , lreg5-class, man/lreg5-class.Rd.

, , - ( : https://github.com/jefferis/lreg, .)

#' carry out regression
#' @param X,y inputs
#' @param predictors defaults to \code{colnames(x)}
#' @param max.iter maximum number of iterations (default: 10)
#' @param tol tolerance
#' @param constant add a constant
#' @param ... Additional arguements (currently ignored)
#' @export
#' @return an object of class lreg5
#' @seealso \code{\link{lreg5-class}}
#' @examples
#' lreg5(X=matrix(rnorm(3*100),ncol=3, dimnames = list(NULL, letters[1:3])), 
#'       y=sample(0:1,100, replace=TRUE))

lreg5 <-function(X, y, predictors=colnames(X), max.iter=10,
        tol=1E-6, constant=TRUE, ...) {
## rest of function definition ...
}

@export , ; , . , man/lreg5.Rd lreg5 .

, , , lreg5 lreg5 , .

+1

All Articles