/
customFunctions.R
executable file
·114 lines (99 loc) · 3.92 KB
/
customFunctions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# A set of useful custom functions
# Iyad Aldaqre
# 17.01.2017
# for
# require(zoo)
require(aspace)
# function to return index of all values that are above or below N Standard Deviations from the mean
stdOutlierIdx <- function(x, nSD = 3, na.rm=T){
idx <- rep(FALSE, length(x))
idx[x < (mean(x, na.rm=na.rm) - (sd(x, na.rm=na.rm) * nSD)) | x > (mean(x, na.rm=na.rm) + (sd(x, na.rm=na.rm) * nSD))] <- TRUE
return(idx)
}
# function to return the index of all values that are above or below a static threshold
threshOutlierIdx <- function(x, min=0, max=6){
idx <- rep(FALSE, length(x))
idx[x < min | x > max] <- TRUE
return(idx)
}
# A function to get the maximum n values (instead of just the single maximum value)
maxN<-function(x,n){
return(sort(x)[(length(x)-n):length(x)])
}
# A function to plot error bars in barplots
# from monkeysuncle.stanford.edu/?p=485
error.bar <- function(x, y, upper, lower = upper, length = 0.1, ...) {
if (length(x) != length(y) | length(y) != length(lower) | length(lower) != length(upper)){
stop("Vectors must be of the same length")
} else {
arrows(x, y + upper, x, y - lower, angle = 90, code = 3, length = length, ...)
}
}
# A function to calculate the standard error of the mean
sem <- function(x, na.rm = TRUE) {
if (na.rm) {stdError <- sd(x, na.rm = T)/sqrt(length(x))} else {stdError <- sd(x)/sqrt(length(x))}
return(stdError)
}
# A function to plot line and shaded error area around the line
error.line<-function(x,y,error,type='l',lineCol='black',ylim=c(min(y),max(y)),ylab='',xlab='',errorCol=lineCol,errorAlpha=40,predict=T,add=F,...){
if(length(error)==1){
warning('Length of error was expanded to match length of x')
error<-rep(error,length(x))
}else if(length(error)>1&length(error)!=length(x)){
stop('Error and x lengths differ!')
}
if(predict==T){
lineData<-predict(smooth.spline(x,y))
errorData<-predict(smooth.spline(x,error))
} else {
lineData<-as.data.frame(cbind(x,y))
errorData<-as.data.frame(cbind(x,error))
names(errorData)<-c('x','y')
}
if(add==F){plot(x,y,type = 'n',ylim = ylim,ylab = ylab, xlab = xlab)}
lines(lineData, col = lineCol,type=type,...)
colVal<-paste(rgb(t(col2rgb(errorCol)),maxColorValue=255),as.character(errorAlpha),sep='')
polygon(c(lineData$x,rev(lineData$x)), c(lineData$y+errorData$y, rev(lineData$y-errorData$y)),col = colVal, border = NA)
}
# A function to calculate cohen's d effect size for t.tests
cohen.d<-function(M1,SD1,M2,SD2){
sPooled=sqrt(((SD1^2)+(SD2^2))/2)
es<-M1-M2/sPooled
return(es)
}
# A function to interpolate ColNAs # col.na.approx(cbind(x,y,maxgap=interpThresh,na.rm=T))
col.na.approx<-function(data,maxgap=5,na.rm=T){
for(colNum in 1:ncol(data)){
data[,colNum]<-na.approx(data[,colNum],...)
}
return(data)
}
# A function to convert pixels into degrees of visual angle
pix2deg<-function(pixels,screenSize=22,screenResolution=c(1920,1080),distance=600){
# dpi: diagnoal pixels, as calculated by pythagorean theorem, divided by screen size in inches
dpi<-sqrt((screenResolution[1]^2)+(screenResolution[2]^2))/screenSize
mm<-(pixels*25.4)/dpi
deg<-2*atan_d(mm/(2*distance))
return(deg)
}
# A function to convert degrees of visual angle into pixels
deg2pix <- function(degrees, screenSize=22, screenResolution=c(1920, 1080), distance = 600){
# dpi: diagnoal pixels, as calculated by pythagorean theorem, divided by screen size in inches
dpi <- sqrt((screenResolution[1]^2)+(screenResolution[2]^2))/screenSize # dot per inch
dpmm <- dpi / 25.4 # converted to mm
pix <- (2*distance*tan_d(degrees/2)) * dpmm
return(round(pix)) # because pixels ar integers!
}
deg2pix2 <- function(degrees, dotPerMm, distance = 600){
pix <- (2*distance*tan_d(degrees/2)) * dotPerMm
return(round(pix)) # because pixels ar integers!
}
# A function to convert mm to degrees of visual angle
mm2deg <- function(mm, distance){
deg <- 2*atan_d(mm/(2*distance))
return(deg)
}
deg2mm <- function(deg, distance){
mm <- 2*distance*tan_d(deg/2)
return(mm)
}