下面是一个seq.rand.chg 函数(改编自seqgen.missing),它将状态更改随机应用于p.cases 的序列比例。对于每个随机选择的序列,函数会随机改变状态
当p.gaps > 0时,在0和p.gaps的位置之间的比例;
当p.left > 0和/或p.right > 0时,最多有p.left(p.right)比例的左(右)位。
与seqgen.missing 函数一样,p.gaps、p.left 和p.right 是每个选定序列中发生变化的最大比例。 p_g、p_l 和 p_r 这些并不完全是您的概率。但是应该很容易为此调整功能。
函数如下:
seq.rand.chg <- function(seqdata, p.cases=.1, p.left=.0, p.gaps=0.1, p.right=.0){
n <- nrow(seqdata)
alph <- alphabet(seqdata)
lalph <- length(alph)
lgth <- max(seqlength(seqdata))
nm <- round(p.cases * n, 0)
## selecting cases
idm <- sort(sample(1:n, nm))
rdu.r <- runif(n,min=0,max=p.right)
rdu.g <- runif(n,min=0,max=p.gaps)
rdu.l <- runif(n,min=0,max=p.left)
for (i in idm){
# inner positions
gaps <- sample(1:lgth, round(rdu.g[i] * lgth, 0))
seqdata[i,gaps] <- alph[sample(1:lalph, length(gaps), replace=TRUE)]
# left positions
nl <- round(rdu.l[i] * lgth, 0)
if (nl>0) seqdata[i,1:nl] <- alph[sample(1:lalph, nl, replace=TRUE)]
# right positions
nr <- round(rdu.r[i] * lgth, 0)
if (nr>0) seqdata[i,(lgth-nr+1):lgth] <- alph[sample(1:lalph, nr, replace=TRUE)]
}
return(seqdata)
}
我们用mvad数据的前三个序列来说明函数的用法
library(TraMineR)
data(mvad)
mvad.lab <- c("employment", "further education", "higher education",
"joblessness", "school", "training")
mvad.shortlab <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvad.seq <- seqdef(mvad[, 17:62], states = mvad.shortlab,
labels = mvad.lab, xtstep = 6)
mvad.ori <- mvad.seq[1:3,]
## Changing up to 50% of states in 30% of the sequences
seed=11
mvad.chg <- seq.rand.chg(mvad.ori, p.cases = .3, p.gaps=0.5)
## plotting the outcome
par(mfrow=c(3,1))
seqiplot(mvad.ori, with.legend=FALSE, main="Original sequences")
seqiplot(mvad.chg, with.legend=FALSE, main="After random changes")
seqlegend(mvad.ori, ncol=6 )
我们观察到更改应用于随机选择的第三个序列。