R/sigtest.R

Defines functions summary.sigtest print.sigtest sigtest

sigtest <- function(In,
                    In.bootstrap,
                    P,
                    bws = NA,
                    ixvar,
                    boot.method,
                    pivot,
                    joint,
                    boot.type,
                    boot.num,
                    pivot.effective = NULL,
                    bootstrap.executed = NULL,
                    bootstrap.reason = NULL){

  if (is.null(pivot.effective))
    pivot.effective <- rep.int(isTRUE(pivot), length(ixvar))
  names(pivot.effective) <- bws$xnames[ixvar]
  if (is.null(bootstrap.executed))
    bootstrap.executed <- rep.int(as.integer(boot.num), length(In))
  if (is.null(bootstrap.reason))
    bootstrap.reason <- rep.int(NA_character_, length(In))

  tsig <- list(In = In,
               In.bootstrap = In.bootstrap,
               P = P,
               bws = bws,
               ixvar = ixvar,
               pmethod = switch(boot.method,
                 "pairwise" = "Pairwise",
                 "iid" = "IID",
                 "wild" ="Wild",
                 "wild-rademacher" = "Rademacher Wild"),
               pivot = pivot,
               joint = joint,
               ptype = boot.type,
               boot.num = boot.num,
               pivot.effective = pivot.effective,
               bootstrap.executed = bootstrap.executed,
               bootstrap.reason = bootstrap.reason)
  
  tsig$reject <- rep('', length(In))
  tsig$rejectNum <- rep(NA, length(In))

  tsig$reject[a <- (P < 0.1)] <- '.'
  tsig$rejectNum[a] <- 10

  tsig$reject[a <- (P < 0.05)] <- '*'
  tsig$rejectNum[a] <- 5

  tsig$reject[a <- (P < 0.01)] <- '**'
  tsig$rejectNum[a] <- 1

  tsig$reject[a <- (P < 0.001)] <- '***'
  tsig$rejectNum[a] <- 0.1

  class(tsig) = "sigtest"

  tsig
}

print.sigtest <- function(x, ...){
  pivot.requested <- x[["pivot", exact = TRUE]]
  pivot.effective <- x[["pivot.effective", exact = TRUE]]
  has.effective <- is.logical(pivot.effective) && length(pivot.effective) > 0L
  pivot.label <- if (is.null(pivot.requested) && has.effective) {
    if (length(unique(pivot.effective)) == 1L) {
      paste0("automatic -> ", unique(pivot.effective))
    } else {
      "automatic by predictor type"
    }
  } else if (is.null(pivot.requested)) {
    "NULL"
  } else {
    as.character(pivot.requested)
  }

  cat("\nKernel Regression Significance Test",
      "\nType ", x$ptype,
      " Test with ", x$pmethod,
      " Bootstrap (",x$boot.num," requested replications,",
      " Pivot = ", pivot.label,", joint = ",x$joint,")",
      "\nExplanatory variables tested for significance:\n",
      paste(paste(x$bws$xnames[x$ixvar]," (",x$ixvar,")", sep=""), collapse=", "),"\n\n",
      sep="")

  executed <- x[["bootstrap.executed", exact = TRUE]]
  skipped <- which(!is.na(executed) & executed == 0L)
  if (length(skipped)) {
    labels <- if (isTRUE(x$joint)) "joint test" else x$bws$xnames[x$ixvar][skipped]
    cat("Analytic non-rejection; no bootstrap fits: ",
        paste(labels, collapse = ", "), ".\n",
        "Skipped bootstrap-statistic columns contain NA, not simulated draws.\n\n",
        sep = "")
  }

  if (is.null(pivot.requested) && has.effective &&
      length(unique(pivot.effective)) > 1L) {
    cat("Effective pivoting: ",
        paste(paste(names(pivot.effective), pivot.effective, sep = "="),
              collapse = ", "),
        "\n\n", sep = "")
  }
      

  if (!identical(x$bws,NA))
    print(matrix(x$bws$bw,ncol=x$bws$ndim,dimnames=list(paste(x$bws$pscaling,":",sep=""),
                                            x$bws$xnames)))

  maxNameLen <- max(nc <- nchar(nm <- x$bws$xnames[x$ixvar]))

  if(!x$joint) {
    cat("\nIndividual Significance Tests\n")
    cat("P Value:", paste("\n", nm," ", blank(maxNameLen-nc), .np_test_format_pvalue(x$P, x$boot.num),
                          " ", x$reject, sep=''))
  } else {
    cat("\nJoint Significance Test\n")
    cat("P Value:", paste(" ", .np_test_format_pvalue(x$P, x$boot.num)," ", x$reject, sep=''))
  }
  cat("\n---\nSignif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n")
}

summary.sigtest <- function(object, ...) {
  print(object)
  .np_test_print_time(object)
}

Try the np package in your browser

Any scripts or data that you put into this service are public.

np documentation built on Nov. 25, 2026, 1:06 a.m.