*
* RANDOMIZE.RPF
* Example of approximate randomization
*
* RATS User's Guide, Example from Section 16.10
*
open data hprice1.raw
data(format=free,org=columns) 1 88 price assess bdrms lotsize sqrft $
colonial lprice lassess llotsize lsqrft
*
* This examines heteroscedasticity in a hedonic price regression based
* upon lot size.
*
order(ranks=lotranks) lotsize
linreg price
# constant lotsize sqrft bdrms
*
* The first test statistic is the ratio between the sum of the residuals
* squared over the final 36 observations (large lot size) to that over
* the first 36 (small lots), skipping the middle 16. The second is the
* correlation between the ranks of the lot size and the squared residual.
*
set ressqr = %resids^2
sstats / ressqr*(lotranks<=36)>>sum1 ressqr*(lotranks>=53)>>sum2
compute refer1=sum2/sum1
order(rank=vrank) ressqr
compute refer2=%corr(lotranks,vrank)
*
* count1 and count2 are the number of times we get a more extreme value
* after reshuffling. We do 999 shuffles.
*
compute count1=count2=0.0
compute ns=999
do draw=1,ns
*
* Use BOOT with NOREPLACE to come up with a new permutation of the
* ressqr variable. Recompute the test statistic for these.
*
boot(noreplace) entries 1 88
set shuffle = ressqr(entries(t))
*
sstats / shuffle*(lotranks<=36)>>sum1 shuffle*(lotranks>=53)>>sum2
compute teststat=sum2/sum1
compute count1=count1+(teststat>refer1)
*
order(rank=vrank) shuffle
compute teststat=%corr(lotranks,vrank)
compute count2=count2+(teststat>refer2)
end do draw
*
* The p-values for the tests are computed by taking (count+1)/(draws+1).
* The +1's are needed because we are, in effect, adding the actual
* sample in with the draws and seeing where it ends up.
*
?"Test 1, p-value" (count1+1)/(ns+1)
?"Test 2, p-value" (count2+1)/(ns+1)