/in.progress/auteurExtended/R/statistical.utilities.R

http://github.com/eastman/auteur · R · 94 lines · 56 code · 24 blank · 14 comment · 6 complexity · 145940790e631dacf2dbc071b587a971 MD5 · raw file

  1. #general statistical function for computing a highest density region (whose proportion is given by 'hpd')
  2. #author: R FITZ-JOHN 2010 (from diversitree)
  3. hdr <-
  4. function(z, hpd, lim=NULL){
  5. xx <- sort(c(lim, seq(min(z), max(z), length = 1024)))
  6. ez <- ecdf(z)
  7. f <- suppressWarnings(approxfun(ez(xx), xx))
  8. fit <- suppressWarnings(optimize(function(x) f(x + hpd) - f(x), c(0, 1 - hpd)))
  9. if (inherits(fit, "try-error") || is.na(fit$objective)) stop("HDR interval failure")
  10. ci <- fit$min
  11. f(c(ci, ci + hpd))
  12. }
  13. #general statistical function for comparing two vectors on non-independent samples
  14. #author: JM EASTMAN 2010
  15. randomization.test <-
  16. function(obs=obs, exp=exp, mu=0, iter=10000, two.tailed=FALSE){
  17. O=as.numeric(obs)[sample(1:length(obs), iter, replace=TRUE)]
  18. E=as.numeric(exp)[sample(1:length(exp), iter, replace=TRUE)]
  19. result=c(O-(E+mu))
  20. p=round(sum(result>=0)/iter, digits=nchar(iter))
  21. q=1-p
  22. if(two.tailed) {
  23. res=list(diffs=result, 2*(c(p,q)[which(c(p,q)==min(c(p,q)))]))
  24. } else {
  25. res=list(diffs=result, greater=p, lesser=q)
  26. }
  27. return(res)
  28. }
  29. #general statistical function for finding probability of a value within a truncated Poisson distribution
  30. #author: JM EASTMAN 2010
  31. ptpois <-
  32. function(x, lambda, k) {
  33. p.k=ppois(k, lambda, lower.tail=FALSE)
  34. p.x=ppois(x, lambda, lower.tail=FALSE)
  35. ptp=p.x/(1-p.k)
  36. return(ptp)
  37. }
  38. #general statistical function for random samples from a truncated Poisson distribution
  39. #author: JM EASTMAN 2010
  40. rtpois <-
  41. function(N, lambda, k) {
  42. p=ppois(k, lambda, lower.tail=FALSE)
  43. out=qpois(runif(N, min=0, max=1-p), lambda)
  44. out
  45. }
  46. #determines if a value falls within a range [min,max]
  47. #author: JM Eastman 2010
  48. withinrange <-
  49. function(x, min, max) {
  50. a=sign(x-min)
  51. b=sign(x-max)
  52. if(abs(a+b)==2) return(FALSE) else return(TRUE)
  53. }
  54. #general statistical function for finding a 'yy' value (by extrapolation or interpolation) given a linear model constructed from 'x' and 'y' and a specified value 'xx' at which to compute 'yy'
  55. #author: JM EASTMAN 2010
  56. infer.y <-
  57. function(xx, x, y) {
  58. f=lm(y~x)
  59. p=unname(coef(f))
  60. yy=xx*p[2]+p[1]
  61. return(yy)
  62. }
  63. #general utility for finding the indices of the two most similar values in 'x', given 'val'
  64. #author: JM EASTMAN 2010
  65. nearest.pair <-
  66. function(val, x) {
  67. dev=abs(x-val)
  68. names(dev)=1:length(dev)
  69. return(sort(as.numeric(names(sort(dev))[1:2])))
  70. }