[英]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. 我正在尝试在MSwM软件包中研究通用Markov切换模型的实现的源代码。 I'm new to R, and I already have managed to look into the function msmFit(). 我是R的新手,我已经设法研究过函数msmFit()。
However there is another function inside msmFit(), at the bottom called em(). 但是,msmFit()内部还有另一个函数,称为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? 有谁知道如何查看此em()函数?
Many thanks 非常感谢
> 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. 如果您使用的是常规R堆栈,则git clone
将MSwM项目git clone
到您自己的计算机上,并查看R
子目录。 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. 看起来em
是一个泛型(我不是R对象模型的专家!),这就是为什么它有一个有趣的扩展名的原因。 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. 您可能可以直接查看.MSM.em
的body()
,但是我不确定在您的R会话中是否可以看到它。
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. 请考虑针对此问题更改标签,因为该软件包的技术目标(与代码结构的详细信息相反)并不十分相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.