R/myboot2_lab9.R

Defines functions myboot2

Documented in myboot2

#' @title Lab 9 function: myboot2()
#'
#' @param iter number of iterations
#' @param x data being sampled
#' @param fun a function that will give a statistic for the data
#' @param alpha alpha for the confidence interval
#' @param cx font size of the numbers on the histogram
#' @param ... for adding more customization to the histogram
#'
#' @return Returns a histogram of the statistic in "fun". Also can return the values for the confidence interval, the values for all the data used, and the function that was used to find the statistic.
#' @export
#'
#' @examples
#' \dontrun{myboot2(iter = 10000, x, fun = "mean", alpha = 0.05)}
#' \dontrun{myboot2(iter = 5000, x, fun = "var", alpha = 0.20)}
#'
myboot2<-function(iter=10000,x,fun="mean",alpha=0.05,cx=1.5,...){  #Notice where the ... is repeated in the code
  n=length(x)   #sample size

  y=sample(x,n*iter,replace=TRUE)
  rs.mat=matrix(y,nr=n,nc=iter,byrow=TRUE)
  xstat=apply(rs.mat,2,fun) # xstat is a vector and will have iter values in it
  ci=quantile(xstat,c(alpha/2,1-alpha/2))# Nice way to form a confidence interval
  # A histogram follows
  # The object para will contain the parameters used to make the histogram
  para=hist(xstat,freq=FALSE,las=1,
            main=paste("Histogram of Bootstrap sample statistics","\n","alpha=",alpha," iter=",iter,sep=""),
            ...)

  #mat will be a matrix that contains the data, this is done so that I can use apply()
  mat=matrix(x,nr=length(x),nc=1,byrow=TRUE)

  #pte is the point estimate
  #This uses whatever fun is
  pte=apply(mat,2,fun)
  abline(v=pte,lwd=3,col="Black")# Vertical line
  segments(ci[1],0,ci[2],0,lwd=4)      #Make the segment for the ci
  text(ci[1],0,paste("(",round(ci[1],2),sep=""),col="Red",cex=cx)
  text(ci[2],0,paste(round(ci[2],2),")",sep=""),col="Red",cex=cx)

  # plot the point estimate 1/2 way up the density
  text(pte,max(para$density)/2,round(pte,2),cex=cx)

  invisible(list(ci=ci,fun=fun,x=x))# Some output to use if necessary
}
theo-frantz/MATH4753fran0107 documentation built on Nov. 23, 2020, 11:21 a.m.