aboutsummaryrefslogtreecommitdiffstats
path: root/r/chisq.R
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@gmail.com>2016-03-31 10:57:34 +0800
committerAaron LI <aaronly.me@gmail.com>2016-03-31 10:57:34 +0800
commitc9c896dea2ba43551c4e10bd49666105449e9bd7 (patch)
treee94b73f17b2d776c2acd4c9549657f500c3dc7ce /r/chisq.R
parent2b6cb9b655a53d43b32a8a211287c82f4f59999a (diff)
downloadatoolbox-c9c896dea2ba43551c4e10bd49666105449e9bd7.tar.bz2
add all scripts/tools
Diffstat (limited to 'r/chisq.R')
-rw-r--r--r/chisq.R21
1 files changed, 21 insertions, 0 deletions
diff --git a/r/chisq.R b/r/chisq.R
new file mode 100644
index 0000000..41f851f
--- /dev/null
+++ b/r/chisq.R
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+#
+# Calculate the chi-squared values between the given data and model values.
+#
+
+calc.chisq <- function(value, error=NULL, model=NULL) {
+ if (is.data.frame(value)) {
+ df <- value
+ value <- df$value
+ error <- df$error
+ model <- df$model
+ }
+ chisq <- (value - model)^2
+ if (! is.null(error)) {
+ weights <- error ^ (-2)
+ chisq <- chisq * weights
+ }
+ return(sum(chisq))
+}
+
+# vim: set ts=8 sw=4 tw=0 fenc=utf-8 ft=r: #