从概率后缀树中获取对数似然

Getting log-likelihood from probabilistic suffix tree

这是我的代码:

library(RCurl)
library(TraMineR)
library(PST)

x <- getURL("https://gist.githubusercontent.com/aronlindberg/08228977353bf6dc2edb3ec121f54a29/raw/c2539d06771317c5f4c8d3a2052a73fc485a09c6/challenge_level.csv")
data <- read.csv(text = x)

# Load and transform data
data <- read.table("thread_level.csv", sep = ",", header = F, stringsAsFactors = F)

data.seq <- seqdef(data[2:nrow(data),2:ncol(data)], missing = "NA", right = "*")

# Make a tree
S1 <- pstree(data.seq, ymin = 0.05, L = 6, lik = TRUE, with.missing = F)
logLik(S1)

出于某种原因,它拒绝 return 对数似然值?为什么会这样?如何获得对数似然值?

您的 seqdef 命令中的 missingright 参数值错误,这会导致 pstree.

中出现错误

data.seq <- seqdef(data[2:nrow(data),2:ncol(data)], missing = NA, right= NA, nr = "*")
# Make a tree
S1 <- pstree(data.seq, ymin = 0.05, L = 6, lik = TRUE, with.missing = TRUE)
logLik(S1)

我们得到

'log Lik.' -31011.32 (df=47179)

请注意,由于您缺少值,我在 pstree 命令中设置了 with.missing = TRUE

===============

要忽略右边的缺失,请在 seqdef 中设置 right='DEL'

seq <- seqdef(data[2:nrow(data),2:ncol(data)], missing = NA, right= "DEL")
S2 <- pstree(seq, ymin = 0.05, L = 6, lik = TRUE, with.missing = F)
logLik(S2)

我不知道 PST 的计算结果是 logLik(S2),也不知道为什么我们会得到一个 NA。用树S2生成数据的似然可以通过predict函数得到returns数据中每个序列的似然。数据的对数似然应为

sum(log(predict(S2, seq)))

这给出了

 [>] 984 sequence(s) - min/max length: 1/32
 [!] sequences have unequal lengths
 [>] max. context length: L=6
 [>] found 1020 distinct context(s)
 [>] total time: 0.588 secs
[1] -4925.79

确实,在计算适合不等长序列的模型的可能性时出现了问题。这是固定的。新版 PST 包 (0.94) 将在 R-Forge 几个小时内提供,安装:

install.packages("PST", repos="http://R-Forge.R-project.org") 

后来在 CRAN 上。

请注意,由于您的序列不包含任何缺失值但长度不等,因此在使用 pstree 函数时既不必设置 with.missing=TRUE 也不必在使用时设置任何选项seqdef.

现在当运行以下代码时:

library(RCurl)
library(TraMineR)
library(PST)

x <- getURL("https://gist.githubusercontent.com/aronlindberg/08228977353bf6dc2edb3ec121f54a29/raw/c2539d06771317c5f4c8d3a2052a73fc485a09c6/challenge_level.csv")
data <- read.csv(text = x)

data.seq <- seqdef(data[2:nrow(data),2:ncol(data)])

# Make a tree
S1 <- pstree(data.seq, ymin = 0.05, L = 6) 

我得到:

> S1@logLik
[1] -4925.79