The number of sequences in my sequence data is about 5,000, and the sequence length is 1440. In order to reduce computational time to obtain an optimal matching pairwise matrix, I want to randomly select 5% of the sample. I would appreciate your suggestions on that.
You can easily select a sample using the "sample" function. Here is an example using the "mvad" data set (unweighted)
## Loading the library library(TraMineR) data(mvad) ## Defining sequence properties mvad.alphabet <- c("employment", "FE", "HE", "joblessness", "school", "training") mvad.lab <- c("employment", "further education", "higher education", "joblessness", "school", "training") mvad.shortlab <- c("EM", "FE", "HE", "JL", "SC", "TR") ## The state sequence object. mvad.seq <- seqdef(mvad, 17:86, alphabet = mvad.alphabet, states = mvad.shortlab, labels = mvad.lab, xtstep = 6)
You then use the "sample" function, specifying the number of sequences in total, and the number you want to keep (here round(0.05*nrow(mvad.seq)). If you want to be able to reproduce the sampling, you should first set the seed (i.e. initialize the random number generator) to a given (no importance) value.
set.seed(1) sampled.indices <- sample(nrow(mvad.seq), round(0.05*nrow(mvad.seq)))
If you want to use case weights, look at the "prob" argument in the help page. The sample function returns the indices of the sequences that you want to keep.
sampled.mvad.seq <- mvad.seq[sampled.indices, ]
If you want to plot the sampled sequence versus the non sampled one, you can use the following code.
sampled <- numeric(nrow(mvad.seq)) sampled[sampled.indices] <- 1 sampled[-sampled.indices] <- 0 seqdplot(mvad.seq, group=sampled)
Apart from selecting a sample, there are at least two ways to reduce computation time.
- If relevant, you can try to reduce the time granularity. Distance computation time is highly dependent on sequence length (O^2).
- There is a hidden option in "seqdist" to use an optimized version of the optimal matching algorithm. It is still in testing phase (that's why it is hidden), but it should replace the actual algorithm in a future version. To use it, set method="OMopt", instead of method="OM". Depending on your sequences, it may reduce computation time.
If your sequences are columns in a spreadsheet then put in an extra row containing random numbers (rand() in Excel gives a uniform random deviate between 0 and 1) and then sort the columns using the random values as the key. Take the first 5% of the columns as your randomly selected 5% of the total.