Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

problems writing a space state model

$
0
0

I am trying to fit a state-space model. I started with a simple model which included only one variable but, since that model did not explained one of my parameters, I am trying to include another variable to it. Here is the first model:


o<-c(22.77619, 19.07782, 22.08817, 16.32168, 32.57081,NA, 10.48027, 15.93440, 27.54557, 33.39933)

#it seems that this model needs negative values for the 

cat(file="pop.din","
model {
  t[1] <- n0
  o[1] ~ dlnorm(log(t[1]),tau.obs)
  for (i in 2:9) {
  v[i] <- t[i-1]+r*t[i-1]*(1-t[i-1]/K) #valor esperado
  t[i] ~ dpois(v[i]) #t valor verdadero
  o[i] ~ dlnorm(t[i],tau.obs)
  }
  r ~ dnorm(0,0.001) 
  K ~ dgamma(1,10)
  tau.obs ~ dgamma(1,10)
  n0 ~ dlnorm(1,0.0001)
}")

pop.din.data<-c("o")

#initial values for the parameters stored as a list
inits<-function()list(r=runif(0.01,1),K=rlnorm(1,1,1),tau.obs=rlnorm(1,1,1),n0=rlnorm(1,1,1))

params<- c("K","r","n0","tau.obs")

ni <- 100000
nt <-1
nb <- 50000
nc <- 3



library(jagsUI)
j.model   <- jags (model.file = "pop.din", data = pop.din.data,parameters.to.save = params,
                 inits=inits, n.burnin=nb,n.chains = nc,n.iter = ni)


print(j.model)

When I try to add another variable, the model the model is as follows:

o<-c(22.77619, 19.07782, 22.08817, 16.32168, 32.57081,NA, 10.48027, 15.93440, 27.54557, 33.39933)
evi<-c(0.07289889,0.06288981,0.065947587,0.05886781,
       0.07037986,0.07256388,0.06540081,0.07219641,0.0798039,0.08368564)

cat(file="pop.din","
model {
  t[1] <- n0
  K[1]<-K0
  o[1] ~ dlnorm(t[1],tau.obs)
  evi[1]~ dunif (K[1],tau.k)
  obs_K[1]~ dlnorm(K[1],tau.k)     

  for (i in 2:9) {
  v[i] <- t[i-1]+r*t[i-1]*(1-t[i-1]/K[i-1]*alpha      #valor esperado
  t[i] ~ dpois(v[i])                            #t valor verdadero
  evi[i]~ dbeta (K[i],b.k)
  o[i] ~ dlnorm((t[i]),tau.obs)
  }

  r ~ dnorm(0,0.001) 
  K0 ~ dlnorm(1,0.001)
  tau.obs ~ dgamma(1,10)
  b.k ~ dbeta(2,2)
  alpha ~ dlnorm (1,0.001)
  n0 ~ dlnorm(1,0.0001)
}")

pop.din.data<-c("o","evi")

#initial values for the parameters stored as a list
inits<-function()list(r=runif(0.01,1),K0=rlnorm(1,1,1),tau.obs=rlnorm(1,1,1),n0=rlnorm(1,1,1))

params<- c("K","r","n0","tau.obs","alpha")

ni <- 10000
nt <-1
nb <- 5000
nc <- 3

library(jagsUI)
j.model   <- jags (model.file = "pop.din", data = pop.din.data,parameters.to.save = params,
                   inits=inits, n.burnin=nb,n.chains = nc,n.iter = ni)

print(j.model)

But with this last version, I am having a syntax error on line 11 near "t" that I can't find. Any idea of why this can be?


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>