# TraMineR seqLLCS and seqdist reference sequence issue

When comparing the same string sequences in the example below, why does the first call return 0 instead of 7? LCS.ex1[3,] is "S-U-M-S-SC-UC-MC" and LCS.ex2[1,] is also "S-U-M-S-SC-UC-MC".

Does one always have to pass in a sequence type with only one value for this function (and seqdist) to work? How can I refer to just one row from a sequence object without the rest of the object still being referenced? i.e., I don't want to have to create a new sequence object for every string value of LCS.ex2 to be able to compare each to LCS.ex1.

LCS.ex1 <- c("S-U-S", "U-S-SC-MC", "S-U-M-S-SC-UC-MC")
LCS.ex2 <- c("S-U-M-S-SC-UC-MC", "1-2-3", "A-B-C-D-E-F-G-H-I-J-K")
LCS.ex3 <- c("S-U-M-S-SC-UC-MC")

LCS.ex1 <- seqdef(LCS.ex1)
LCS.ex2 <- seqdef(LCS.ex2)
LCS.ex3 <- seqdef(LCS.ex3)

seqLLCS(LCS.ex1[3,],LCS.ex2[1,])
R> 0
seqLLCS(LCS.ex1[3,],LCS.ex3[1,])
R> 7


You are right; the first result should be 7. This is because you used two different state sequences objects, which do not have the same state definition. The list of states can be retrieved with the "alphabet" command.

alphabet(LCS.ex1)
alphabet(LCS.ex2)


The function seqLLCS should check that the alphabet of seq1 and seq2 are equal and throws an error if it's not the case.

Now, you can use one single state sequence object, with the following code.

LCS.ex1 <- c("S-U-S", "U-S-SC-MC", "S-U-M-S-SC-UC-MC")
LCS.ex2 <- c("S-U-M-S-SC-UC-MC", "1-2-3", "A-B-C-D-E-F-G-H-I-J-K")
LCS.ex3 <- c("S-U-M-S-SC-UC-MC")

LCS.ex <- seqdef(c(LCS.ex1, LCS.ex2, LCS.ex3))


seqLLCS(LCS.ex[3,], LCS.ex[4,])
seqLLCS(LCS.ex[3,], LCS.ex[7,])


If you use seqdist, then you directly pass the whole object (you do not have to specify each two by two comparisons). Note that this computes distances rather than the length of the longest common prefix. seqdist(LCS.ex, method="LCS")

So the answer is that you can pass only one row, but the rows have to come from the same state sequence object.

The arguments of the seqLLCS function should be sequences from state sequence objects with a same alphabet. In your case, the LCS.ex2 object has a much larger alphabet, which you can pass in the definition of the two other objects.

LCS.ex2 <- seqdef(LCS.ex2)
LCS.ex1 <- seqdef(LCS.ex1, alphabet=alphabet(LCS.ex2))
LCS.ex3 <- seqdef(LCS.ex3, alphabet=alphabet(LCS.ex2))


The following command produces then the expected result

seqLLCS(LCS.ex1[3,],LCS.ex2[1,])

[1] 7


Hope this helps.