R/myconstructor.R

Defines functions print.Rttest myconstructor

Documented in myconstructor print.Rttest

#' Constructor Function
#'
#' @description Constructor function that takes the input of 2 vectors and an
#' integer value alpha and outputs the confidence interval and p-value for the t.test.
#'
#' @param x is a vector
#' @param y is a vector
#' @param alpha is a numeric value between 0 and 1 representing the error
#' @importFrom stats t.test
#'
#' @return list containing the data, alpha, the confidence interval from input alpha value
#'  and the resulting p-value.
#' @export
#'
#' @examples
#' myconstructor(x=rnorm(30,5,2),y=rnorm(40,3,2),0.05)
myconstructor <- function(x,y,alpha) {
  if(!is.numeric(x)||!is.numeric(y)){
    stop("X and Y must be numeric")
  }
  max_len <- max(c(length(x),length(y)))
  df <- data.frame("x"=c(x,rep(NA,max_len-length(x))),"y"=c(y,rep(NA,max_len-length(y))))
  test <- t.test(x,y,var.equal = TRUE,mu=0,conf.level=1-alpha)
  l <- list(df=df,alpha=alpha,confidence=test$conf.int[1:2],pvalue = test$p.value)
  class(l) <- "Rttest"
  l
}


#' Constructor print function
#'
#' @param x data from constructor function
#' @param ... table variables
#' @importFrom knitr kable
#' @importFrom kableExtra kable_styling add_header_above
#' @importFrom magrittr '%>%'
#'
#' @return a table with the data and the alpha value, confidence intervals and p-value.
#' @export
#'
#' @examples
#' \dontrun{print(x)}
print.Rttest <- function(x, ...) {
  df<-x$df
  data <- data.frame(alpha=x$alpha,confidence1=x$confidence[1],confidence2=x$confidence[2],pvalue=x$pvalue)
  print(knitr::kable(x = df,digits=3,col.names = NULL) %>%
          kable_styling(full_width = FALSE, bootstrap_options = "condensed",position="float_left") %>%
          add_header_above(c("Data"=2)))

  print(knitr::kable(x = data,digits=3,col.names = NULL) %>%
          kable_styling(full_width = FALSE, bootstrap_options = "condensed",position="float_left") %>%
          add_header_above(c("Alpha"=1,"Confidence"=2,"P-Value"=1)))
}
Jordan-Daugherty-1/projectpart3daugherty documentation built on April 20, 2022, 12:43 a.m.