Advertisement
CarloNaamloos

Grubbs test

Jan 25th, 2021 (edited)
2,446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 1.43 KB | None | 0 0
  1. %macro  grubbs(dataset ,var ,id,alpha =0.05);
  2. ods  select  none;
  3. proc  means  data=&dataset  mean  var n;
  4. var &var;
  5. output  out=out  mean=mean  var=var n=n;
  6. run;
  7. data  outliers;
  8. set &dataset(keep=&id &var);
  9. if _n_=1 then  set  out;
  10. /*  statistic  */u = abs((&var - mean) / sqrt(var));
  11. /*  critical  value */t = quantile ("t", &alpha / (2*n), n-2);
  12. c = (n-1) * sqrt(t**2 / (n * (t**2 + n - 2)));
  13. /*  check if this is an  outlier  */if(u > c) then outlier = "yes";
  14. else outlier = "no";
  15. /* p-value */u_inv = u*sqrt((n-2)*n) / sqrt(1 - (u**2-(n-2))*n);
  16. p_value = min (2*n*(1-cdf("t", u_inv , n-2)), 1);
  17. keep &id  p_value u c outlier;
  18. run;
  19. proc  sort  data=outliers;
  20. by  descending u;
  21. run;
  22. ods  select  all;
  23. title1 "Grubbs  test";
  24. title2 "Variable: &var";
  25. proc  print  data=outliers(obs =1)  label  noobs;
  26. var &id  p_value u c outlier;
  27. label &id="id" p_value ="p-value" u=" statistic  (|u|)"c=" critical  value"outlier ="is  outlier ?";
  28. run;
  29. title;
  30. %mend;
  31.  
  32. /* ================ EXAMPLE ============================*/
  33. libname  SASDATA  "/folders/myfolders ";
  34. data IVFTransformed;
  35.     set SASDATA.IVF;
  36.     TTP_transformed = SQRT(TTP);
  37. run;
  38.  
  39. %grubbs(dataset=IVFTransformed,var=TTP_transformed,id=ID,alpha=0.05);
  40.  
  41. /* ================= CHECK FOR ASSUMPTIONS ============== */
  42.  
  43. /*If the data is normal distributed */
  44. PROC UNIVARIATE DATA=IVF4monthTTP NORMAL;
  45. VAR TTP_transformed;
  46. PROBPLOT TTP_transformed /NORMAL(MU=est SIGMA=est);
  47. RUN;
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement