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

Uploaded
author pmac
date Wed, 01 Jun 2016 03:38:39 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
64e75e21466e Uploaded
pmac
parents:
diff changeset
1 # Finding outliers by standard deviation
64e75e21466e Uploaded
pmac
parents:
diff changeset
2
64e75e21466e Uploaded
pmac
parents:
diff changeset
3 # Get samples whose pc1 OR pc2 values lie more than 'numsds' s.devs
64e75e21466e Uploaded
pmac
parents:
diff changeset
4 # away from the sample median for that pc.
64e75e21466e Uploaded
pmac
parents:
diff changeset
5 outliers_by_sd = function(pca_data, xsamples, numsds) {
64e75e21466e Uploaded
pmac
parents:
diff changeset
6 pc1_outliers = find_outliers(pca_data$values[, 1], numsds)
64e75e21466e Uploaded
pmac
parents:
diff changeset
7 pc2_outliers = find_outliers(pca_data$values[, 2], numsds)
64e75e21466e Uploaded
pmac
parents:
diff changeset
8 all_outliers = union(pc1_outliers, pc2_outliers)
64e75e21466e Uploaded
pmac
parents:
diff changeset
9 return(all_outliers)
64e75e21466e Uploaded
pmac
parents:
diff changeset
10 }
64e75e21466e Uploaded
pmac
parents:
diff changeset
11
64e75e21466e Uploaded
pmac
parents:
diff changeset
12 # compute outliers
64e75e21466e Uploaded
pmac
parents:
diff changeset
13 # Returns indices of all samples which lie more than
64e75e21466e Uploaded
pmac
parents:
diff changeset
14 # 'numsds' s.devs away from the sample median
64e75e21466e Uploaded
pmac
parents:
diff changeset
15 find_outliers = function(input_data, numsds) {
64e75e21466e Uploaded
pmac
parents:
diff changeset
16 lower = median(input_data) - numsds*sd(input_data)
64e75e21466e Uploaded
pmac
parents:
diff changeset
17 upper = median(input_data) + numsds*sd(input_data)
64e75e21466e Uploaded
pmac
parents:
diff changeset
18
64e75e21466e Uploaded
pmac
parents:
diff changeset
19 outliers = which(input_data < lower | input_data > upper)
64e75e21466e Uploaded
pmac
parents:
diff changeset
20 return(outliers)
64e75e21466e Uploaded
pmac
parents:
diff changeset
21 }