diff R_functions/outlier_trimming.R @ 0:64e75e21466e draft default tip

Uploaded
author pmac
date Wed, 01 Jun 2016 03:38:39 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/R_functions/outlier_trimming.R	Wed Jun 01 03:38:39 2016 -0400
@@ -0,0 +1,21 @@
+# Finding outliers by standard deviation
+
+# Get samples whose pc1 OR pc2 values lie more than 'numsds' s.devs
+# away from the sample median for that pc.
+outliers_by_sd = function(pca_data, xsamples, numsds) {
+  pc1_outliers = find_outliers(pca_data$values[, 1], numsds)
+  pc2_outliers = find_outliers(pca_data$values[, 2], numsds)
+  all_outliers = union(pc1_outliers, pc2_outliers)
+  return(all_outliers)
+}
+
+# compute outliers
+# Returns indices of all samples which lie more than
+# 'numsds' s.devs away from the sample median
+find_outliers = function(input_data, numsds) {
+  lower = median(input_data) - numsds*sd(input_data)
+  upper = median(input_data) + numsds*sd(input_data)
+  
+  outliers = which(input_data < lower | input_data > upper)
+  return(outliers)
+}
\ No newline at end of file