Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %macro bartlett(data=_last_, var=, group=, bygroup=_none);
- /*****************************************************************/
- /* The Bartlett macro fills in the template for calculations on */
- /* a number of samples. The variable containing the observations */
- /* is "var". The samples are identified by the classification */
- /* variable "group". The calculation may be repeated on several */
- /* groups of samples. The groups are identified by the "bygroup" */
- /* variable. */
- /* Furthermore, the macro calculates Bartlett's test for */
- /* homogeniety of variance. */
- /* (Samples for which all the observations are equal and groups */
- /* for which the number of non-missing observations is less than */
- /* 2 are excluded from Bartletts test althought they appear in */
- /* the template for calculations. */
- /* */
- /* Due to the layout of the output it is recommanded that the */
- /* "group" variable either is numerical or can be identified */
- /* from the first four characters. */
- /* */
- /* The lines in the template of calculation are 80 characters */
- /* long so it is recommended to have a linesize of 80 or larger */
- /* (OPTIONS LS=80;) */
- /* */
- /* The data set "_bartlett" contains nearly all values written */
- /* on the output. */
- /* */
- /* Reference: */
- /* Preben Blaesild & Jørgen Granfeldt: */
- /* STATISTICS with APPLICATIONS IN BIOLOGY AND GEOLOGY, */
- /* 2002. */
- /* */
- /* Danish versions: */
- /* J›rn Attermann 19-01-94, 14-04-94, 08-06-94 */
- /* First english version: */
- /* Preben Blaesild & J›rgen Granfeldt 03-01-01 */
- /* */
- /*****************************************************************/
- data _mact1;
- set &data;
- _none=1;
- proc sort;
- by &bygroup &group;
- proc means noprint;
- var &var;
- output out=_mact2 n=_ni sum=_Si USS=_USSi mean=_meani;
- by &bygroup &group;
- data _bartlett;
- set _mact2;
- by &bygroup;
- retain _n _S _USS _CT _SSD _f _s_invfi _s_flns _k . _ekskl 0;
- if first.&bygroup then do;
- _n=.;
- _S=.;
- _USS=.;
- _CT=.;
- _SSD=.;
- _f=.;
- _s_invfi=.;
- _s_flns=.;
- _k=.;
- _ekskl = 0;
- end;
- if _ni > 0 then _CTi=_Si**2/_ni;
- else _CTi = .;
- _SSDi=_USSi-_CTi;
- _fi=_ni-1;
- if _fi >= 1 & _SSDi > 0 then do;
- _vari=_SSDi/_fi;
- _s_invfi+1/_fi;
- _s_flns+_fi*log(_vari);
- _k+1;
- end;
- else do;
- _ekskl + 1;
- _fi = 0;
- _vari = .;
- end;
- _n+_ni;
- _S+_Si;
- _USS+_USSi;
- _CT+_CTi;
- _SSD+_SSDi;
- _f+_fi;
- if last.&bygroup then do;
- _var=_SSD/_f;
- if _k > 1 then do;
- _C=1+1/3/(_k-1)*(_s_invfi-1/_f);
- _2lnQ=_f*log(_var)-_s_flns;
- _Ba=_2lnQ/_C;
- _testss=1-probchi(_Ba,_k-1);
- end;
- end;
- /* Print calculated quantities */
- file print;
- if _n_=1 then do;
- put / @34 'Macro "BARTLETT"';
- put / @37 "Data set: %upcase(&data)";
- put @28 "Response variable: %upcase(&var)";
- put @31 "Group variable: %upcase(&group)";
- %if &bygroup ^= _none %then %do;
- put @24 "Bygroup variable: %upcase(&bygroup)";
- %end;
- end;
- if first.&bygroup then do;
- %if &bygroup^=_none %then %do;
- _bychar=trim(left(&bygroup));
- put // @33 "&bygroup = " @44 _bychar $28.;
- put ' ____________________________________________________';
- %end;
- put // @28 'Calculations in k samples:' /;
- put @63 'Sample' @74 'Sample' ;
- put @1 'i' @7 'ni' @14 'Si' @25 'USSi' @37 'Si2/ni' @49 'SSDi'
- @58 'fi' @62 'Variance' @75 'Mean' /;
- end;
- _grpchar=trim(left(&group));
- put @1 _grpchar $4. @6 _ni 3.0 @10 _Si 9.3 @20 _USSi 12.4 @33 _CTi 12.4
- @46 _SSDi 10.4 @57 _fi 3.0 @61 _vari 10.5 @72 _meani 9.4;
- if last.&bygroup then do;
- put '------------------------------------------------------------====================';
- put @6 _n 3.0 @10 _S 9.3 @20 _USS 12.4 @33 _CT 12.4
- @46 _SSD 10.4 @57 _f 3.0 @61 _var 10.5;
- put // @33 "Bartlett's test:";
- put / @23 'C' @32 '-2lnQ' @44 'Ba' @52 'Pr > ChiSq' /;
- put @ 17 _C 10.5 @28 _2lnQ 10.5 @39 _Ba 10.5 @50 _testss 10.5;
- if _ekskl > 0 then do;
- put /// @5 'NB: ' _ekskl ' group(s) excluded from the test.';
- end;
- put _PAGE_;
- end;
- keep
- %if &bygroup^=_none %then %do;
- &bygroup
- %end;
- &group _ni _n _Si _S _USSi _USS _CTi _CT _SSDi _SSD _fi _f _vari _var
- _meani _C _Ba _2lnQ _testss;
- run;
- %mend bartlett;
- DATA kraft;
- DO i=1 TO 4;
- INPUT person@@;
- INPUT vaegt@@;
- DO j=1 TO 4;
- INPUT kraft@@;
- OUTPUT;
- END;
- END;
- DATALINES;
- 1 73.2 3253 3136 3245 3321
- 2 78.3 3526 3621 3548 3612
- 3 75.3 3325 3417 3345 3442
- 4 72.5 3123 3112 3201 3159
- ;
- RUN;
- /* Omregning til 1000 Newton med 2 decimaler*/
- DATA kraft;
- SET kraft;
- kraft=round(kraft/1000,0.01);
- RUN;
- PROC GLM DATA=kraft;
- %bartlett(data=kraft, var=i, group=j);
- RUN;
- PROC PRINT;
- RUN;
Add Comment
Please, Sign In to add comment