################################################################################################ #####R CODE FOR VIRGINIA TECH DEPARTMENT OF STATISTICS TECHNICAL REPORT NO. 05-7 ##### #####"ROBUST PARAMETER DESIGN: A SEMI-PARAMETRIC APPROACH" ##### #####STEPHANIE M. PICKLE, TIMOTHY J. ROBINSON, JEFFREY B. BIRCH, CHRISTINE M. ANDERSON-COOK##### ################################################################################################ #The following code will perform the parametric, nonparametric, and semi-parametric approach to the printing ink data# #Read Ink Data in from a file #Note that data is coded such that each regressor is between 0 and 1 ink.data.coded<-read.table('FileName',sep=",",header=TRUE) #Create separate vectors for each column of data matrix x1<-ink.data.coded[,1] #x1=speed x2<-ink.data.coded[,2] #x2=pressure x3<-ink.data.coded[,3] #x3=distance X.design<-cbind(x1,x2,x3) #design matrix ybar<-ink.data.coded[,4] #mean response s<-ink.data.coded[,5] #standard deviation t<-ink.data.coded[,6] #transformation of standard deviation = log(s^2+1) ############################################################################################################################# ############################################################################################################################# ############################################################################################################################# ############################# #####PARAMETRIC APPROACH##### ############################# #The following code will estimate responses parametrically using OLS for the transformed variance and EWLS for the mean# ####################### #Preliminary Functions# ####################### #Leave one out Cross-validation yhat.minusi.p.fn<-function(y,res,H){ n<-length(y) h<-diag(H) #grabs diagonals from H yhat.minusi<-rep(0,n) for(i in 1:n){ yhat.minusi[i]<-y[i]-(res[i]/(1-h[i])) } return(yhat.minusi) } obj.p.fn<-function(beta.mean,beta.t,theta,x0){ #This function will find the estimated MSE=(yhat-theta)^2+varhat #at the location x0 based on the estimated ols functions for the #transformed variance and wls functions for mean #here the mean model is second order #and the t model is first order x1.x2<-x0[1]*x0[2] x1.x3<-x0[1]*x0[3] x2.x3<-x0[2]*x0[3] x1.x1<-x0[1]*x0[1] x2.x2<-x0[2]*x0[2] x3.x3<-x0[3]*x0[3] modelx0.mean<-c(1,x0,x1.x2,x1.x3,x2.x3,x1.x1,x2.x2,x3.x3) modelx0.t<-c(1,x0) yhat<-beta.mean%*%modelx0.mean bias<-yhat-theta that<-beta.t%*%modelx0.t varhat<-exp(that)-1 msehat<-bias^2+varhat msehat } ga.p.fn<-function(beta.mean,beta.t,theta){ #This function will perform a Simple Genetic Algorithm #For the objective function estimated MSE=(yhat-theta)^2+var #Where yhat is the estimate of the mean and theta is the target value #And var is the estimate of the variance #var will be found with ols and ybar will be found with wls #Stopping Criteria maxiter<-10000 #set maximum number of iterations satiter<-1000 #set number of iterations during which the best results keep saturating epsln<-1e-8 #smallest gain worth recognizing #GA Parameters m<-50 #population size at each generation mutrate<-.2 #mutation rate crossrate<-.9 #crossover rate zerorate<-.2 #zero rate extremerate<-.2 #extreme rate keep<-2 #keep top two from each generation to remain unchanged num.parents<-m-keep #number of population that will be used for mating mate<-ceiling((num.parents)/2) #number of matings #Create initial population and initialize 'best result' holders xrange<-matrix(c(0,1,0,1,0,1),2,3) k<-ncol(xrange) elite<-matrix(0,keep,k) #matrix for elite chromosomes to remain unchanged cross<-matrix(0,num.parents,k) #matrix for crossover iter<-0 #generation (iteration) counter stopcode<-0 inarow<-0 #number of iterations with function's value consecutively less than epsln bestfun<-1e30 #essential positive infinity bestx<-matrix(0,1,k) f<-rep(0,m) #initialize function value for each of m vectors G<-matrix(runif(m*k,0,1),m,k) #initial generation with population size m #####Iterate through generations##### while(stopcode==0){ iter<-iter+1 #increments counter if(iter>maxiter) stopcode<-2 #loop will exit on stopcode=2 for exceeding the maximum number of iterations #Evaluate current generation for(i in 1:m){ f[i]<-obj.p.fn(beta.mean,beta.t,theta,G[i,]) } mat<-cbind(G,f) #create matrix of x locations and corresponding function values newmat<-mat[order(mat[,4]),] #sort matrix of by function values increasing minf<-newmat[1,4] #optimal function (minimum) bf<-min(minf, bestfun) fgain<-bestfun-bf #fgain is always non-negative it measure the change if(fgain>epsln) inarow<-0 else inarow<-(inarow+1) if(fgain>0){ bestfun<-bf bestx<-newmat[1,1:3] #optimal x location } if(inarow>satiter) stopcode<-1 #loop will exit on stopcode=1 for best result having been achieved #Select elite to remain unchanged elite<-newmat[1:keep,1:3] #Select parents for mating cross<-newmat[(keep+1):m,1:3] #Do Crossover for(i in 1:mate){ z<-rbinom(1,1,crossrate) if(z==1){ x<-ceiling(runif(1,0,(k-1))) temp<-cross[i,(x+1):k] cross[i,(x+1):k]<-cross[i+mate,(x+1):k] cross[i+mate,(x+1):k]<-temp } } #Do Mutation M<-matrix(runif((num.parents*k),0,1),num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zz<-rbinom(1,1,mutrate) if(zz==1) cross[i,j]<-M[i,j] } } #Do Zero Gene Operator for(i in 1:num.parents){ for(j in 1:k){ zzz<-rbinom(1,1,zerorate) if(zzz==1) cross[i,j]<-0 } } #Do Extreme Values Operator Extreme<-matrix(1,num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zzzz<-rbinom(1,1,extremerate) if(zzzz==1) cross[i,j]<-Extreme[i,j] } } G<-rbind(elite,cross) } if(itermaxiter) stopcode<-2 #loop will exit on stopcode=2 for exceeding the maximum number of iterations #Evaluate current generation for(i in 1:m){ f[i]<-obj.llr.fn(X,y,theta,b1,d1,t,b2,d2,G[i,],W) } mat<-cbind(G,f) #create matrix of x locations and corresponding function values newmat<-mat[order(mat[,4]),] #sort matrix of by function values increasing minf<-newmat[1,4] #optimal function (minimum) bf<-min(minf, bestfun) fgain<-bestfun-bf #fgain is always non-negative it measure the change if(fgain>epsln) inarow<-0 else inarow<-(inarow+1) if(fgain>0){ bestfun<-bf bestx<-newmat[1,1:3] #optimal x location } if(inarow>satiter) stopcode<-1 #loop will exit on stopcode=1 for best result having been achieved #Select elite to remain unchanged elite<-newmat[1:keep,1:3] #Select parents for mating cross<-newmat[(keep+1):m,1:3] #Do Crossover for(i in 1:mate){ z<-rbinom(1,1,crossrate) if(z==1){ x<-ceiling(runif(1,0,(k-1))) temp<-cross[i,(x+1):k] cross[i,(x+1):k]<-cross[i+mate,(x+1):k] cross[i+mate,(x+1):k]<-temp } } #Do Mutation M<-matrix(runif((num.parents*k),0,1),num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zz<-rbinom(1,1,mutrate) if(zz==1) cross[i,j]<-M[i,j] } } #Do Zero Gene Operator for(i in 1:num.parents){ for(j in 1:k){ zzz<-rbinom(1,1,zerorate) if(zzz==1) cross[i,j]<-0 } } #Do Extreme Values Operator Extreme<-matrix(1,num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zzzz<-rbinom(1,1,extremerate) if(zzzz==1) cross[i,j]<-Extreme[i,j] } } G<-rbind(elite,cross) } if(itermaxiter) stopcode<-2 #loop will exit on stopcode=2 for exceeding the maximum number of iterations #Evaluate current generation for(i in 1:m){ f[i]<-obj.dmrr.fn(X,y,beta.mean,theta,b1,d1,l1,t,beta.t,b2,d2,l2,G[i,]) } mat<-cbind(G,f) #create matrix of x locations and corresponding function values newmat<-mat[order(mat[,4]),] #sort matrix of by function values increasing minf<-newmat[1,4] #optimal function (minimum) bf<-min(minf, bestfun) fgain<-bestfun-bf #fgain is always non-negative it measure the change if(fgain>epsln) inarow<-0 else inarow<-(inarow+1) if(fgain>0){ bestfun<-bf bestx<-newmat[1,1:3] #optimal x location } if(inarow>satiter) stopcode<-1 #loop will exit on stopcode=1 for best result having been achieved #Select elite to remain unchanged elite<-newmat[1:keep,1:3] #Select parents for mating cross<-newmat[(keep+1):m,1:3] #Do Crossover for(i in 1:mate){ z<-rbinom(1,1,crossrate) if(z==1){ x<-ceiling(runif(1,0,(k-1))) temp<-cross[i,(x+1):k] cross[i,(x+1):k]<-cross[i+mate,(x+1):k] cross[i+mate,(x+1):k]<-temp } } #Do Mutation M<-matrix(runif((num.parents*k),0,1),num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zz<-rbinom(1,1,mutrate) if(zz==1) cross[i,j]<-M[i,j] } } #Do Zero Gene Operator for(i in 1:num.parents){ for(j in 1:k){ zzz<-rbinom(1,1,zerorate) if(zzz==1) cross[i,j]<-0 } } #Do Extreme Values Operator Extreme<-matrix(1,num.parents,k) for(i in 1:num.parents){ for(j in 1:k){ zzzz<-rbinom(1,1,extremerate) if(zzzz==1) cross[i,j]<-Extreme[i,j] } } G<-rbind(elite,cross) } if(iter