- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 03-07-2011 02:18 PM
(4883 views)
The following SAS link provides the code to implement Run's test. Does anyone know how to implement this code with a by group option in the data step. In otherwords, is it possible to compute the test statistic simultaneously by an id variable.
Wald-Wolfowitz (or Runs) test for randomness
http://support.sas.com/kb/33/092.html
Wald-Wolfowitz (or Runs) test for randomness
http://support.sas.com/kb/33/092.html
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello DB_ECON,
This is what I've got formally applying BY statement:
[pre]
data one;
do id=1 to 5;
drop i;
do i=1 to 75;
d=rannor(123);
n=75;
output;
end;
end;
run;
proc standard data=one out=two mean=0;
var d;
by ID;
run;
data runcount;
keep ID runs numpos numneg n;
set two end=last;
retain runs 0 numpos 0;
if FIRST.ID then do; runs=0; numpos=0; ld=.; end;
else do; prevpos=( ld GE 0 ); currpos=( D GE 0 ); ld=lag(D); end;
if currpos and prevpos then numpos+1;
else if currpos and ^prevpos then do;
runs+1;
numpos+1;
end;
else if ^currpos and prevpos then runs+1;
if last.ID then do;
numneg=n-numpos;
output;
end;
by ID;
run;
data waldwolf;
label z='Wald-Wolfowitz Z' pvalue='Pr > |Z|';
set runcount;
mu = ( (2*numpos*numneg) / (numpos+numneg) ) + 1;
sigmasq = ( (2*numpos*numneg) * (2*numpos*numneg-numneg-numpos) ) /
( ( (numpos+numneg)**2 ) * (numpos+numneg-1) );
sigma=sqrt(sigmasq);
drop sigmasq;
if N GE 50 then Z = (Runs - mu) / sigma;
else if Runs-mu LT 0 then Z = (Runs-mu+0.5)/sigma;
else Z = (Runs-mu-0.5)/sigma;
pvalue=2*(1-probnorm(abs(Z)));
by ID;
run;
title 'Wald-Wolfowitz Test for Randomness';
title2 'H0: The data are random';
proc print data=waldwolf label noobs;
id ID;
var z pvalue;
format pvalue pvalue.;
run;
[/pre]
Sincerely,
SPR
This is what I've got formally applying BY statement:
[pre]
data one;
do id=1 to 5;
drop i;
do i=1 to 75;
d=rannor(123);
n=75;
output;
end;
end;
run;
proc standard data=one out=two mean=0;
var d;
by ID;
run;
data runcount;
keep ID runs numpos numneg n;
set two end=last;
retain runs 0 numpos 0;
if FIRST.ID then do; runs=0; numpos=0; ld=.; end;
else do; prevpos=( ld GE 0 ); currpos=( D GE 0 ); ld=lag(D); end;
if currpos and prevpos then numpos+1;
else if currpos and ^prevpos then do;
runs+1;
numpos+1;
end;
else if ^currpos and prevpos then runs+1;
if last.ID then do;
numneg=n-numpos;
output;
end;
by ID;
run;
data waldwolf;
label z='Wald-Wolfowitz Z' pvalue='Pr > |Z|';
set runcount;
mu = ( (2*numpos*numneg) / (numpos+numneg) ) + 1;
sigmasq = ( (2*numpos*numneg) * (2*numpos*numneg-numneg-numpos) ) /
( ( (numpos+numneg)**2 ) * (numpos+numneg-1) );
sigma=sqrt(sigmasq);
drop sigmasq;
if N GE 50 then Z = (Runs - mu) / sigma;
else if Runs-mu LT 0 then Z = (Runs-mu+0.5)/sigma;
else Z = (Runs-mu-0.5)/sigma;
pvalue=2*(1-probnorm(abs(Z)));
by ID;
run;
title 'Wald-Wolfowitz Test for Randomness';
title2 'H0: The data are random';
proc print data=waldwolf label noobs;
id ID;
var z pvalue;
format pvalue pvalue.;
run;
[/pre]
Sincerely,
SPR
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am trying to run your code, but the value for numneg and n is missing. Can you advise me on this please?
I am trying to run your code, but the value for numneg and n is missing. Can you advise me on this please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The runs test is available in the autoreg procedure which does support by-processing. It should go like;
proc autoreg data=myData;
by myByVariable;
model myVariable= / runs;
run;
Note proc autoreg requires licence to SAS/ETS.
PG