#######################################################
# Name: Rahul Sachan
#
# Roll No: MA14C035
#
# Subject : Applied Statistics
#Assignment on Quality Control
#
#*******************Assignment**************************************

library(qcc)
## Warning: package 'qcc' was built under R version 3.3.3
## Package 'qcc', version 2.6
## Type 'citation("qcc")' for citing this R package in publications.
rm(list = ls())
##we will use data from given file
d1= read.csv("D:/R/AssignmentCSV.csv")
d2= read.csv("D:/R/AssignmentCSV2.csv")
d3= read.csv("D:/R/AssignmentCSV3.csv")
#Question -1
d1.df=data.frame(d1)
A=matrix(0,90,1)
A=data.frame(A)
for(i in 1:30){
  for(j in 1:3){
    A[3*i-3+j,1]=d1.df[i,j]
  }
}

batch=rep(1:30,each=3)
E=data.frame(A,batch)
dim(E)
## [1] 90  2
d1data=qcc.groups(E$A,E$batch)
d1data
##      [,1]   [,2]   [,3]
## 1  64.010 63.989 63.990
## 2  64.015 64.008 63.993
## 3  63.982 63.984 63.995
## 4  63.988 64.024 64.021
## 5  64.002 63.996 63.993
## 6  63.992 64.007 64.015
## 7  64.009 63.994 63.997
## 8  63.995 64.006 63.994
## 9  63.985 64.003 63.993
## 10 64.008 63.995 64.009
## 11 63.998 74.000 63.990
## 12 63.994 63.998 63.994
## 13 64.004 64.000 64.007
## 14 63.983 64.002 63.998
## 15 64.030 64.002 64.019
## 16 63.995 63.992 64.001
## 17 64.006 63.967 63.994
## 18 64.012 64.014 63.998
## 19 64.000 63.984 64.005
## 20 63.994 64.012 63.986
## 21 64.006 64.010 64.018
## 22 63.984 64.002 64.003
## 23 64.000 64.010 64.013
## 24 63.988 64.001 64.009
## 25 64.004 63.999 63.990
## 26 63.998 58.000 63.990
## 27 63.994 63.998 63.994
## 28 64.004 64.000 64.007
## 29 63.983 64.002 63.998
## 30 64.030 64.002 64.019
#part-A
# Calculate the control limits for Xbar and R charts

obj1=qcc(d1data,type ="xbar")

summary(obj1)
## 
## Call:
## qcc(data = d1data, type = "xbar")
## 
## xbar chart for d1data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   62.00   64.00   64.00   64.04   64.01   67.33 
## 
## Group sample size:  3
## Number of groups:  30
## Center of group statistics:  64.04474
## Standard deviation:  0.324808 
## 
## Control limits:
##       LCL      UCL
##  63.48216 64.60733
obj2<-qcc(d1data,type ="R")

summary(obj2)
## 
## Call:
## qcc(data = d1data, type = "R")
## 
## R chart for d1data 
## 
## Summary of group statistics:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##  0.00400  0.01225  0.01850  0.54990  0.02275 10.01000 
## 
## Group sample size:  3
## Number of groups:  30
## Center of group statistics:  0.5499
## Standard deviation:  0.324808 
## 
## Control limits:
##  LCL      UCL
##    0 1.415549
# part-B
##Determine the control limits for Xbar and S charts and draw the charts.

obj3 <-qcc(d1data,type ="S")

summary(obj3)
## 
## Call:
## qcc(data = d1data, type = "S")
## 
## S chart for d1data 
## 
## Summary of group statistics:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.002309 0.006695 0.009518 0.316500 0.011800 5.777000 
## 
## Group sample size:  3
## Number of groups:  30
## Center of group statistics:  0.3164702
## Standard deviation:  0.3570984 
## 
## Control limits:
##  LCL       UCL
##    0 0.8127492
#part- C
##No, the process is not under the control.
newdata=E[-c(11,26),]
d1newdata=qcc.groups(newdata$A,newdata$batch)
obj4=qcc(d1newdata,type = "xbar")

summary(obj4)
## 
## Call:
## qcc(data = d1newdata, type = "xbar")
## 
## xbar chart for d1newdata 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   62.00   64.00   64.00   64.04   64.00   67.33 
## 
## Summary of group sample sizes:              
##   sizes  2  3
##   counts 2 28
## 
## Number of groups:  30
## Center of group statistics:  64.04545
## Standard deviation:  0.3249564 
## 
## Control limits:
##          LCL     UCL
##     63.48261 64.6083
##     63.48261 64.6083
## ...                 
##     63.48261 64.6083
obj5<-qcc(d1newdata,type ="R")

summary(obj5)
## 
## Call:
## qcc(data = d1newdata, type = "R")
## 
## R chart for d1newdata 
## 
## Summary of group statistics:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##  0.00400  0.01200  0.01750  0.54950  0.02275 10.01000 
## 
## Summary of group sample sizes:              
##   sizes  2  3
##   counts 2 28
## 
## Number of groups:  30
## Center of group statistics:  0.5614886
## Standard deviation:  0.3249564 
## 
## Control limits:
##     LCL      UCL
##       0 1.427533
##       0 1.427533
## ...             
##       0 1.427533
obj6 <-qcc(d1newdata,type ="S")

