Pregunta

I want to simulate heteroskedasticity and see how White's test performs for various sample sizes (this is similar to Greene, but testing different kinds).

Now, the model I wish to test is:

Replications=1000=n 
y=1.5x+ e 
x ~ N(25, 15) 
e ~ N(0, sigma_i) 
sigma_i= 1+ alpha (sqrt(x[i]^2) 

And say initially sample size=20 (varying this part is easy)

So, using the bstats command for the white test, I wrote my code as

for (j in 1:n) {
  for (i in 1:20){
    x[i]=rnorm(1, 25, 15)
    sigma [i]=1+0*sqrt(x[i]^2)
    epsilon[i]=rnorm(1, 0, sigma[i])
    y[i]=1.5*x[i]+epsilon[i]}
  lm1[j]<- lm(y~x); white.test(lm1[j])
  if (white.test(lm1)$p.value > 0.05){ind=ind
  }else{
    if (white.test(lm1)$p.value < 0.05) {ind=ind+1}
  } 
  IND1=ind/1000}
} 

Now, I know this is a problem because the i section works, but I can't get the outer for loop to work. Does anyone have any suggestions?

¿Fue útil?

Solución

require(tseries)
# if you dont have above package, please install it
# it can be done by running following code
# install.packages('tseries')

########### function that simulates 100 times for hetro- 
test_gen=function(n,m){
  esp=0
  saved=0
  for(i in 1:m){   # simulate  100 times
    X=rnorm(n,25,1)
    for(j in 1:length(X)){
    eps[j]=rnorm(1,0,sqrt((X[j])^2)[1])
    }
    Y=1.5*X+eps

    temp=white.test(X,Y)

    saved[i]=temp$p.value
  }
  return(saved)
}

#red dots are the values less than 0.05
#n controls the size of sample
#m controls numbers of simulation
n=20
m=100
out=test_gen(n,m)
plot(c(1:length(out)),out,main="p-value(whitetest) for each simulated data") 
ind=out<0.05
points(c(1:length(out))[ind],out[ind],col='red',pch=16)

plot

out2=0
#simulate 100 times for each specific data size 
for(i in 20:100){
  k=i-19
  temp=test_gen(i,100)
  out2[k]=sum(temp<0.5)/100
}

plot(20:100,out2,main="error rate",xlab="sample size") # error rate

plot

I feel like I'm doing your homework...anyway good luck

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top