R:输入尺寸误差(bayesm:`rmvpGibbs()`)

时间:2016-01-06 21:30:16

标签: r exception matrix statistics bayesian

我是 bayesm 包的新手(通常是贝叶斯建模)。我有一个用例激发了当前的努力:需要一个我可以用Python包装的多变量概率模型。 bayesm 包提供了这样一种方法rmvpGibbs()。但是我遇到了一些困难,因为我一直抱怨异常位置参数(V)是“不正确的维度”。

我只是尝试使用库中的 Scotch 数据进行玩具设置,然后再转到我的用例。这是我的设置。

#Capture number of equations (scotches)
p<-ncol(Scotch)

#Capture number of observations
n<-nrow(Scotch)

#Capture the number of covariates (coefficients to be estimated)
d<-1

#Capture regressor data
z<-matrix(d)

#Construct design matrix (kronecker product of d-dimensional array of regressors and Ip for each equation)
X3_tmp<-z %x% diag(p)
X3<-X3_tmp
for(i in 2:n) {X3=rbind(X3,X3_tmp)}

#Capture number of columns k
k<-ncol(X3)

#Convert to long format
scotch_long<-melt(Scotch)

#Capture dependent data
y3<-scotch_long$value

#Capture regressors
#X3<-matrix(1,length(y3),1)

#Consolidate in list
Data3=list(p=nrow(Scotch),y=y3,X=X3)

#Capture priors for coefficients
betabar3<-c(rep(0,k))
A3<-0.01*diag(k)

#Capture priors for variance (start with default)
nu3<-(n-1)+3
V3<-nu3*diag(p)

#Consolidate in list
Prior3=list(betabar=betabar3,A=A3,nu=nu3,V=V3)

#Define initial values
beta0_3<-rep(0,p)
sigma0_3<-diag(p)
sigma0_3[lower.tri(sigma0_3)]<-.5
sigma0_3[upper.tri(sigma0_3)]<-.5

#Define number of iterations
R3<-10000

#Define keep behavior
keep3=1

#Consolidate in list
MCMC3=list(beta0=beta0_3,sigma0=sigma0_3,R=R3,keep=keep3)

#Fit model
simOut3=rmvpGibbs(Data3,Prior3,MCMC3)

rmvpGibbs()的调用失败,异常为"V is of incorrect dimension"。但是,当我直接检查条件时(来自源头)......

if(sum(dim(V)==c(p,p)) != 2) pandterm("V is of incorrect dimension")

...我发现sum(dim(V)==c(p,p))确实等于2(如预期的那样)。任何关于为什么失败的建议将不胜感激。 (如果您有这种倾向,请随意批评该设置。)

1 个答案:

答案 0 :(得分:0)

我认为你需要改变这个:

nu3<-(n-1)+3

对此:

nu3<-(p-1)+3

由于nu的定义是:

nu d.f. parm for IWishart prior (def: (p-1) + 3)

有关详细信息,请参阅https://cran.r-project.org/web/packages/bayesm/bayesm.pdf

相关问题