######Lab-work########################
#Rahul Sachan                        #
#MA14C035                            #
#Department of mathematics IIT Madras#
#************************************#

library(qcc)
## Warning: package 'qcc' was built under R version 3.1.3
## Package 'qcc', version 2.6
## Type 'citation("qcc")' for citing this R package in publications.
mu = 74
n  =5
k = 40
s = 1
set.seed(1)
D = rnorm(n*k, mu, s)
batch = rep(1:40, each = n)
E = data.frame(D=D, batch = batch)
dim(E)
## [1] 200   2
diameter <- qcc.groups(D, batch)
diameter
##        [,1]     [,2]     [,3]     [,4]     [,5]
## 1  73.37355 74.18364 73.16437 75.59528 74.32951
## 2  73.17953 74.48743 74.73832 74.57578 73.69461
## 3  75.51178 74.38984 73.37876 71.78530 75.12493
## 4  73.95507 73.98381 74.94384 74.82122 74.59390
## 5  74.91898 74.78214 74.07456 72.01065 74.61983
## 6  73.94387 73.84420 72.52925 73.52185 74.41794
## 7  75.35868 73.89721 74.38767 73.94619 72.62294
## 8  73.58501 73.60571 73.94069 75.10003 74.76318
## 9  73.83548 73.74664 74.69696 74.55666 73.31124
## 10 73.29250 74.36458 74.76853 73.88765 74.88111
## 11 74.39811 73.38797 74.34112 72.87064 75.43302
## 12 75.98040 73.63278 72.95587 74.56972 73.86495
## 13 76.40162 73.96076 74.68974 74.02800 73.25673
## 14 74.18879 72.19504 75.46555 74.15325 76.17261
## 15 74.47551 73.29005 74.61073 73.06590 72.74637
## 16 74.29145 73.55671 74.00111 74.07434 73.41048
## 17 73.43133 73.86482 75.17809 72.47643 74.59395
## 18 74.33295 75.06310 73.69582 74.37002 74.26710
## 19 73.45748 75.20787 75.16040 74.70021 75.58683
## 20 74.55849 72.72341 73.42673 72.77539 73.52660
## 21 73.37963 74.04212 73.08908 74.15803 73.34542
## 22 75.76729 74.71671 74.91017 74.38419 75.68218
## 23 73.36426 73.53836 75.43228 73.34930 73.79262
## 24 73.60719 73.68001 73.72089 74.49419 73.82267
## 25 73.49404 75.34304 73.78542 73.82044 73.89981
## 26 74.71267 73.92644 73.96237 73.31834 73.67573
## 27 74.06016 73.41111 74.53150 72.48161 74.30656
## 28 72.46355 73.69902 73.47172 73.34791 73.94310
## 29 72.08564 75.17658 72.33503 73.53647 72.88408
## 30 73.24918 76.08717 74.01740 72.71370 72.35939
## 31 74.45019 73.98144 73.68193 73.07064 72.51254
## 32 72.92481 75.00003 73.37873 72.61557 75.86929
## 33 74.42510 73.76135 75.05848 74.88642 73.38076
## 34 76.20610 73.74497 72.57551 73.85560 74.20754
## 35 76.30798 74.10580 74.45700 73.92285 73.66600
## 36 73.96527 74.78764 76.07525 75.02739 75.20791
## 37 72.76868 74.98390 74.21992 72.53275 74.52102
## 38 73.84125 75.46459 73.23392 73.56979 73.07389
## 39 73.82290 74.40201 73.26825 74.83037 72.79192
## 40 72.95202 75.44116 72.98415 74.41197 73.61892
#xbar chart and diameter
obj1 <- qcc(diameter[1:25,], type="xbar", newdata=diameter[26:40,])

summary(obj1)
## 
## Call:
## qcc(data = diameter[1:25, ], type = "xbar", newdata = diameter[26:40,     ])
## 
## xbar chart for diameter[1:25, ] 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   73.40   73.90   74.08   74.11   74.24   75.09 
## 
## Group sample size:  5
## Number of groups:  25
## Center of group statistics:  74.10806
## Standard deviation:  0.8854026 
## 
## Summary of group statistics in diameter[26:40, ]:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   73.20   73.72   73.84   73.91   74.04   75.01 
## 
## Group sample size:  5
## Number of groups:  15 
## 
## Control limits:
##       LCL      UCL
##  72.92017 75.29596
#R chart and summary
obj2 <- qcc(diameter[1:25,],type="R")

