I need to use a function, which I have implemented in R. This is my code:
IS_IV <- function(name,SR){ ## SR is in Hz
data <- get(name)
SR <- SR
n <- nrow(data)
p <- 60*60*24*SR ## no. of data points per day
l <- 60*60*SR ## no. of data points per hour
mean_all <- mean(data[1:n,])
## -----------------------------
## IS numerator calculation
for (h in 1:p){
x <- ((mean(data[h:(l+h-1),]))-mean_all)^2
if (h == 1){
result_ISnum <- x
} else {
result_ISnum <- rbind (result_ISnum, x)
}
}
ISnum <- sum(result_ISnum)
ISnumerator <- n*ISnum
## -----------------------------
## IS denominator calculation
for (i in 1:n){
y <- ((data[i,]-mean_all)^2)
if (i == 1){
result_ISdenom <- y
} else {
result_ISdenom <- rbind (result_ISdenom, y)
}
}
ISdenom <- sum(result_ISdenom)
## -----------------------------
ISdenominator <- p*ISdenom
## -----------------------------
## IS calculation
IS <- ISnumerator/ISdenominator
## -----------------------------
## -----------------------------
## IV numerator calculation
for (k in 2:n){
x <- ((data[i,]-data[(i-1),])^2)
if (k == 1){
result_IVnum <- x
} else {
result_IVnum <- rbind (result_IVnum, x)
}
}
IVnum <- sum(result_IVnum)
IVnumerator <- n*IVnum
## -----------------------------
## IV denominator calculation
IVdenominator <- (n-1)*ISdenom ## uses ISdenom as only the multiplier is different
## -----------------------------
## IV calculation
IV <- IVnumerator/IVdenominator
result <- c(IS, IV)
colnames(result) <- c("Interday Stability (IS)", "Intraday Variability (IV)")
return(result)
}
Obviously, what makes the process so slow is the calculation of the ISdenominator, because it has to loop through every single data point (may be up to 800,000 or even more).
The question now is whether I will just have to live with this and look for the fastest computer around or whether you see an opportunity, to speed the whole thing up.
Thanks a lot!