pointStats.r Program

From GM-RKB
Jump to navigation Jump to search

A pointStats.r Program is an R-based point estimation system.



References

/*
** pointStats.r(v1.0) - Calculate some point statistics.
** return results in a hash {Average}{Min}{Max}{Median}{SquareSum}{StdDev}
** References: http://szabgab.com/talks/fundamentals_of_perl/solution-median.html
*/

pointStats() <- function(NumList){
# "This R function returns several well known population statistics. It assumes input of a non-empty numeric list."

   # ideally should test if non-empty ...

   cnt_val = length(Numlist) ;

   sum_val = 0;
   for val in Numlist:
   	 sum_val += val 
   # sum_val = sum(Numlist)

   avg_val = float(sum_val) / cnt_val
   # avg_val = float(sum(Numlist)) / len(Numlist)
 
   SortedNumlist = sorted (Numlist)
   med_val = 0
   if cnt_val % 2 == 1:
     med_val = SortedNumlist[(cnt_val+1)/2-1]
   else:
     lower = SortedNumlist[cnt_val/2-1]
     upper = SortedNumlist[cnt_val/2]
     med_val = (float(lower + upper)) / 2
 
   min_val = SortedNumlist[0]
   max_val = SortedNumlist[cnt_val-1] ;

   varS_val = 0
   for val in Numlist:
     varS_val += (avg_val - val) ** 2
   stddev_val = (varS_val / cnt_val) ** 0.5 ;

   Result = {}
   Result["Count"]       = cnt_val ;
   Result["Sum"]         = sum_val ;
   Result["Average"]     = avg_val ;
   Result["Min"]         = min_val ;
   Result["Max"]         = max_val ;
   Result["Median"]      = med_val ;
   Result["VarSquSum"]   = varS_val ;
   Result["StdDev"]      = stddev_val ;
 
   return Result 
   #return cnt_val, min_val, max_val, avg_val, med_val, stddev_val ;

}

# Call the pointStats function

NumList <- c(2, 20, 24, 19)
Result = pointStats( NumList );
print Result

# {'Count': 4, 'Min': 2, 'Max': 24, 'Sum': 65, 'Median': 19.5, 'VarSquSum': 284.75, 'StdDev': 8.4372685, 'Average': 16.25}