Vous êtes sur la page 1sur 7

> tim<- proc.

time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-25
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)
+ beta=lm(y~x1+x2,w=mu )
+ b[i,]=beta$coefficients
+ #for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
+ }
> #data
> #performances of the estmators
> MEV1=mean(b[,1])
> MEV2=mean(b[,2])
> MEV3=mean(b[,3])
> var1=(sd(b[,1]))^2
> var2=(sd(b[,2]))^2
> var3=(sd(b[,3]))^2
> bias1=MEV1-b1
> bias2=MEV2-b2
> bias3=MEV3-b3
> mse1=bias1^2+var1
> mse2=bias2^2+var2
> mse3=bias3^2+var3
> cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 65.38097 -1.826066 -4.09073 2963.937 11.51144 31.3297
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 7238.608 14.84596 48.06378
> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 28.75584 -4.360259e-16 4.424249e-16 94.13682 -1.826066 -4.09073
> proc.time()-tim
user system elapsed
20.50
0.25 21.44

>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>
###50
> tim<- proc.time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-50
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)
+ beta=lm(y~x1+x2,w=mu )
+ b[i,]=beta$coefficients
+ #for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
+ }
> #data
> #performances of the estmators
> MEV1=mean(b[,1])
> MEV2=mean(b[,2])
> MEV3=mean(b[,3])
> var1=(sd(b[,1]))^2
> var2=(sd(b[,2]))^2
> var3=(sd(b[,3]))^2
> bias1=MEV1-b1
> bias2=MEV2-b2
> bias3=MEV3-b3
> mse1=bias1^2+var1
> mse2=bias2^2+var2
> mse3=bias3^2+var3
> cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 45.31409 1.479928 -9.18125 8121.466 43.89367 12.78669
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 10174.83 46.08386 97.08204

> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 28.49998 1.54249e-16 1.14157e-16 73.81407 1.479928 -9.18125
> proc.time()-tim
user system elapsed
20.93
0.23 21.17
>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>
###100
> tim<- proc.time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-100
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)
+ beta=lm(y~x1+x2,w=mu )
+ b[i,]=beta$coefficients
+ #for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
+ }
> #data
> #performances of the estmators
> MEV1=mean(b[,1])
> MEV2=mean(b[,2])
> MEV3=mean(b[,3])
> var1=(sd(b[,1]))^2
> var2=(sd(b[,2]))^2
> var3=(sd(b[,3]))^2
> bias1=MEV1-b1
> bias2=MEV2-b2
> bias3=MEV3-b3
> mse1=bias1^2+var1
> mse2=bias2^2+var2
> mse3=bias3^2+var3

> cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 91.32635 -6.608258 0.09458111 1777.054 7.063396 5.582172
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 10117.56 50.73247 5.591118
> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 28.86629 1.401209e-16 1.868045e-16 120.1926 -6.608258 0.09458111
> proc.time()-tim
user system elapsed
22.27
0.67 22.94
>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>
###200
> tim<- proc.time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-200
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)
+ beta=lm(y~x1+x2,w=mu )
+ b[i,]=beta$coefficients
+ #for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
+ }
> #data
> #performances of the estmators
> MEV1=mean(b[,1])
> MEV2=mean(b[,2])
> MEV3=mean(b[,3])
> var1=(sd(b[,1]))^2
> var2=(sd(b[,2]))^2
> var3=(sd(b[,3]))^2

>
>
>
>
>
>
>

bias1=MEV1-b1
bias2=MEV2-b2
bias3=MEV3-b3
mse1=bias1^2+var1
mse2=bias2^2+var2
mse3=bias3^2+var3
cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 79.50837 -4.243682 -3.797147 803.1298 2.376181 6.908739
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 7124.711 20.38502 21.32707
> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 28.97558 2.820584e-16 -1.428629e-16 108.484 -4.243682 -3.797147
> proc.time()-tim
user system elapsed
25.14
0.61 25.75
>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>
###500
> tim<- proc.time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-500
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)
+ beta=lm(y~x1+x2,w=mu )
+ b[i,]=beta$coefficients
+ #for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
+ }
> #data
> #performances of the estmators

>
>
>
>
>
>
>
>
>
>
>
>
>

MEV1=mean(b[,1])
MEV2=mean(b[,2])
MEV3=mean(b[,3])
var1=(sd(b[,1]))^2
var2=(sd(b[,2]))^2
var3=(sd(b[,3]))^2
bias1=MEV1-b1
bias2=MEV2-b2
bias3=MEV3-b3
mse1=bias1^2+var1
mse2=bias2^2+var2
mse3=bias3^2+var3
cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 0.1180906 0.8939252 -2.246839 157.5312 1.691706 4.026438
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 157.5452 2.490808 9.074722
> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 29.05819 7.237553e-18 -2.709751e-18 29.17628 0.8939252 -2.246839
> proc.time()-tim
user system elapsed
35.18
0.19 35.81
>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>
###1000
> tim<- proc.time()
>
> #R program for Random Walk MH sampling of parameters in heteroscedastic linear
model
> #ols LEAST SQUARES
> #read in data, establish x and y matrices
> set.seed(1234)
> m = 10^4
> n <-1000
> #covariates
> x1<-runif(n,11,15)
> x2<-runif(n,4,8)
>
> delta=0 #scale of heteroscedasticity subject to change
> #mu<- ((-2+0.25*x1+x2))
> mu<- ((-2+0.25*x1+x2))^delta
>
> g=10+x1+x2
> y<- (rnorm(n,mean(g),sd(log(mu))))
> bols=lm(y~x1+x2,w=mu)
> b1=bols$coefficients[1]
> b2=bols$coefficients[2]
> b3=bols$coefficients[3]
>
> #establish parameter vectors, proposal scales and acceptance rates
> b=matrix(0,m,3)
> data=cbind(y,1,x1,x2)
> #############simul
> for ( i in 1:m) {
+ y=y+rnorm(n)

+
+
+
+
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

beta=lm(y~x1+x2,w=mu )
b[i,]=beta$coefficients
#for(i%10==0) print{i,b[i,]} #cat("Iteration",i,b[i,],date(),"\n")
}
#data
#performances of the estmators
MEV1=mean(b[,1])
MEV2=mean(b[,2])
MEV3=mean(b[,3])
var1=(sd(b[,1]))^2
var2=(sd(b[,2]))^2
var3=(sd(b[,3]))^2
bias1=MEV1-b1
bias2=MEV2-b2
bias3=MEV3-b3
mse1=bias1^2+var1
mse2=bias2^2+var2
mse3=bias3^2+var3
cbind(bias1,bias2,bias3,var1,var2,var3)
bias1
bias2
bias3
var1
var2
var3
(Intercept) 9.617094 -0.9588608 0.2818664 73.58316 0.5002411 0.5568787
> cbind(mse1,mse2,mse3)
mse1
mse2
mse3
(Intercept) 166.0717 1.419655 0.6363274
> cbind(b1,b2,b3,MEV1,MEV2,MEV3)
b1
b2
b3
MEV1
MEV2
MEV3
(Intercept) 28.97425 2.220122e-16 -2.127761e-16 38.59134 -0.9588608 0.2818664
> proc.time()-tim
user system elapsed
52.63
0.61 53.24
>
> write.csv(cbind(bias1,bias2,bias3,var1,var2,var3,mse1,mse2,mse3,b1,b2,b3,MEV1,
MEV2,MEV3),"wole.csv")
>
>

Vous aimerez peut-être aussi