moncom<-function(z,t,lambda,alpha1,n1,n2,b)
{
# x and z's are created outside
#n1--replications, n2--sample size
# create vectors of 1000*1 to store the estimate for alpha1.
#s1--cv, s2-chesher, s3-2sqq, s4-2smq, s5-2slq, s6-qr; l-least square, m-median, q-quantile

s1<-rep(0,n1)
s2<-rep(0,n1)
s3<-rep(0,n1)
s4<-rep(0,n1)
s5<-rep(0,n1)
s6<-rep(0,n1)

set.seed(999)


# the loop.
for(i in 1:n1) {
     
     nu<-rnorm(n2,0,.5)
     epsilon<-rnorm(n2,0,1)
    
     
     Y= 1 + 2*x + 3*z + nu 
     y= 3 + 4*x+ alpha1*Y + Y*b*(lambda*nu + epsilon)
     
     # QR results for alpha1
     s6[i]<-rq(y~Y+x,tau=t)$coef[2,1]
     
     # 2SLQ results for alpha1: coef
     Yhat<-fitted(lm(Y~z+x))
     s4[i]<-rq(y~Yhat+x,tau=t)$coef[2,1]
      
     #2SMQ results
     fit0<-rq(Y~z+x,tau=.5)
     Ym<-fit0$coef[1,1] + fit0$coef[2,1]*z+ fit0$coef[3,1]*x
     s4[i]<-rq(y~Ym + x,tau=t)$coef[2,1]
     
     
     # 2SQQ results for alpha1: coef,  s.d., and MSE.
     fit1<-rq(Y~z+x,tau=t)
     Ystar<-fit1$coef[1,1] + fit1$coef[2,1]*z+ fit1$coef[3,1]*x
     s3[i]<-rq(y~Ystar + x,tau=t)$coef[2,1]
     
    
     # Chesher's result---WAD result
     Y1<-Ystar
     Y2<-Y^2
     Yz<-Y*z
     Yx<-Y*x
     fit2<-rq(y~Y+Y2+Yz+Yx+x,tau=t)
     s2[i]<-mean(fit2$coef[2,1] + fit2$coef[3,1]*2*Y1 + fit2$coef[4,1]*z + fit2$coef[5,1]*x+ fit2$coef[4,1]*Y1/(fit1$coef[2,1]))

     # CV estimator
     nuhat=Y-Ystar
     Ynuhat=Y*nuhat
     s1[i]<-rq(y~Y+ x+Ynuhat,tau=t)$coef[2,1]
    
     }

     # theoretical results for alpha1(\tau) .
     theb1<- alpha1 + (lambda*qnorm(t, 0, .5) + qnorm(t,0,1))*b
         
    # the average results of n1 times regression:mean,bias, std.error, MSE.
      
      p1<- mean(s1)
      p2<- p1 - theb1
      p3<- sqrt(var(s1))
      p4<- sqrt(p2^2+var(s1))
      
      q1<- mean(s2)
      q2<- q1 - theb1
      q3<- sqrt(var(s2))
      q4<- sqrt(q2^2 + var(s2))
      
      r1<- mean(s3)
      r2<- r1-theb1
      r3<- sqrt(var(s3))
      r4<- sqrt(r2^2+var(s3))
      
      t1<- mean(s4)
      t2<- t1-theb1
      t3<- sqrt(var(s4))
      t4<- sqrt(t2^2+var(s4))
      
      u1<-mean(s5)
      u2<-u1-theb1
      u3<-sqrt(var(s5))
      u4<-sqrt(u2^2+var(s5))
      
      v1<-mean(s6)
      v2<-v1-theb1
      v3<-sqrt(var(s6))
      v4<-sqrt(v2^2 + var(s6))
           
      # lsit the results if tau is not 0.5.
       
          coef1<-matrix(0,7,4)
      
          coef1[1,1]<-theb1
          coef1[1,2]<-0
          coef1[1,3]<-0
          coef1[1,4]<-0
      
          coef1[2,1]<-p1
          coef1[2,2]<-p2
          coef1[2,3]<-p3
          coef1[2,4]<-p4
      
          coef1[3,1]<-q1
          coef1[3,2]<-q2
          coef1[3,3]<-q3
          coef1[3,4]<-q4
          
          coef1[4,1]<-r1
          coef1[4,2]<-r2
          coef1[4,3]<-r3
          coef1[4,4]<-r4
      
          coef1[5,1]<-t1
          coef1[5,2]<-t2
          coef1[5,3]<-t3
          coef1[5,4]<-t4
          
          coef1[6,1]<-u1
          coef1[6,2]<-u2
          coef1[6,3]<-u3
          coef1[6,4]<-u4
          
          coef1[7,1]<-v1
          coef1[7,2]<-v2
          coef1[7,3]<-v3
          coef1[7,4]<-v4
          
      return (coef1)    

}


moncomb<-function(z,lambda,alpha1,n1,n2,b)
{
#this function yields the table2 for CV, chesher, 2sqr 2spq estimates.
#scale-location model is used in moncom(.).
coef<-matrix(0,35,4)
tau<-c(0.1,0.3,0.5,0.7,0.9)
coefa<- moncom(z,.1,lambda,alpha1,n1,n2,b)
coefb<- moncom(z,.3,lambda,alpha1,n1,n2,b)
coefc<- moncom(z,.5,lambda,alpha1,n1,n2,b)
coefd<- moncom(z,.7,lambda,alpha1,n1,n2,b)
coefe<- moncom(z,.9,lambda,alpha1,n1,n2,b)
coef<-rbind(coefa, coefb,coefc,coefd,coefe)

methods1<-rbind("Theoretical Value","CV", "Chesher","2SQQ","2SMQ", "2SLQ", "QR")
methods<-rbind(methods1, methods1, methods1, methods1, methods1)

dimnames(coef)<-list (methods, c("Coeffcient", "Bias", "Std. Error", "MSE"))

dump("coef", "bbb")
# put into latex.table.
# return(coef)
# latex.table (coef,file="Table2",  rowlabel="", dec=3, rgroup =paste("t = ", tau), n.rgroup=c(5,5,5,5,5), label="", caption="Case (B): b2 = 1")

}
