【问题标题】:How to introduce noise into sequence data using TraMineR?如何使用 TraMineR 将噪声引入序列数据?
【发布时间】:2018-07-03 07:07:10
【问题描述】:

出于模拟的目的,我想随机更改序列数据集中的状态。目标是了解集群质量的不同度量如何在数据中不同程度的结构下表现。

如果我要介绍缺失,TraMineRextras 中有方便的seqgen.missing() 函数,但它只会添加缺失状态。我将如何随机选择一个比例pof 序列并随机插入一个随机选择的字母表元素,并以p_gp_lp_r 概率将它们插入到中间、左侧和对吧?

【问题讨论】:

  • 这是一个关于编码没有统计内容的问题。应该在 StackOverflow 上询问。在任何情况下,您都应该解释您想要更改序列中随机选择的元素的内容。到固定状态?随机选择的字母表元素?
  • 好的。我明白。请免费投票以将其迁移到正确的站点。我将进行编辑以澄清您提出的观点。

标签: r sequence-analysis traminer


【解决方案1】:

下面是一个seq.rand.chg 函数(改编自seqgen.missing),它将状态更改随机应用于p.cases 的序列比例。对于每个随机选择的序列,函数会随机改变状态

  1. p.gaps > 0时,在0p.gaps的位置之间的比例;

  2. p.left > 0和/或p.right > 0时,最多有p.leftp.right)比例的左(右)位。

seqgen.missing 函数一样,p.gapsp.leftp.right 是每个选定序列中发生变化的最大比例。 p_gp_lp_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 )

我们观察到更改应用于随机选择的第三个序列。

【讨论】:

    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    相关资源
    最近更新 更多