--
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 view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/7a90d35e-75bf-478d-8f9c-af4fa4132055%40googlegroups.com.
>> To unsubscribe from this group and stop receiving emails from it, send an email to nimble...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/7a90d35e-75bf-478d-8f9c-af4fa4132055%40googlegroups.com.
>
> --
> 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...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/05ef22de-307b-49a6-b8d8-d67ab0ec0213%40googlegroups.com.
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/213312d8-521f-4a1c-8962-12a2c1373e38%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/8ff30da2-a016-427d-8581-83630d0c7ad0%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/8ff30da2-a016-427d-8581-83630d0c7ad0%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/fdc25f83-7f74-43b8-a48d-29652769fffa%40googlegroups.com.
require(nimble)
require(ggplot2)
require(data.table)
timeModelCode <- nimbleCode({
# inital states
x[1,1] ~ dnorm(y[1], sd = obs_error) # C
x[1,2] ~ dnorm(0, sd = 5) # J
x[1,3] ~ dnorm(kw[1] * (Csat - x[1,1]), sd = 0.0001) # gas exchange
# state space evolution
for(i in 2:t){
x[i,1] ~ dnorm(x[i-1,1] + (x[i-1,2] * dT) + (x[i-1,3] * dT), sd = pro_error) # change in C
x[i,2] ~ dnorm(x[i-1,2], sd = var_J) # J, (random walk)
x[i,3] ~ dnorm(kw[i] * (Csat - x[i,1]), sd = 0.0001) # G
y[i] ~ dnorm(x[i,1], sd = obs_error) # observation
}
obs_error ~ dnorm(0, sd=5)
pro_error ~ dnorm(0, sd=1)
var_J ~ dnorm(0, 1) # how much J varies between steps
})
# simulate some data
t <- 48
pro_error <- 0.1
obs_error <- 0.5
x <- rep(300, t) # inital state + populate vector
y <- rnorm(1, x, obs_error) # first observation
dT = 600 # time between observations (seconds)
J = (8 * sin(0.03 * pi * 1:t) + 0.5) / dT # our unobserved latent state of interest
kw = rep(3e-05, t)
Csat = 250
G = rep(0, t)
G[1] <- kw[1] * (Csat - x[1])
for(i in 2:t){
x[i] <- x[i-1] + (J[i-1] * dT) + (G[i-1] * dT)
G[i] <- rnorm(1, kw[i] * (Csat - x[i]), 0.0001)
y[i] <- rnorm(1, x[i], obs_error)
}
truth = data.table(x, J, G, t = 1:t)
truth = melt(truth, id.vars = "t")
truth[, var := as.numeric(as.factor(variable))]
# y[10:20] = NA # for testing missing observations
plot(x, type="l")
points(y, col="red")
plot(J, type="l")
lines(G, col="blue")
plot(J + G, type="l", col="red")
# build the model
rTimeModel <- nimbleModel(timeModelCode, constants = list(t = t, dT = dT),
data <- list(y = y, kw = kw, Csat = Csat), check = T,
inits = list(var_J = 1, obs_error = 0.1, pro_error = 0.1)) # should x be init?
rTimeModel$initializeInfo()
cTimeModel <- compileNimble(rTimeModel)
timeConf <- configureMCMC(rTimeModel)
# Add random walk PMCMC sampler with particle number optimization.
timeConf$addSampler(target = c("var_J", "obs_error", "pro_error"), type = "RW_PF_block",
control <- list(pfType = "auxiliary", pfControl = list(saveAll = T, smoothing = T),
latents = c("x")))
timeConf$addMonitors("x")
RtimeConf <- buildMCMC(timeConf)
CtimeConf <- compileNimble(RtimeConf, project=rTimeModel, resetFunctions=T)
CtimeConf$run(5000)
# check top level parameters against truth
hist(as.matrix(CtimeConf$mvSamples, "obs_error"), breaks=100)
abline(v=obs_error, col="red")
hist(as.matrix(CtimeConf$mvSamples, "pro_error"), breaks=100)
abline(v=pro_error, col="red")
hist(as.matrix(CtimeConf$mvSamples, "var_J"), breaks=100)
abline(v=sd(diff(J)), col="red")
# extract time series latent state samples, if there is more elegant way of doing this I don't know it.
out = as.matrix(CtimeConf$mvSamples, "x")
out = melt(data.table(out))
out = out[,.(mean = mean(value, na.rm = F), min= quantile(value, 0.05, na.rm=F), max=quantile(value, 0.95, na.rm=F)), by=variable]
out[, t := stringr::str_extract_all(variable, "\\d+", simplify = T)[,1]]
out[, var := stringr::str_extract_all(variable, "\\d+", simplify = T)[,2]]
out[, t := as.numeric(t)]
#
ggplot(out) +
geom_ribbon(aes(t, ymin=min, ymax=max), alpha=0.5) +
geom_point(data=truth, aes(t, value)) +
facet_grid(var ~ ., scales = "free_y") +
geom_line(aes(t, mean))
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/fdc25f83-7f74-43b8-a48d-29652769fffa%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/8eba743f-4914-4426-b4fc-40ad14474354%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/8eba743f-4914-4426-b4fc-40ad14474354%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/40aa090b-44fc-4c20-ba86-491e013e8bba%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/40aa090b-44fc-4c20-ba86-491e013e8bba%40googlegroups.com.