SLIDE 5 5
9
Plotting (enhanced)
10
Moving Average – R programming
Here we use a created function ”movAve” to calculate
- the moving average of the daily gain
- the upper and lower control limits using the expected mean
(mean1) and standard deviation of the process in control (sd1) movAve <- function(vector,span,nosigma,mean1,sd1) { n <- length(vector); ma <- rep(NA,n); for (i in (span : n)) { x <- c(vector[(i-span+1):i]); ma[i] <- mean(x); } ucl <- mean1 + nosigma*sd1; lcl <- mean1 - nosigma*sd1; return(list(ma=ma,ucl=ucl,lcl=lcl)); } Create an empty data frame to store the results res.ma <- data.frame() Calculate and store the results in the data frame res.ma <- movAve(table$Observed.gain,4,2,750,20) Add the moving average to our table table$ma <- res.ma$ma Finally we can save the new table as csv file for further calculations write.table(table,file=”adg2.csv”)