I'm doing a project for bayesian inference based box-cox transformation data. I define a box-cox transformation function with nimble function, and give priors on box-cox transformation parameter lambda, and use loglikelihood control the sampling. The code cannot be complied into C++. I attached the code in this email. I think it is something wrong with boxcox transfomation code. Could you help me with this problem?
boxcox<- nimbleFunction(
run=function(raw=double(1),lambda=double(1),constant=double(1)){
returnType(double(1))
if (lambda == 0) {
return(log(raw - constant))
} else {
return(((raw - constant) ^ lambda - 1) / (lambda))
}
})
boxcoxfit<- nimbleCode({
for (i in 1:n){
y_star[i]<- boxcox(y[i],lambda,constant)
mu[i]<-beta0+beta1*x1[i]+beta2*x2[i]+beta3*x3[i]
#y_star[i]~dnorm(mu[i],var=sigma2)
logll[i]<-0.5*log(2*3.14)-0.5*log(sigma2)-1/sigma2*pow((y_star[i]-mu[i]),2)
}
ll<-sum(logll[1:n])
sigma2~dinvgamma(1,1)
beta0~dnorm(0,sd=10)
beta1~dnorm(0,sd=10)
beta2~dnorm(0,sd=10)
beta3~dnorm(0,sd=10)
lambda~dnorm(0,sd=10)
})
llFun <- nimbleFunction(
setup <- function(model) {},
run <- function() {
ll<-model$ll
returnType(double())
return(ll[1])
}
)
n=100
y=rnorm(100,0,1)
x1=rnorm(100,0,1)
x2=rnorm(100,0,1)
x3=rnorm(100,0,1)
data <- list(
y =y ,x1=x1,x2=x2,x3=x3
)
constants <- list(
n = n,constant=-10
)
inits <- list(lambda=0,sigma2=1,beta0=1,beta1=1,beta2=1,beta3=1)
Rmodel <- nimbleModel(boxcoxfit, data = data, constants = constants,inits=inits)
RllFun <- llFun(Rmodel)
mcmcConf <- configureMCMC(Rmodel, nodes = NULL)
mcmcConf$addMonitors(target = c("beta0","beta1", "beta2", "beta3", "sigma2","lambda"))
mcmcConf$addSampler(target = "beta0", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
mcmcConf$addSampler(target = "beta1", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
mcmcConf$addSampler(target = "beta2", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
mcmcConf$addSampler(target = "beta3", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
mcmcConf$addSampler(target = "sigma2", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
mcmcConf$addSampler(target = "lambda", type = 'RW_llFunction',
control = list(llFunction = RllFun,
includesTarget = FALSE))
Rmcmc <- buildMCMC(mcmcConf)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
## MCMC run
Cmcmc$run(1000)