jhylands

Basic stats

Apr 29th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.93 KB | None | 0 0
  1. <h1>Stats calculator</h1>
  2. <textarea  id="datas" rows="5" cols="50">1,2,3,4,7,4</textarea>
  3. <br />
  4. <input type="button" onclick="splitdata()" value="Sort shit"/>
  5. <x id="Report"></x>
  6. <script>
  7. //basic analysis of one set of data through various methods
  8. var x;
  9. function splitdata(){
  10. var Zx = document.getElementById("datas").value;
  11. x = Zx.split(",");
  12. var n = x.length;
  13. for(i=0;i<n;i++){
  14. x[i] = Number(x[i]);
  15. }
  16. bubblesort();
  17. //start building the report
  18. var report="<p>The data in ascending order: " + x.join();
  19. report= report + "<br />The sum is: " + sumx() + " The total data points is: " + n + " Thus the mean is: " + sumx()/n;
  20. report = report + "<br />The lower quartile is: " + x[Math.round(n/4)-1] + " The median is: " + median() + " The upper quartile is: " + x[Math.round(3*n/4)-1];
  21. report = report + "<br />The overall range is: " + (x[n-1]-x[0]) + " The interquartile range is: " + (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  22. report = report + "<br />The lower outliers are:" + Loutliers() + " The upper outliers are: " + Uoutliers();
  23. report = report + "<br />The mean calculated without outliers is: " + MEAN();
  24. report = report + "<br />The sum of the squares is: " + sumx2();
  25. report = report + "<br />The variance is: " + variance() + " Hence the standard deviation is: " + Math.pow(variance(),0.5);
  26.  
  27. report = report + "</p>"
  28. document.getElementById("Report").innerHTML = report;
  29. }
  30. function variance(){
  31. var n = x.length;
  32. return sumx2()/n - Math.pow(sumx()/n,2);
  33. }
  34. function sumx2(){
  35. var sum = 0;
  36. for (i=0;i<x.length;i++){
  37. sum = sum + Math.pow(x[i],2);
  38. }
  39. return sum;
  40. }
  41. function MEAN(){
  42. var sum = 0;
  43. var n = 0;
  44. for (i=0;i<x.length;i++){
  45.     if(x[i]>= GetLowQ() && x[i] <= GetUpQ()){
  46.     sum = sum + x[i];
  47.     n++;
  48.     }
  49. }
  50. return sum/n;
  51. }
  52. function Uoutliers(){
  53. var list = "";
  54. for (i=0;i<x.length;i++){
  55. if(x[i]>GetUpQ()){
  56.  list = list + x[i] + ",";
  57.  }
  58. }
  59. if (list==""){
  60. return "No higher outliers!";
  61. }else{
  62. return list;
  63. }
  64. }
  65. function Loutliers(){
  66. var list = "";
  67. for (i=0;i<x.length;i++){
  68. if(x[i]<GetLowQ()){
  69. list = list + x[i] + ",";
  70. }
  71. }
  72. if (list==""){
  73. return "No lower outliers!";
  74. }else{
  75. return list;
  76. }
  77. }
  78. function GetLowQ(){
  79. var n = x.length;
  80. var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  81. var LB = x[Math.round(n/4)-1] - rang;
  82. return LB;
  83. }
  84. function GetUpQ(){
  85. var n = x.length;
  86. var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  87. var HB = x[Math.round(3*n/4)-1] + rang;
  88. return HB;
  89. }
  90. function median(){
  91. n=x.length;
  92. alert(n % 2);
  93. if (n % 2 == 0){
  94.     return (x[n/2]+x[n/2-1])/2;
  95. }else{
  96.     return x[n/2+0.5];
  97. }
  98. }//end of median
  99. function sumx(){
  100. var sum = 0;
  101. for (i=0;i<x.length;i++){
  102. sum = sum + x[i];
  103. }
  104. return sum;
  105. }
  106. function bubblesort(){
  107. var cat = 0;
  108. while(cat==0){
  109. cat=1;
  110. for (i=0;i<(x.length-1);i++){
  111.  if (x[i]>x[i+1]){//if their in the wrong order swap them
  112.    hold=x[i];
  113.    x[i]=x[i+1];
  114.    x[i+1] = hold;
  115.    cat=0;
  116.   }//exit if
  117.  }//exit the for loop
  118. }//exit bubble sort loop
  119. document.getElementById("datas").value = x.join();
  120. }//exit bubble sort function
  121.  
  122.  
  123. </script>
Advertisement
Add Comment
Please, Sign In to add comment