0

我刚刚开始在我的论文项目中使用OpenBUGS软件和R2OpenBUGS软件包,R但我遇到了一个严重的问题。当我使用bugs()功能时,R控制台、OpenBUGS窗口中什么都没有发生,只是保持在相同的状态。我不确定该代码是否有帮助,但无论如何都是这个模型:

model{

    # Likelihood

    for(i in 1:n.cc){

    z[i] <- equals(reports.star[i], 0)  # I(reports = 0)

    logit(pi[i]) <- gamma[1]*intercept.cc[i] +
        gamma[2]*share[i] +
        gamma[3]*owner[i] +
        gamma[4]*accounts[i]

    mu[i] <- beta[1]*intercept.cc[i] +
        beta[2]*share[i] +
        beta[3]*owner[i] +
        beta[4]*accounts[i] +
        (1 - 2*tau)*w[i]/(tau*(1 - tau))

    prec[i] <- delta*tau*(1 - tau)/(2*w[i])

    log.like[i] <- z[i]*log(1 - pi[i]) + (1 - z[i])*(log(pi[i]) +
        0.5*log(prec[i]/(2*3.14159)) - 0.5*pow(reports.star[i] - mu[i], 2)*prec[i])

    #   zeros trick

    zeros[i] <- 0

    zeros[i] ~ dpois(lambda[i])

    lambda[i] <- -log.like[i] + 10000

    }

    # prior distributions

    for(i in 1:4){

    beta[i] ~ dnorm(0, 0.1)

    gamma[i] ~ dnorm(0, 0.1)
    }

    delta ~ dgamma(0.001, 0.001)


    for(i in 1:n.cc){w[i] ~ dexp(delta)}


}

这是主要代码:

################################################################################################
######  Defining responses and covariates from credit card data ################################
################################################################################################

library(AER)
data("CreditCard")
zeros <- which(CreditCard$reports == 0)
nonzero <- which(CreditCard$reports != 0)

n.cc <- length(CreditCard$reports)
reports.star <- CreditCard$reports
reports.star[nonzero] <- log(reports.star[nonzero] - runif(length(nonzero)))
save(reports.star, file = "CChurdle_reports.RData")

intercept.cc <- rep(1, n.cc)
share <- CreditCard$share
owner <- as.numeric(CreditCard$owner) - 1
accounts <- CreditCard$active

################################################################################################
######  Running R2OpenBUGS for Quantile Regression  ############################################
################################################################################################

library(R2OpenBUGS)
library(coda)

CC.data <- list("reports.star", "n.cc", "tau", "intercept.cc", "share", "owner", "accounts")

# initial values chosen from prior run at the median
CC.initials <- list(
  list(beta = c(0.1, -3, -0.5, 0.03), gamma = c(-1.7, -5, -0.5, 0.1), delta = 2.1,
       w = rep(0.5, n.cc)),
  list(beta = c(-0.2, -5, -1, 0), gamma = c(-2, -7.5, -1, 0), delta = 1.5, w = rep(1, n.cc)),
  list(beta = c(0.5, -1, 0, 0.1), gamma = c(-1, -2.5, 0, 0.2), delta = 3, w = rep(2, n.cc)))

CC.keeps <- list("beta", "gamma", "delta", "pi", "w")

###### 5th percentile ######

tau <- 0.05

CC.05 <- bugs(model.file = "CChurdle.txt", data = CC.data, inits = CC.initials,
                 n.iter = 7500, n.chains = 3, n.thin = 100, n.burnin = 500,
                 parameters.to.save = CC.keeps, DIC = FALSE)
save(CC.05, file = "CC05.RData")
rm(CC.05)

这就是我得到的:

在此处输入图像描述

这些代码取自一篇文章,可供公众使用。

希望你们帮帮我。谢谢

4

0 回答 0