Markov切换模型,R包,如何查看源代码

时间:2016-10-30 21:22:31

标签: r open-source

我试图在MSwM包中查看一般Markov切换模型的实现的源代码。我是R的新手,我已经设法调查函数msmFit()。

然而,msmFit()中还有另一个函数,底部名为em()。我还没有能够查看这个内部函数的源代码。有谁知道如何查看这个em() - 函数?

非常感谢

代码和输出

> getMethod(msmFit, signature = c(object="lm", k="numeric", sw="logical", p="ANY", data="missing", family="missing")):


Method Definition:

function (object, k, sw, p, data, family, control) 
{
    if (!missing(data)) {
        if (is.list(data)) {
            if (class(data[[1]]) == "call") {
                call = data[[1]]
            }
            else {
                call = match.call()
            }
        }
        else {
            call = match.call()
        }
    }
    else {
        call = match.call()
    }
    if (missing(p)) 
        p = 0
    if (missing(control)) 
        control = list()
    control <- do.call(msmControl, control)
    if (p > 0) {
        var = object$model[, 1]
        Ar = apply(as.matrix(1:p), 1, function(el) {
            length(var) = length(var) - el
            var = c(rep(NA, el), var)
            return(var)
        })
        colnames(Ar) = paste(names(object$model)[1], "_", 1:p, 
            sep = "")
        aux = paste(colnames(Ar), collapse = "+")
        object = update(formula = as.formula(paste("~.+", aux, 
            sep = "")), data = data.frame(object$model, Ar), 
            object)
    }
    Coef = data.frame(matrix(NA, nrow = k, ncol = length(coef(object))))
    std = rep(0, k)
    ind = sample(1:k, length(object$residuals), replace = T)
    for (i in 1:k) {
        data1 = as.data.frame(object$model[ind == i, , drop = F])
        mod1 = update(object, formula = object$terms, data = data1)
        Coef[i, ] = coef(mod1)
        std[i] = summary(mod1)$sigma
    }
    names(Coef) = names(coef(object))
    transMat = t(matrix(table(ind, c(ind[-1], NA))/rep(table(ind[-length(ind)]), 
        k), ncol = k))
    ans = new(Class = "MSM.lm", call = as.call(call), model = object, 
        k = k, switch = sw, p = p, Coef = Coef, std = std, transMat = transMat, 
        iniProb = rep(1/k, k))
    validMSM.linear(ans)
    validMSM.lm(ans)
    ans = em(ans, control)
    return(ans)
}
<environment: namespace:MSwM>

Signatures:
        object k         sw        p     data      family   
target  "lm"   "numeric" "logical" "ANY" "missing" "missing"
defined "lm"   "numeric" "logical" "ANY" "missing" "missing"

1 个答案:

答案 0 :(得分:0)

如果您正在使用传统的R堆栈,git clone MSwM 项目到您自己的计算机并查看R子目录。看起来em是一个通用的(我不是R对象模型的专家!),这就是为什么它有一个有趣的扩展名称。您可以直接查看body()的{​​{1}},但我不确定它是否会在您的R会话中显示。

请考虑更改此问题的标签,因为包的技术目标(与代码结构的细节相反!)不是很相关。