I want to compare adjusted R2 across two models. To be more specific, assume that using auto data, I want to test the differences in the ability of weight to explain the price for domestic cars compared with that of foreign cars. I want to follow the steps below:
(A) get the actual difference in R2. This should be straightforward as follows: (The observed difference is 33% )
Code:
sysuse auto, clear regress price weight if foreign==1 regress price weight if foreign==0
(B) shuffle the foreign dummy variable randomly across all cars.
(C) re-estimate the regressions from step A (both domestic and foreign cars) based on the reshuffled variable constructed in step B and record the difference in R2 values from these regressions,
(D) note if the difference in R2 from the randomly generated samples is larger than the observed difference in R2 of 33%.
Repeat the steps (B to C) 1000 times and the get p-value as the number of times that the randomly generated difference in R2 is larger than the actual/observed difference in R2 divided by the number of iterations.
Please see my try below:
Code:
sysuse auto, clear regress price weight if foreign==1 regress price weight if foreign==0 local obs_r2=`e(r2)' tempname compare tempfile results postfile `compare' r2_1 r2_2 using "`results'" forvalues i=1/1000{ shufflevar foreign quietly regress price weight if foreign_shuffled==1 local topost `topost' (`e(r2_a)') quietly regress price weight if foreign_shuffled==0 local topost `topost' (`e(r2_a)') } postclose `compare' use "`results'", clear sum r2_1 r2_2 count if r2_2- r2_1>0.33
Will be great if anyone could help why this is not working?
Thanks!
0 Response to compare and test "r2_a" across two regressions
Post a Comment