summary(obj6)
## 
## Call:
## qcc(data = d1newdata, type = "S")
## 
## S chart for d1newdata 
## 
## Summary of group statistics:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.002309 0.006247 0.009367 0.316500 0.011800 5.777000 
## 
## Summary of group sample sizes:              
##   sizes  2  3
##   counts 2 28
## 
## Number of groups:  30
## Center of group statistics:  0.3233332
## Standard deviation:  0.357219 
## 
## Control limits:
##     LCL       UCL
##       0 0.8197798
##       0 0.8197798
## ...              
##       0 0.8197798
# part-D
# hence Avg = 64.04545 and Std Dev : 0.3249564
# part-E
process.capability(obj4,spec.limits=c(63.98,64.02))

## 
## Process Capability Analysis
## 
## Call:
## process.capability(object = obj4, spec.limits = c(63.98, 64.02))
## 
## Number of obs = 88           Target = 64
##        Center = 64.05           LSL = 63.98
##        StdDev = 0.325           USL = 64.02
## 
## Capability indices:
## 
##          Value      2.5%     97.5%
## Cp     0.02052  0.017470   0.02356
## Cp_l   0.06714  0.008098   0.12619
## Cp_u  -0.02611  0.032427  -0.08465
## Cp_k  -0.02611  0.043642  -0.09586
## Cpm    0.02032  0.017291   0.02334
## 
## Exp<LSL 42%   Obs<LSL 2.3%
## Exp>USL 53%   Obs>USL 4.5%
#*******************************************************************************
#Question-2
# part-A. 
##Artibute chart we will use. Use C chart as well
#part-B 
## control limits and plot the chart.
obj7=qcc(d2,sizes = 500,type = "c")

#part-c
# remove 17
d2=d2[-c(17),]
obj8=qcc(d2,sizes = 500,type = "c")

##***************************************************************************

#Question- 3
## create three control charts with this data to monitor each machine.
d3=data.frame(d3)
m1data=qcc.groups(d3$Machine.1,d3$Subgroup.ID)
m2data=qcc.groups(d3$Machine.2,d3$Subgroup.ID)
m3data=qcc.groups(d3$Machine.3,d3$Subgroup.ID)

obj9=qcc(m1data,type = "xbar")

summary(obj9)
## 
## Call:
## qcc(data = m1data, type = "xbar")
## 
## xbar chart for m1data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   598.2   599.5   600.0   600.1   600.6   601.4 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  600.072
## Standard deviation:  1.16939 
## 
## Control limits:
##       LCL      UCL
##  598.5031 601.6409
obj10=qcc(m2data,type = "xbar")

summary(obj10)
## 
## Call:
## qcc(data = m2data, type = "xbar")
## 
## xbar chart for m2data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   599.0   599.3   599.5   599.5   599.7   600.2 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  599.548
## Standard deviation:  0.5846948 
## 
## Control limits:
##       LCL      UCL
##  598.7635 600.3325
obj11=qcc(m3data,type = "xbar")

summary(obj11)
## 
## Call:
## qcc(data = m3data, type = "xbar")
## 
## xbar chart for m3data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   598.1   599.5   600.1   600.2   600.8   603.0 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  600.23
## Standard deviation:  1.599312 
## 
## Control limits:
##       LCL      UCL
##  598.0843 602.3757
obj12=qcc(m1data,type = "R")

summary(obj12)
## 
## Call:
## qcc(data = m1data, type = "R")
## 
## R chart for m1data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.20    2.60    2.80    2.72    3.30    4.00 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  2.72
## Standard deviation:  1.16939 
## 
## Control limits:
##  LCL      UCL
##    0 5.751358
obj13=qcc(m2data,type = "R")

summary(obj13)
## 
## Call:
## qcc(data = m2data, type = "R")
## 
## R chart for m2data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.60    1.00    1.20    1.36    1.65    2.60 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  1.36
## Standard deviation:  0.5846948 
## 
## Control limits:
##  LCL      UCL
##    0 2.875679
obj14=qcc(m3data,type = "R")

summary(obj14)
## 
## Call:
## qcc(data = m3data, type = "R")
## 
## R chart for m3data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.80    2.20    3.50    3.72    4.85    7.40 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  3.72
## Standard deviation:  1.599312 
## 
## Control limits:
##  LCL      UCL
##    0 7.865827
obj15=qcc(m1data,type = "S")

summary(obj15)
## 
## Call:
## qcc(data = m1data, type = "S")
## 
## S chart for m1data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1095  1.0620  1.1810  1.1480  1.4810  1.6150 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  1.147503
## Standard deviation:  1.220767 
## 
## Control limits:
##  LCL      UCL
##    0 2.397131
obj16=qcc(m2data,type = "S")

summary(obj16)
## 
## Call:
## qcc(data = m2data, type = "S")
## 
## S chart for m2data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.3033  0.4117  0.5099  0.5440  0.6697  1.0060 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  0.5440291
## Standard deviation:  0.5787632 
## 
## Control limits:
##  LCL      UCL
##    0 1.136476
obj17=qcc(m3data,type = "S")

summary(obj17)
## 
## Call:
## qcc(data = m3data, type = "S")
## 
## S chart for m3data 
## 
## Summary of group statistics:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.7266  0.9583  1.4830  1.5370  2.0090  2.6590 
## 
## Group sample size:  5
## Number of groups:  20
## Center of group statistics:  1.537047
## Standard deviation:  1.635181 
## 
## Control limits:
##  LCL      UCL
##    0 3.210888