22

我被困在以下代码中:

y = c(2.5, 6.0, 6.0, 7.5, 8.0, 8.0, 16.0, 6.0, 5.0, 6.0, 28.0, 5.0, 9.5, 
      6.0, 4.5, 10.0, 14.0, 3.0, 4.5, 5.5, 3.0, 3.5, 6.0, 2.0, 3.0, 4.0, 
      6.0, 5.0, 6.5, 5.0, 10.0, 6.0, 18.0, 4.5, 20.0) 

x2 = c(650, 2500, 900, 800, 3070, 2866, 7500, 800, 800, 650, 2100, 2000, 
       2200, 500, 1500, 3000, 2200, 350, 1000, 600, 300, 1500, 2200, 900, 
       600, 2000, 800, 950, 1750, 500, 4400, 600, 5200, 850, 5000) 

x1 = c(16.083, 48.350, 33.650, 45.600, 62.267, 73.2170, 204.617, 36.367, 
       29.750, 39.7500, 192.667, 43.050, 65.000, 44.133, 26.933, 72.250, 
       98.417, 78.650, 17.417, 32.567, 15.950, 27.900, 47.633, 17.933, 
       18.683, 26.217, 34.433, 28.567, 50.500, 20.950, 85.583, 32.3830, 
       170.250, 28.1000, 159.8330)

library(MASS) 
n = length(y) 
X = matrix(nrow = n, ncol = 2) 
for (i in 1:n) {
    X[i,1] = x1[i]
}

for (i in 1:n) {
    X[i,2] = x2[i]
} 
gibbs = function(data, m01 = 0, m02 = 0, k01 = 0.1, k02 = 0.1, 
                     a0 = 0.1, L0 = 0.1, nburn = 0, ndraw = 5000) {
    m0      = c(m01,m02) 
    C0      = matrix(nrow=2,ncol=2) 
    C0[1,1] = 1/k01 
    C0[1,2] = 0 
    C0[2,1] = 0 
    C0[2,2] = 1/k02 
    beta    = mvrnorm(1,m0,C0) 
    omega   = rgamma(1,a0,1)/L0 
    draws   = matrix(ncol=3,nrow=ndraw) 
    it      = -nburn 

    while (it < ndraw) {
        it    = it + 1 
        C1    = solve(solve(C0)+omega*t(X)%*%X) 
        m1    = C1%*%(solve(C0)%*%m0+omega*t(X)%*%y) 
        beta  = mvrnorm(1,m1,C1) 
        a1    = a0 + n/2 
        L1    = L0 + t(y-X%*%beta)%*%(y-X%*%beta) / 2 
        omega = rgamma(1, a1, 1) / L1 
        if (it > 0) { 
            draws[it,1] = beta[1]
            draws[it,2] = beta[2]
            draws[it,3] = omega
        }
    }
    return(draws)
}

当我运行它时,我得到:

Error in omega * t(X) %*% X : non-conformable arrays 

但是当我omega * t(X) %*% X在函数外部检查时,我得到一个结果,这意味着数组是一致的,我不知道为什么会出现这个错误。任何帮助将非常感激。

4

1 回答 1

28

问题是omega你的情况是matrix维度1 * 1。如果您希望乘以t(X) %*% X标量(即omega) ,则应将其转换为向量

特别是,您必须替换此行:

omega   = rgamma(1,a0,1) / L0

和:

omega   = as.vector(rgamma(1,a0,1) / L0)

在您的代码中无处不在。它发生在两个地方(一次在循环内,一次在循环外)。您可以替换as.vector(.)c(t(.))。两者是等价的。

这是应该可以工作的修改后的代码:

gibbs = function(data, m01 = 0, m02 = 0, k01 = 0.1, k02 = 0.1, 
                     a0 = 0.1, L0 = 0.1, nburn = 0, ndraw = 5000) {
    m0      = c(m01, m02) 
    C0      = matrix(nrow = 2, ncol = 2) 
    C0[1,1] = 1 / k01 
    C0[1,2] = 0 
    C0[2,1] = 0 
    C0[2,2] = 1 / k02 
    beta    = mvrnorm(1,m0,C0) 
    omega   = as.vector(rgamma(1,a0,1) / L0)
    draws   = matrix(ncol = 3,nrow = ndraw) 
    it      = -nburn 

    while (it < ndraw) {
        it    = it + 1 
        C1    = solve(solve(C0) + omega * t(X) %*% X) 
        m1    = C1 %*% (solve(C0) %*% m0 + omega * t(X) %*% y)
        beta  = mvrnorm(1, m1, C1) 
        a1    = a0 + n / 2 
        L1    = L0 + t(y - X %*% beta) %*% (y - X %*% beta) / 2 
        omega = as.vector(rgamma(1, a1, 1) / L1)
        if (it > 0) { 
            draws[it,1] = beta[1]
            draws[it,2] = beta[2]
            draws[it,3] = omega
        }
    }
    return(draws)
}
于 2013-02-09T21:11:17.660 回答