-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
I think the main advantages of rcppbugs compared to JAGS and STAN is that user can have customized function in R in rcppbugs. Users can't define new functions as part of the modelling language in BUGS and JAGS. Although it could be done using low-level prgramming for all 3.
I would like to implement MCMC methods for parameter estimation and inference for a complex computer model (which takes parameters and generates simulated results), in this case TOXSWA.
In the script below, I defined a function toxswa which write the parameter values into input files, then run command line version of toxswa and extract the output. It seems working fine but the acceptance ratio from the ouput chain (300 runs) is 0. I am not sure where it went wrong.
parDat <- data.frame(name=c("kdomwb1","dt50wl","dt50wb"),
val=c(0.2183,100,100),lb=0,ub=Inf,fixed=c(1,0,0))
test.tpl <- "test.tpl"
obsDat <- read.table("CObserved.txt",skip=20)
names(obsDat) <- c("Time","Water","Sediment")
obsDat[1:2,3] <- NA
obsDat
library(reshape)
longObs <- melt(obsDat,id.vars=1) #!variable is obsGrp, value is Observed
con<-file(test.tpl,open="rt")
texts <- readLines(con)
pattern <- "\\Q@\\E[^)]*\\Q@\\E"
## pattern1 <- "\\([^)]*\\)"
toReplace <- regexpr(pattern,texts)
id <- which(toReplace>0)
ind1 <- toReplace[id]
ind2 <- attr(toReplace,"match.length")[id]
tmptext <- strsplit(texts[id],split="")
pnames <- sapply(1:length(id),function(i){
str <- tmptext[[i]]
str <- paste(str[(ind1[i]+1):(ind1[i]+ind2[i]-2)],collapse="")
parNms <- gsub("[[:space:]]", "", str)
parNms
})
close(con)
optPar <- c("dt50wl"=100,"dt50wb"=100)
logoptPar <- log(optPar)
toxswa <- function(par,fixPar=c("kdomwb1"=0.2183),tofile="C:/TOXSWA/Pesticides/fenamidone/SU.INP",
templateText,id,pnames=NULL,resFile="C:/TOXSWA/RUNS/Onga9707/ec001.out",...){
## from par to parDat
allPars <- c(exp(par),fixPar)
## Here should make sure that allPars has the same order as pnames.
if(!is.null(pnames)) if(any(names(allPars)!=pnames)) stop("Parameter vector should be rearranged!")
## from parDate to INPUT File
## Do not call genInput function to save some time.
pattern <- "\\Q@\\E[^)]*\\Q@\\E"
repPars <- as.character(format(allPars))
texts[id] <- sapply(1:length(id),function(i){
gsub(pattern,repPars[i],texts[id[i]])
})
fw <- file(tofile, open="wt")
writeLines(texts,fw)
close(fw)
system("run.bat")
## Extract Ouput!
yhat <- extractPred(resFile,model="TOXSWA",outlen=152/0.01,outInd=obsDat$Time/0.01+1,writeRes=FALSE)
return(yhat)
}
longObs <- melt(obsDat,id.vars=1)
y <- longObs$val
### MCMC Part
library(rcppbugs)
sepWatSed <- FALSE
if(sepWatSed) tau.y <- mcmc.gamma(
c(sd(obsDat$Water),sd(obsDat$Sediment,na.rm=TRUE)),
alpha=0.1,beta=0.1) else tau.y <- mcmc.gamma(sd(y,na.rm=TRUE),alpha=0.1,beta=0.1)
par <- mcmc.normal(rnorm(2),mu=log(313.7,77.1),tau=0.0001)
y.hat <- deterministic(toxswa,par,fixPar=c("kdomwb1"=0.2183),tofile="C:/TOXSWA/Pesticides/fenamidone/SU.INP",templateText=texts,id=id,pnames=NULL)
y.lik <- mcmc.normal(y,mu=y.hat,tau=tau.y,observed=TRUE)
m <- create.model(par, tau.y, y.hat, y.lik)
#runtime <- system.time(ans <- run.model(m, iterations=1e5L, burn=1e4L, adapt=2e3L, thin=10L))
runtime <- system.time(ans <- run.model(m, iterations=3e2L, burn=0, adapt=200, thin=2L))
print(apply(ans[["par"]],2,mean))
cat("acceptance.ratio:",get.ar(ans),"\n")
print(runtime)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels