简体   繁体   中英

Markov switching model, R package, how to view source code

I am trying to look into the source code of an implementation of a general Markov switching model, in the MSwM package. I'm new to R, and I already have managed to look into the function msmFit().

However there is another function inside msmFit(), at the bottom called em(). I have not yet been able to view the source code of this inside function. Does anyone know how to view this em()-function?

Many thanks

Code and output

> 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"

If you're using the conventional R stack, git clone the MSwM project to your own machine and look in the R subdirectory. It looks like em is a generic (I'm not an expert on the R object models!) which is why it has a funny extended name. You may be able to get away with looking directly at the body() of .MSM.em , but I'm not sure if it will be visible from your R session.

Please consider changing the tags for this question as the technical aims of the package (as opposed to the details of code structure!) aren't very relevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM