# Example of Hypergeometric function # Help page: # http://rss.acs.unt.edu/Rdoc/library/stats/html/Hypergeometric.html # # Download URL for R Windows Platform (version 2.4.1) # http://www.cran.r-project.org/bin/windows/base/R-2.4.1-win32.exe # Parameters # Total size of lot LotSize <- 1000 # Maximum percentage of defects in Lotsize maxDefectPerc <- .25 # Number of draws in sample sampSize <- 15 # Tick-mark resolution on x-axis gridResolution <- .01 # Accept zero defects in sample acceptance <- 0 # Define function for Operating Characteristic Curve for Hypergeometric Curve # k=sampSize for the "dyper" function; acceptance=number of defects hyperCurve <- function(k, maxDefectPerc, LotSize, gridResolution) { percentDef <- seq(0, maxDefectPerc, by=gridResolution) defects <- LotSize*percentDef nondefects <- LotSize*(1-percentDef) yaxisValues <- dhyper(acceptance, defects, nondefects, k) return(yaxisValues) } # Create empty matrix with zeros graphsHyperCurve <- matrix(0, ncol=sampSize, nrow=length(seq(0,maxDefectPerc,gridResolution))) # Loop over various sample sizes; put hypergeometric curves in columns of matrix for(k in 1:sampSize){ graphsHyperCurve[,k] <- hyperCurve(k, maxDefectPerc, LotSize,gridResolution) } # Display x-y values of curves for various sample sizes (e.g. k) graphsHyperCurve # Create x-axis values xAxis <- seq(0,maxDefectPerc,gridResolution) # Plot all curves on one graph matplot(xAxis, graphsHyperCurve[,c(1:sampSize)], ylim=c(0,1), main='Operating Characteristic Curve for Hypergeometric Probability Distribution', xlab='Percentage of Defects in Total Lot',ylab='Probabilty of NO Defects in the Sample') matlines(xAxis, graphsHyperCurve[,c(1:sampSize)], ylim=c(0,1))