CovMat <- nimbleFunction( run = function(phi, dist = double(2), N, sigmasq) { # function for getting covariance matrix: sigmasq*exp(-phi*dist) CovM <- matrix(0, ncol = N, nrow = N) for (j in 1:N){ for (k in 1:N){ CovM[j, k] <- sigmasq * exp(-phi * dist[j, k]) } } returnType(double(2)) return(CovM[1:N, 1:N]) })
FullGPCode <- nimbleCode({ w[1:N] ~ dmnorm( meanv[1:N], cov = CovMat(phi, D[1:N, 1:N], N, 1 / invsigmasq))
for (i in 1:N){ y[i] ~ dnorm(inprod(x[i, 1:2], beta[1:2]) + w[i], sd = sqrt(1 / invtausq)) } # Hyperparameters: phi ~ dgamma(ap, rate = bp) # 1/range invsigmasq ~ dgamma(as, rate = bs) # partial sill invtausq ~ dgamma(at, rate = bt) # nugguts beta[1:2] ~ dmnorm(mu_b[1:2], V_b[1:2 , 1:2]) # regression coefficients})Error in checkMultivarExpr() : Error with parameter 'cov' of distribution 'dmnorm': multivariate parameters cannot be expressions; please define the expression as a separate deterministic variable and use that variable as the parameter.FullGPCode <- nimbleCode({ Covm[1:N, 1:N] <- CovMat(phi, D[1:N, 1:N], N, 1 / invsigmasq) w[1:N] ~ dmnorm( meanv[1:N], cov = Covm[1:N, 1:N])
for (i in 1:N){ y[i] ~ dnorm(inprod(x[i, 1:2], beta[1:2]) + w[i], sd = sqrt(1 / invtausq)) } # Hyperparameters: phi ~ dgamma(ap, rate = bp) # 1/range invsigmasq ~ dgamma(as, rate = bs) # partial sill invtausq ~ dgamma(at, rate = bt) # nugguts beta[1:2] ~ dmnorm(mu_b[1:2], V_b[1:2 , 1:2]) # regression coefficients})
compiling... this may take a minute. On some systems there may be some compiler warnings that can be safely ignored.Error in x[[1]] : object of type 'symbol' is not subsettableError: There is some problem at the setSizes processing step for this code: { model$Covm[1:200, 1:200] <<- CovMat(phi = model$phi[1], dist = model$D[1:200, 1:200], N = 200, sigmasq = 1/model$invsigmasq[1])}Covm[1:N, 1:N] <- CovMat(phi, D[1:N, 1:N], N, 1 / invsigmasq)w[1:N] ~ dmnorm( meanv[1:N], cov = Covm[1:N, 1:N])
--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/ef98c0cc-4b9d-47ae-a528-7fa109dd70fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
library("nimble")
CovMat <- nimbleFunction( run = function(phi = double(), dist = double(2), N = integer(), sigmasq = double()) { # function for getting covariance matrix: sigmasq*exp(-phi*dist) CovM <- matrix(NA, ncol = N, nrow = N) for (j in 1:N){ for (k in 1:N){ CovM[j, k] <- sigmasq * exp(-phi * dist[j, k]) } } returnType(double(2)) return(CovM[1:N, 1:N]) })
FullGPCode <- nimbleCode({
# This is the part I think fails to work properly: Covm[1:N, 1:N] <- CovMat(phi, D[1:N, 1:N], N, (1 / invsigmasq)) w[1:N] ~ dmnorm(meanv[1:N], cov = Covm[1:N, 1:N])
for (i in 1:N){ y[i] ~ dnorm(inprod(x[i, 1:2], beta[1:2]) + w[i], sd = sqrt(1 / invtausq)) } # Hyperparameters: parameters in blue do not get correct simulation phi ~ dgamma(ap, rate = bp) # 1/range invsigmasq ~ dgamma(as, rate = bs) # partial sill invtausq ~ dgamma(at, rate = bt) # nugguts beta[1:2] ~ dmnorm(mu_b[1:2], V_b[1:2, 1:2]) # regression coefficients })
Constants <- list(N = n, P = 2, ap = 12, bp = 1, as = 2 , bs = 1, at = 2, bt = 0.1, mu_b = c( 1 , 5 ), V_b = matrix (c(100, 0, 0, 10000), nrow = 2 , ncol = 2), D = D[1:n, 1:n], meanv = rep(0, n))
## list specifying model dataData <- list(y = y, x = X)
## list specifying initial valuesInits <- list(invsigmasq = 2, invtausq = 5, beta = c(5.0, 1.0), phi = 5, w = rep(0.01, n))
## build the R model objectFullGP_build <- nimbleModel(code = FullGPCode, name = 'FullGP', constants = Constants, data = Data, inits = Inits)
CFullGP <- compileNimble(FullGP_build)
################################################ MCMC configuration and building ################################################
## generate the default MCMC configurationFullGP_conf <- configureMCMC(FullGP_build, print = T)FullGP_conf$addMonitors(c('invsigmasq', 'invtausq', 'beta','phi', 'w'))
## check the samplers assigned by default MCMC configurationFullGP_conf$printSamplers()
## double-check our monitors, and thinning intervalFullGP_conf$getMonitors()
## build the executable R MCMC functionbuild_mcmc_FullGP <- buildMCMC(FullGP_conf)
######################################## compile to C++, and run ########################################
## compile the the modelCFullGPMCMC <- compileNimble(build_mcmc_FullGP, project = FullGP_build)
## run the default MCMC functionniter <- 5000set.seed(0)t<-proc.time()CFullGPMCMC$run(niter)proc.time() - t
Fsamples <- as.matrix(CFullGPMCMC$mvSamples)--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/cbab2cb9-05f5-4065-9519-f7f9dd182182%40googlegroups.com.
# define hierarchical model with BUGS codesiteHierarchyCode <- nimble::nimbleCode({
# --- # Define priors for hyperparameters # ---
# mean of means alpha ~ dnorm(mean = 0,sd = 1000)
# sd of means : approximates Jeffrey's prior (inverse prior) tau ~ dgamma(shape = 0.0001, rate = 0.0001)
# sd at each site : approximates Jeffrey's prior (inverse prior) sigma ~ dgamma(shape = 0.0001, rate = 0.0001)
# correlation length at each site : approximates Jeffrey's prior (inverse prior) lambda ~ dgamma(shape = 0.0001, rate = 0.0001)
# --- # Define hierarchical model at each site # ---
for (i in 1:I){ # for each site
# the mean is drawn from a univariate gaussian distribution mu[i] ~ dnorm(mean = alpha,sd = tau) # distribution of mean at site i mat_mu_i[i,1:J_i[i]] <- mu[i] * mat_ones[i,1:J_i[i]]
# the covariance matrix is defined by an exponential variogram function Sigma[1:J_i[i],1:J_i[i],i] <- sigma^2 * (1 - exp( -(d_ij[1:J_i[i], 1:J_i[i], i]/lambda) ))
# observations are multivariate normal y[i,1:J_i[i]] ~ dmnorm(mat_mu_i[i,1:J_i[i]], cov = Sigma[1:J_i[i],1:J_i[i],i])
}
})#'#' # Create NIMBLE model and run ##'
# actually create the modelsiteModel <- nimble::nimbleModel(code = siteHierarchyCode, constants = siteConst, data = siteData, inits = siteInit, name = 'siteModel')
# compile the modelCsiteModel <- nimble::compileNimble(siteModel)
# create compile and run MCMCsiteModelConf <- nimble::configureMCMC(model = siteModel, print=T, multivariateNodesAsScalars=T)
siteModelConf$addMonitors(c('alpha','tau','sigma','lambda'))
siteModelMCMC <- nimble::buildMCMC(siteModelConf)CsiteModelMCMC <- nimble::compileNimble(siteModelMCMC, project = siteModel)
CsiteModelMCMC$run(10^5)--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users+unsubscribe@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/98524c1f-bed7-4ee1-9b03-d5b008f6977d%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.