문제

I have a list that contains 24 TraMineR sequence objects. Now I want to calculate the Optimal Matching distances for each of these sequence objects (only within each object) and store it in a new list, now consisting of 24 OM distance objects (distance matrices).

The dataset can be found here.

library(TraMineR)
sequences <- read.csv(file = "event-stream-20-l-m.csv", header = TRUE, nrows=10)
repo_names = colnames(sequences)

# 1. Loop across and define the 24 sequence objects & store them in sequence_objects
colpicks <- seq(10,240,by=10)
sequence_objects <- mapply(function(start,stop) seqdef(sequences[,start:stop]), colpicks-    9, colpicks)

# 2. Calculate the costs for OM distances within each object 
costs <- mapply(seqsubm(sequence_objects, method="TRATE"))

# 3. Calculate the OM distance objects for each sequence object
sequences.om <- seqdist(sequence_objects, method="OM", indel=1, sm=costs, with.missing=FALSE, norm="maxdist")

Step (1) works fine, but when I progress to step (2), it tells me:

Error in seqsubm(sequence_objects, method = "TRATE") : 
[!] data is NOT a sequence object, see seqdef function to create one

This is natural, because sequence_objects is not a sequence object, but a list of sequence objects.

How can I apply the seqsubm function to a list of sequence objects?

도움이 되었습니까?

해결책

I'm not familiar with the TraMineR package, however it looks like you are trying to iterate over the elements of sequence_objects.

mapply is for iterating over multiple objects simultaneously.
lapply in contrast is for iterating over a single object.

Therefore, the following might work for you:

 costs <- lapply(sequence_objects, seqsubm, method="TRATE")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top