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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#===========================================================================#
# caTools - R library #
# Copyright (C) 2005 Jarek Tuszynski #
# Distributed under GNU General Public License version 3 #
#===========================================================================#
#source('C:/programs/R/R-2.9.2/src/library/caTools/R/runfunc.R')
runmean = function(x, k, alg=c("C", "R", "fast", "exact"),
endrule=c("mean", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{
alg = match.arg(alg)
endrule = match.arg(endrule)
align = match.arg(align)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
x = as.vector(x)
n = length(x)
if (k<=1) return (x)
if (k >n) k = n
k2 = k%/%2
y=double(n)
if (alg=="exact") {
.C("runmean_exact", x, y , as.integer(n), as.integer(k),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
} else if (alg=="C") {
.C("runmean", as.double(x), y , as.integer(n), as.integer(k),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
} else if (alg=="fast") {
.C("runmean_lite", as.double(x), y , as.integer(n), as.integer(k),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
} else { # the similar algorithm implemented in R language
k1 = k-k2-1
y = c( sum(x[1:k]), diff(x,k) ); # find the first sum and the differences from it
y = cumsum(y)/k # apply precomputed differences
y = c(rep(0,k1), y, rep(0,k2)) # make y the same length as x
if (endrule=="mean") endrule="func"
}
y = EndRule(x, y, k, dimx, endrule, align, mean, na.rm=TRUE)
return(y)
}
#==============================================================================
runmin = function(x, k, alg=c("C", "R"),
endrule=c("min", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{
alg = match.arg(alg)
align = match.arg(align)
endrule = match.arg(endrule)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
x = as.vector(x)
n = length(x)
if (k<=1) return (x)
if (k >n) k = n
y = double(n)
if (alg=="C") {
.C("runmin", as.double(x) ,y , as.integer(n), as.integer(k),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
} else { # the similar algorithm implemented in R language
k2 = k%/%2
k1 = k-k2-1
a <- y[k1+1] <- min(x[1:k], na.rm=TRUE)
if (k!=n) for (i in (2+k1):(n-k2)) {
if (a==y[i-1]) # point leaving the window was the min, so ...
y[i] = min(x[(i-k1):(i+k2)], na.rm=TRUE) # recalculate min of the window
else # min=y[i-1] is still inside the window
y[i] = min(y[i-1], x[i+k2 ], na.rm=TRUE) # compare it with the new point
a = x[i-k1] # point that will be removed from the window next
if (!is.finite(a)) a=y[i-1]+1 # this will force the 'else' option
}
if (endrule=="min") endrule="func"
}
y = EndRule(x, y, k, dimx, endrule, align, min, na.rm=TRUE)
return(y)
}
#==============================================================================
runmax = function(x, k, alg=c("C", "R"),
endrule=c("max", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{
alg = match.arg(alg)
endrule = match.arg(endrule)
align = match.arg(align)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
x = as.vector(x)
n = length(x)
k = as.integer(k)
if (k<=1) return (x)
if (k >n) k = n
y = double(n)
if (alg=="C") {
.C("runmax", as.double(x) ,y , as.integer(n), as.integer(k),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
} else { # the same algorithm implemented in R language
k2 = k%/%2
k1 = k-k2-1
a <- y[k1+1] <- max(x[1:k], na.rm=TRUE)
if (k!=n) for (i in (2+k1):(n-k2)) {
if (a==y[i-1]) # point leaving the window was the max, so ...
y[i] = max(x[(i-k1):(i+k2)], na.rm=TRUE) # recalculate max of the window
else # max=y[i-1] is still inside the window
y[i] = max(y[i-1], x[i+k2 ], na.rm=TRUE) # compare it with the new point
a = x[i-k1] # point that will be removed from the window next
if (!is.finite(a)) a=y[i-1]+1 # this will force the 'else' option
}
if (endrule=="max") endrule="func"
}
y = EndRule(x, y, k, dimx, endrule, align, max, na.rm=TRUE)
return(y)
}
#==============================================================================
runquantile = function(x, k, probs, type=7,
endrule=c("quantile", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{ ## see http://mathworld.wolfram.com/Quantile.html for very clear definition
## of different quantile types
endrule = match.arg(endrule)
align = match.arg(align)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
yIsVec = is.null(dimx) # original x was a vector
x = as.vector(x)
n = length(x)
np = length(probs)
k = as.integer(k)
type = as.integer(type)
if (k<=1) return (rep(x,n,np))
if (k >n) k = n
if (is.na(type) || (type < 1 | type > 9))
warning("'type' outside allowed range [1,9]; changing 'type' to ", type<-7)
y = double(n*np)
.C("runquantile", as.double(x) ,y , as.integer(n), as.integer(k),
as.double(probs), as.integer(np),as.integer(type),
NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
dim(y) = c(n,np)
for (i in 1:np) { # for each percentile
yTmp = EndRule(x, y[,i], k, dimx, endrule, align, quantile, probs=probs[i], type=type, na.rm=TRUE)
if (i==1) {
if (is.null(dimx)) dimy = length(yTmp) else dimy = dim(yTmp)
yy = matrix(0,length(yTmp),np) # initialize output array
}
yy[,i] = as.vector(yTmp)
}
if (np>1) dim(yy) = c(dimy,np) else dim(yy) = dimy
return(yy)
}
#==============================================================================
runmad = function(x, k, center = runmed(x,k), constant = 1.4826,
endrule=c("mad", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{
endrule = match.arg(endrule)
align = match.arg(align)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
x = as.vector(x)
n = length(x)
if (k<3) stop("'k' must be larger than 2")
if (k>n) k = n
y = double(n)
.C("runmad", as.double(x), as.double(center), y, as.integer(n),
as.integer(k), NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
y = EndRule(x, y, k, dimx, endrule, align, mad, constant=1, na.rm=TRUE)
return(constant*y)
}
#==============================================================================
runsd = function(x, k, center = runmean(x,k),
endrule=c("sd", "NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"))
{
endrule = match.arg(endrule)
align = match.arg(align)
dimx = dim(x) # Capture dimension of input array - to be used for formating y
x = as.vector(x)
n = length(x)
if (k<3) stop("'k' must be larger than 2")
if (k>n) k = n
y = double(n)
.C("runsd", as.double(x), as.double(center), y, as.integer(n),
as.integer(k), NAOK=TRUE, DUP=FALSE, PACKAGE="caTools")
y = EndRule(x, y, k, dimx, endrule, align, sd, na.rm=TRUE)
return(y)
}
#==============================================================================
EndRule = function(x, y, k, dimx,
endrule=c("NA", "trim", "keep", "constant", "func"),
align = c("center", "left", "right"), Func, ...)
{
# Function which postprocess results of running windows functions and cast
# them in to specified format. On input y is equivalent to
# y = runFUNC(as.vector(x), k, endrule="func", align="center")
# === Step 1: inspects inputs and unify format ===
align = match.arg(align)
k = as.integer(k)
k2 = k%/%2
if (k2<1) k2 = 1
yIsVec = is.null(dimx) # original x was a vector -> returned y will be a vector
if (yIsVec) dimx=c(length(y),1) # x & y will become 2D arrays
dim(x) <- dimx
dim(y) <- dimx
n = nrow(x)
m = ncol(x)
if (k >n) k2 = (n-1)%/%2
k1 = k-k2-1
idx1 = 1:k1
idx2 = (n-k2+1):n
# === Step 2: Apply different endrules ===
if (endrule=="NA") {
y[idx1,] = NA
y[idx2,] = NA
} else if (endrule=="keep") {
y[idx1,] = x[idx1,]
y[idx2,] = x[idx2,]
} else if (endrule=="constant") {
y[idx1,] = y[k1+1+integer(m),]
y[idx2,] = y[n-k2+integer(m),]
} else if (endrule=="trim") {
y = y[(k1+1):(n-k2),] # change y dimensions
} else if (endrule=="func" || !yIsVec) {
for (j in 1:m) {
for (i in idx1) y[i,j] = Func(x[1:(i+k2),j], ...)
for (i in idx2) y[i,j] = Func(x[(i-k1):n,j], ...)
}
}
# === Step 3: Adjust aligment if needed ===
if (endrule!="trim") {
if (align=="left") {
y[1:(n-k1),] = y[(k1+1):n,]
if (endrule=="keep") {
y[(n-k1+1):n,] = NA
} else if (endrule=="func" || !yIsVec) {
for (j in 1:m) for (i in (n-k+1):n) y[i,j] = Func(x[i:n,j], ...)
}
} else if (align=="right") {
y[(k2+1):n,] = y[1:(n-k2),]
if (endrule=="keep") {
y[1:k2,] = NA
} else if (endrule=="func" || !yIsVec) {
for (j in 1:m) for (i in 1:k) y[i,j] = Func(x[1:i,j], ...)
}
}
} # if (endrule!="trim")
# === Step 4: final casting and return results ===
if (yIsVec) y = as.vector(y);
return(y)
}
|