[Vectis-commits] r6 - in pkg: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Mar 28 21:58:51 CET 2013
Author: cbattles
Date: 2013-03-28 21:58:50 +0100 (Thu, 28 Mar 2013)
New Revision: 6
Modified:
pkg/R/Cap_anal.R
pkg/man/Cap_anal.Rd
Log:
Added plot = TRUE/FALSE as a parameter
Updated documentation
Code commenting
Modified: pkg/R/Cap_anal.R
===================================================================
--- pkg/R/Cap_anal.R 2013-03-28 20:33:18 UTC (rev 5)
+++ pkg/R/Cap_anal.R 2013-03-28 20:58:50 UTC (rev 6)
@@ -1,3 +1,4 @@
+#Function to generate text and graphical capabilities analyses on a data set
vectis.cap <- function(data,
distribution = "normal",
USL = NA,
@@ -12,7 +13,8 @@
unbias_sub = TRUE,
unbias_overall = FALSE,
density = FALSE,
- binwidth = -1
+ binwidth = -1,
+ plot = TRUE
)
{
library(ggplot2)
@@ -21,10 +23,10 @@
if (is.na(target)){
stop("Target not specified")
}
- if (is.na(LSL) & is.na(USL)){
+ if (is.na(LSL) && is.na(USL)){
stop("Upper and Lower Specification Limits not specified")
}
- if (groupsize < 1 | groupsize > 50){
+ if (groupsize < 1 || groupsize > 50){
stop("Group Size must be between 1 and 50")
}
if (mrlength < 2){
@@ -98,6 +100,10 @@
# unbias_sub = TRUE
# unbias_overall = TRUE
+ # Estimate the standard deviation within subgroups by the average of the moving range
+ # Add other methods here for subgroup size of 1
+
+
if (groupsize == 1){
R_i <- vector(mode = "numeric", length = (length(data[!is.na(data)])-(mrlength-1)))
range_temp <- vector(mode = "numeric", length = mrlength)
@@ -120,6 +126,7 @@
#Add Here
}
+ # Calculate overall standard deviation and apply the unbiasing constant if desired
if (unbias_overall) {
S_overall <- sd(data)/(Lookup$c4[length(data[!is.na(data)])])
} else {
@@ -140,8 +147,7 @@
Proc_Data["StDev(Within)"] <- S_within
Proc_Data["StDev(Overall)"] <- S_overall
Proc_Data["Group Size"] <- groupsize
-
-
+
# Potential Capability Matrix
CPS <- vector(mode = "numeric", length = 5)
names(CPS) <- c("Cp","CPL", "CPU", "Cpk", "CCpk")
@@ -173,14 +179,14 @@
PERF["OBGU"] <- 1e6*(length(data[data>USL])/length(data[!is.na(data)]))
PERF["OBT"] <- sum(PERF["OBLL"],PERF["OBGU"])
- #Determine max densities for plot limits
+ if(plot){
+ #Determine max densities for plot limits
if(density) dens_max <- max(density(data)[[2]]) else dens_max <- 0
freq_max <- max(hist(as.vector(data), plot = FALSE)$density)
with_max <- dnorm(mean(data), mean = mean(data),sd = S_within)
over_max <- dnorm(mean(data), mean = mean(data),sd = S_overall)
-
-
+
#Calculate the binwidth if not specified
if (binwidth == -1) {
#Freedman-Diaconis
@@ -189,10 +195,13 @@
#binwidth = diff(range(data))/sqrt(length(data[!is.na(data)]))
}
-# Create Plots
+ #Create Plots
data <- as.data.frame(data)
+
+ #define function for aes that evaluates expressions
aes_now <- function(...) {structure(list(...), class = "uneval")}
+ #Initial plot definition
p <- ggplot(data, aes(x = data)) +
theme(plot.margin = unit(c(3,1,1,1), "lines"),
panel.grid.minor = element_blank(),
@@ -213,15 +222,16 @@
max(max(data),1.1 * USL - 0.1 * LSL, target + 3 * S_within, target + 3 * S_overall)) +
ylim(0, max(1.05 * dens_max, 1.05 * freq_max,
1.05 * with_max, 1.05 * over_max))
-
+ #Add histogram
p <- p + geom_histogram(aes(y=..density..),
binwidth = binwidth,
color = "black", fill = "slategray1", position = "identity")
+ #Add Density
if(density) {p <- p + geom_line(stat="density", size = 1.1,
color = "dodgerblue3", position="identity")}
-
+ #Add Spec Limits and labels
p <- p + geom_vline(xintercept = LSL, linetype = 5, size = .65, color = "red3")
p <- p + geom_vline(xintercept = target, linetype = 5, size = .65, color = "green3")
p <- p + geom_vline(xintercept = USL, linetype = 5, size = .65, color = "red3")
@@ -233,44 +243,28 @@
p <- p + geom_text(aes_now(label = c("Target"), x = c(target), y = Inf, family = "sans"),
hjust = .5, vjust = -1, color = "green3", size=5)
+ #Add within and overall distribution lines
p <- p + stat_function(fun = dnorm,args=list(mean = mu, sd = S_within),
color = "red3", size = 1.1, linetype = 1)
p <- p + stat_function(fun = dnorm,args=list(mean = mu, sd = S_overall),
color = "gray0", size = 1.1, linetype = 2)
-# p <- p + opts(panel.background = theme_rect())
-
-# p <- p + annotate(geom = "text",
-# x = LSL,
-# y = 0,
-# label = "LSL",
-# hjust = -0.1,
-# size = 5, color = "darkred")
-# p <- p + annotate(geom = "text",
-# x = target,
-# y = 0,
-# label = "TAR",
-# hjust = -0.1,
-# size = 5, color = "green3")
-# p <- p + annotate(geom = "text",
-# x = USL,
-# y = 0,
-# label = "USL",
-# hjust = 1.1,
-# size = 5, color = "darkred")
-
- # Disable Clipping
+ #Disable Clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
+ #Render plot
print(gt)
+ }
+ #Define output
output <- list(Proc_Data,CPS,PPS,PERF)
class(output) <- 'myclass'
return(output)
}
+#Format Text Output
print.myclass <- function(x) {
cat("Capabiliy Analysis","\n")
print(noquote(cbind(`Process Data` = unlist(x[[1]]))), digits = 4)
Modified: pkg/man/Cap_anal.Rd
===================================================================
--- pkg/man/Cap_anal.Rd 2013-03-28 20:33:18 UTC (rev 5)
+++ pkg/man/Cap_anal.Rd 2013-03-28 20:58:50 UTC (rev 6)
@@ -1,78 +1,85 @@
-\name{vectis.cap}
-\alias{vectis.cap}
-%- Also NEED an '\alias' for EACH other topic documented here.
-\title{
-Function to Compute and Display Process Capability
-}
-\description{
-Computes the process capability of a continuous variable
-}
-\usage{
-vectis.cap (x, distribution = "normal",
- USL = NA,
- LSL = NA,
- target = NA,
- main = "Capabilities Analysis",
- sub = "",
- groupsize = 1,
- mrlength = 2,
- alpha = 0.05,
- tol = 5.15,
- unbias_sub = TRUE,
- unbias_overall = FALSE)
-}
-%- maybe also 'usage' for other objects documented here.
-\arguments{
- \item{x}{Data values}
- \item{distribution}{Distribution of sampled data. Currently only "normal" is supported.}
- \item{USL}{Upper Spec Limit}
- \item{LSL}{Lower Spec Limit}
- \item{target}{Target}
- \item{main}{Chart Title}
- \item{sub}{Chart Subtitle}
- \item{groupsize}{Group size for grouped data. Data must be sorted by groups. Currently only a groupsize of 1 is supported.}
- \item{mrlength}{Number of observations used in the moving range to compute the within standard deviation for groupsize = 1.}
- \item{alpha}{Probability of a Type I error}
- \item{tol}{Sigma multiplier for capabilities statistics. Usually defined as 5.15 or 6}
- \item{unbias_sub}{Logical value as to whether the subgroup standard deviation should include the unbiasing constant. Default is TRUE}
- \item{unbias_overall}{Logical value as to whether the overall standard deviation should include the unbiasing constant. Default is FALSE}
-}
-\details{
-%% ~~ If necessary, more details than the description above ~~
-}
-\value{
-%% ~Describe the value returned
-%% If it is a LIST, use
-%% \item{comp1 }{Description of 'comp1'}
-%% \item{comp2 }{Description of 'comp2'}
-%% ...
-}
-\references{
-%% ~put references to the literature/web site here ~
-}
-\author{
-Christopher Battles
-}
-\note{
-%% ~~further notes~~
-}
-
-%% ~Make other sections like Warning with \section{Warning }{....} ~
-
-\seealso{
-%% ~~objects to See Also as \code{\link{help}}, ~~~
-}
-\examples{
-##---- Should be DIRECTLY executable !! ----
-##-- ==> Define data, use random,
-##-- or do help(data=index) for the standard data sets.
-
-## The function is currently defined as
-function (x)
-{
- }
-}
-% Add one or more standard keywords, see file 'KEYWORDS' in the
-% R documentation directory.
-\keyword{ ~kwd1 }
-\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line
+\name{vectis.cap}
+\alias{vectis.cap}
+%- Also NEED an '\alias' for EACH other topic documented here.
+\title{
+Function to Compute and Display Process Capability
+}
+\description{
+Computes the process capability of a continuous variable
+}
+\usage{
+vectis.cap(data,
+ distribution = "normal",
+ USL = NA,
+ LSL = NA,
+ target = NA,
+ main = "Capabilities Analysis",
+ sub = "",
+ groupsize = 1,
+ mrlength = 2,
+ alpha = 0.05,
+ tol = 5.15,
+ unbias_sub = TRUE,
+ unbias_overall = FALSE
+ density = FALSE,
+ binwidth = -1
+ plot = TRUE)
+}
+%- maybe also 'usage' for other objects documented here.
+\arguments{
+ \item{x}{Data values}
+ \item{distribution}{Distribution of sampled data. Currently only "normal" is supported.}
+ \item{USL}{Upper Spec Limit}
+ \item{LSL}{Lower Spec Limit}
+ \item{target}{Target}
+ \item{main}{Chart Title}
+ \item{sub}{Chart Subtitle}
+ \item{groupsize}{Group size for grouped data. Data must be sorted by groups. Currently only a groupsize of 1 is supported.}
+ \item{mrlength}{Number of observations used in the moving range to compute the within standard deviation for groupsize = 1.}
+ \item{alpha}{Probability of a Type I error.}
+ \item{tol}{Sigma multiplier for capabilities statistics. Usually defined as 5.15 or 6.}
+ \item{unbias_sub}{Logical value as to whether the subgroup standard deviation should include the unbiasing constant. Default is TRUE.}
+ \item{unbias_overall}{Logical value as to whether the overall standard deviation should include the unbiasing constant. Default is FALSE.}
+ \item{density}{Logical value to control the display of the density curve. Default is FALSE.}
+ \item{binwidth}{Overrides automatic determination of bin width in histogram. If set to -1 (default) then the function automatically calculates the optimum binwidth.}
+ \item{plot}{Logical value to control plotting. Default is TRUE}
+}
+\details{
+%% ~~ If necessary, more details than the description above ~~
+}
+\value{
+%% ~Describe the value returned
+%% If it is a LIST, use
+%% \item{comp1 }{Description of 'comp1'}
+%% \item{comp2 }{Description of 'comp2'}
+%% ...
+}
+\references{
+%% ~put references to the literature/web site here ~
+}
+\author{
+Christopher Battles
+}
+\note{
+%% ~~further notes~~
+}
+
+%% ~Make other sections like Warning with \section{Warning }{....} ~
+
+\seealso{
+%% ~~objects to See Also as \code{\link{help}}, ~~~
+}
+\examples{
+##---- Should be DIRECTLY executable !! ----
+##-- ==> Define data, use random,
+##-- or do help(data=index) for the standard data sets.
+
+## The function is currently defined as
+function (x)
+{
+ }
+}
+% Add one or more standard keywords, see file 'KEYWORDS' in the
+% R documentation directory.
+\keyword{ ~kwd1 }
+\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line
More information about the Vectis-commits
mailing list