[R] Speeding up a bootstrap routine
I have written the R code below to perform White's Data Mining Reality Check (DMRC) but as it stands at the moment it is painfully slow. It is written as a function as I call it many times from a script file with different data input, and the output is sunk() to a text file. Could anyone suggest improvements to the code to increase its speed? boot_white_test <- function(data) { detrendedreturns <- data[,1]; # creates a separate column vector of detrended returns (preparation for multiplication that follows) posvector1 <- data[,2]; # creates a column of position vectors for smoothed price posvector2 <- data[,3]; # creates a column of position vectors for 2 bar prediction posvector3 <- data[,4]; # creates a column of position vectors for 5 bar prediction actualreturns1 <- detrendedreturns*posvector1; actualreturns2 <- detrendedreturns*posvector2; actualreturns3 <- detrendedreturns*posvector3; average_daily_return1 <- mean(actualreturns1); average_daily_return2 <- mean(actualreturns2); average_daily_return3 <- mean(actualreturns3); # create zero centred sampling distributions for the null hypothesis zerocentredreturns1 <- actualreturns1-average_daily_return1; zerocentredreturns2 <- actualreturns1-average_daily_return2; zerocentredreturns3 <- actualreturns1-average_daily_return3; n <- length(detrendedreturns); result1 <- 0.0; # initialise result result2 <- 0.0; # initialise result result3 <- 0.0; # initialise result # create matrices to hold sampling returns matrix_1 <- matrix(0,1,n) matrix_2 <- matrix(0,1,n) matrix_3 <- matrix(0,1,n) datevector <- 1:n # create vector for the actual "date sampling" # the bootstrap routine, placing results into the above results matrices for(i in 1:5000) { date_sample <- datevector[sample(n,n,replace=TRUE)] for(j in 1:n) { matrix_1[j] <- zerocentredreturns1[date_sample[j]] matrix_2[j] <- zerocentredreturns2[date_sample[j]] matrix_3[j] <- zerocentredreturns3[date_sample[j]] x <- mean(matrix_1) y <- mean(matrix_2) z <- mean(matrix_3) max_boot_return <- max(x,y,z) if (max_boot_return>=average_daily_return1) result1 <- result1+1 # create "p values" if (max_boot_return>=average_daily_return2) result2 <- result2+1 # create "p values" if (max_boot_return>=average_daily_return3) result3 <- result3+1 # create "p values" } } -- View this message in context: http://www.nabble.com/Speeding-up-a-bootstrap-routine-tp24908001p24908001.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Semi Standard Deviation
Is there a function in R to calculate the semi deviation of a data set? I cannot seem to find a reference to one in the manual. -- View this message in context: http://www.nabble.com/Semi-Standard-Deviation-tp25138640p25138640.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Circular shift function?
Is there a circular shift function in R similar to the shift function in Octave? From my reading of the manual there does not appear to be one, but if I am mistaken, can someone point out the relevant section? -- View this message in context: http://www.nabble.com/Circular-shift-function--tp23896972p23896972.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Custom annotation vector in R tsmp package
I'd like to use a custom annotation vector (av) for a matrix profile (mp) produced using the tsmp package, but I am unsure how to amend said profile produced by e.g. the compute function. I note that the package provides some functions to do this for "standard" cases, such as av_complexity, av_hardlimit_artifact etc. and then av_apply, but I want to apply a custom av. I have tried directly replacing the mp in the results produced by compute, i.e. result <- compute( data , ... ) result$mp <- my_av_mp but this doesn't work as I get the following error Error in `[.data.frame`(matrix_profile$mp, min_idx) : undefined columns selected Trying result$mp[,1] <- my_av_mp doesn't give an error, but when I try to use the amended result further, i.e. motif_results <- find_motif( result , n_motifs = 5 , ... ) I get the error Error in apply(.mp$mp, 2, which.min) : dim(X) must have a positive length What is the correct way to apply a custom av with the tsmp package? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error "could not find function"
I have copied a very simple function but when I try to call it I get an error message "could not find function". The function file is named addthree.r and I cd to the directory where the function is saved before calling it. What am I doing wrong? -- View this message in context: http://www.nabble.com/Error-%22could-not-find-function%22-tp21279720p21279720.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.