summary(obj2)
## 
## Call:
## qcc(data = diameter[1:25, ], type = "R")
## 
## R chart for diameter[1:25, ] 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.881   1.386   1.864   2.059   2.702   3.978 
## 
## Group sample size:  5
## Number of groups:  25
## Center of group statistics:  2.059446
## Standard deviation:  0.8854026 
## 
## Control limits:
##  LCL      UCL
##    0 4.354637
##X-bar chart for diameter
qcc(diameter, type = "xbar")

## List of 11
##  $ call      : language qcc(data = diameter, type = "xbar")
##  $ type      : chr "xbar"
##  $ data.name : chr "diameter"
##  $ data      : num [1:40, 1:5] 73.4 73.2 75.5 74 74.9 ...
##   ..- attr(*, "dimnames")=List of 2
##  $ statistics: Named num [1:40] 74.1 74.1 74 74.5 74.1 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ sizes     : Named int [1:40] 5 5 5 5 5 5 5 5 5 5 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ center    : num 74
##  $ std.dev   : num 0.944
##  $ nsigmas   : num 3
##  $ limits    : num [1, 1:2] 72.8 75.3
##   ..- attr(*, "dimnames")=List of 2
##  $ violations:List of 2
##  - attr(*, "class")= chr "qcc"
##R-chart for diameter
qcc(diameter, type="R")

## List of 11
##  $ call      : language qcc(data = diameter, type = "R")
##  $ type      : chr "R"
##  $ data.name : chr "diameter"
##  $ data      : num [1:40, 1:5] 73.4 73.2 75.5 74 74.9 ...
##   ..- attr(*, "dimnames")=List of 2
##  $ statistics: Named num [1:40] 2.431 1.559 3.726 0.989 2.908 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ sizes     : Named int [1:40] 5 5 5 5 5 5 5 5 5 5 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ center    : num 2.2
##  $ std.dev   : num 0.944
##  $ nsigmas   : num 3
##  $ limits    : num [1, 1:2] 0 4.64
##   ..- attr(*, "dimnames")=List of 2
##  $ violations:List of 2
##  - attr(*, "class")= chr "qcc"
#S-chart for diameter
qcc(diameter, type="S")

## List of 11
##  $ call      : language qcc(data = diameter, type = "S")
##  $ type      : chr "S"
##  $ data.name : chr "diameter"
##  $ data      : num [1:40, 1:5] 73.4 73.2 75.5 74 74.9 ...
##   ..- attr(*, "dimnames")=List of 2
##  $ statistics: Named num [1:40] 0.961 0.669 1.499 0.465 1.201 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ sizes     : Named int [1:40] 5 5 5 5 5 5 5 5 5 5 ...
##   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
##  $ center    : num 0.88
##  $ std.dev   : num 0.937
##  $ nsigmas   : num 3
##  $ limits    : num [1, 1:2] 0 1.84
##   ..- attr(*, "dimnames")=List of 2
##  $ violations:List of 2
##  - attr(*, "class")= chr "qcc"
#############################New_Part-2#####################################################
mu = 74
n  =1
k = 20
s = 1
data1 = rnorm(n*k, mu, s)
qcc(data1, type = "xbar.one")

## List of 11
##  $ call      : language qcc(data = data1, type = "xbar.one")
##  $ type      : chr "xbar.one"
##  $ data.name : chr "data1"
##  $ data      : num [1:20, 1] 74.4 75.7 75.6 73.7 71.7 ...
##   ..- attr(*, "dimnames")=List of 2
##  $ statistics: Named num [1:20] 74.4 75.7 75.6 73.7 71.7 ...
##   ..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
##  $ sizes     : int [1:20] 1 1 1 1 1 1 1 1 1 1 ...
##  $ center    : num 74.3
##  $ std.dev   : num 1.14
##  $ nsigmas   : num 3
##  $ limits    : num [1, 1:2] 70.9 77.7
##   ..- attr(*, "dimnames")=List of 2
##  $ violations:List of 2
##  - attr(*, "class")= chr "qcc"
##Process Capability Analysis
process.capability(obj1,spec.limit=c(71,77))

## 
## Process Capability Analysis
## 
## Call:
## process.capability(object = obj1, spec.limits = c(71, 77))
## 
## Number of obs = 125          Target = 74
##        Center = 74.11           LSL = 71
##        StdDev = 0.8854          USL = 77
## 
## Capability indices:
## 
##       Value    2.5%  97.5%
## Cp    1.129  0.9889  1.270
## Cp_l  1.170  1.0384  1.302
## Cp_u  1.089  0.9649  1.213
## Cp_k  1.089  0.9412  1.236
## Cpm   1.121  0.9812  1.261
## 
## Exp<LSL 0.022%    Obs<LSL 0%
## Exp>USL 0.054%    Obs>USL 0%
###Difference between Variable and attribute characteristic