PointStats.R

From GM-RKB
Jump to navigation Jump to search

pointStats.R 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 ...

NumList <- c(2, 20, 24, 19)
cnt_val=length(NumList) 

sum_val=0 ;
for (val in NumList ) {
   sum_val = sum_val + val 
   }
   # sum_val = sum(NumList)

avg_val = sum_val / cnt_val
# avg_val = sum(NumList)/length(NumList)

SortedNumList = sort (NumList)
med_val = 0
 

if (cnt_val %% 2 == 1) {
   med_val = SortedNumList[(cnt_val+1)/2-1]
} else {
   lower = SortedNumList[1+cnt_val/2-1]
   upper = SortedNumList[1+cnt_val/2]
   med_val = (lower + upper) / 2
}
# med_val = median(NumList) 

min_val = SortedNumList[1]
# min_val = min(NumList)
max_val = SortedNumList[cnt_val]
# max_val = max(NumList)

varS_val = 0
for (val in NumList ) {
     varS_val = varS_val + (avg_val - val) ** 2
  }
stddev_val = (varS_val / cnt_val) ** 0.5 ;
stddev_val = stddev(NumList) ;

tuple = c(cnt=cnt_val, sum=sum_val, avg=avg_val, med=med_val, min=min_val, max=max_val, varS=varS_val, stddev=stddev_val)
NumList
tuple
quit()

 
   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}