rjags model negative binomial likelihood and gamma prior - r

I read in my data. I make the model string. I hand it JAGS. I get "Error in node y[1] - Node inconsistent with parents".
Y=read.table("data.txt",header=T)
Y=Y$Y
model_string <- "model{
# Likelihood:
for( i in 1 : N ) {
y[i] ~ dnegbin( l , r )
}
# Prior:
r ~ dgamma(1,1)
l ~ dgamma(.1,.1)
}"
model <- jags.model(textConnection(model_string),
data = list(y=Y,N=200))
First off all, I have no clue if my model is right. I cannot find even basic documentation for JAGS. I'm actually ashamed to admit it, because this should be as simple as an internet search, but I cannot find any document to tell me 1) how a JAGS model is set up or 2) what kinds of functions/distribution/parameters are available in JAGS. I only got this far because I found someone doing a similar model. If anyone knows of a JAGS wiki or documentation, that would be great.
Edit: If someone could even just tell me what the parameters for dnegbin are that would be a huge help. When I plug in random numbers for l and r in dnegbin(l,r) it 'works' as in it draws numbers for l and r, but I have no clue if it means anything.

You can find some info about dnegbin in the JAGS user manual.
The first parameter of dnegbin must be between and 0 and 1. You can assign e.g. a uniform distribution:
library(rjags)
model_string <- "model{
# Likelihood:
for( i in 1 : N ) {
y[i] ~ dnegbin( l , r )
}
# Prior:
r ~ dgamma(1,1)
l ~ dunif(0,1)
}"
y <- rpois(200, 10)
model <- jags.model(textConnection(model_string),
data = list(y=y, N=length(y)))
You also have to be sure that the values of y are non-negative integers.

Related

Syntax error when trying to parse Bayesian model using RJAGS

I'm running the following code to attempt Bayesian modelling using rjags but coming up with the syntax error below.
Error in jags.model(file = "RhoModeldef.txt", data = ModelData, inits
= ModelInits, : Error parsing model file: syntax error on line 4 near "~"
RhoModel.def <- function() {
for (s in 1:S) {
log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))
rho[s] ~ dgamma(Kappa,Beta)
}
Kappa ~ dt(0,2.5,1) # dt(0, pow(2.5,-2), 1) https://stackoverflow.com/questions/34935606/cauchy-prior-in-jags https://arxiv.org/pdf/0901.4011.pdf
sig.k <- abs(Kappa)
Beta ~ dt(0,2.5,1)
sig.b <- abs(Beta)
}
S <- length(africasad21)-1 # integer
Rhohat <- afzip30$Rho # vector
Rhovar <- afzip30$RhoVar # vector
ModelData <-list(S=S,rhohat=Rhohat,rhovar=Rhovar)
ModelInits <- list(list(rho = rep(1,S),Kappa=0.1,Beta=0.1))
Model.1 <- jags.model(file = 'RhoModeldef.txt',data = ModelData,inits=ModelInits,
n.chains = 4, n.adapt = 100)
Does anyone have any ideas how I might be able to fix this? I'm thinking it might have something to do with my attempts to fit a logged model? Please let me know if more details are needed.
Thanks!
Line 4 of the file 'RhoModeldef.txt' is most likely this one:
log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))
JAGS does not allow log transformations on the left hand side of stochastic relations, only deterministic ones. Given that you are providing rhohat as data, the easiest solution is to do the log transformation in R and drop that part in JAGS i.e.:
log_rhohat[s] ~ dnorm(log(rho[s]), log(rhovar[s]))
ModelData <-list(S=S, log_rhohat=log(Rhohat), rhovar=Rhovar)
Alternatively, you could use dlnorm rather than dnorm in JAGS.
Does that solve your problem? Your example is not self-contained so I can't check myself, but I guess it should work now.

JAGS: variable number of clusters

I am trying to run a Bayesian clustering model where the number of clusters is random with binomial distribution.
This is my Jags model:
model{
for(i in 1:n){
y[ i ,1:M] ~ dmnorm( mu[z[i] , 1:M] , I[1:M, 1:M])
z[i] ~ dcat(omega[1:M])
}
for(j in 1:M){
mu[j,1:M] ~ dmnorm( mu_input[j,1:M] , I[1:M, 1:M] )
}
M ~ dbin(p, Mmax)
omega ~ ddirich(rep(1,Mmax))
}
to run it, we need to define the parameters anche the initial values for the variables, which is done in this R script
Mmax=10
y = matrix(0,100,Mmax)
I = diag(Mmax)
y[1:50,] = mvrnorm(50, rep(0,Mmax), I)
y[51:100,] = mvrnorm(50, rep(5,Mmax), I)
plot(y[,1:2])
z = 1*((1:100)>50) + 1
n = dim(y)[1]
M=2
mu=matrix(rnorm(Mmax^2),nrow=Mmax)
mu_input=matrix(2.5,Mmax,Mmax) ### prior mean
p=0.5
omega=rep(1,Mmax)/Mmax
data = list(y = y, I = I, n = n, mu_input=mu_input, Mmax = Mmax, p = p)
inits = function() {list(mu=mu,
M=M,
omega = omega) }
require(rjags)
modelRegress=jags.model("cluster_variabile.txt",data=data,inits=inits,n.adapt=1000,n.chains=1)
however, running the last command, one gets
Error in jags.model("cluster_variabile.txt", data = data, inits = inits,
: RUNTIME ERROR: Compilation error on line 6.
Unknown variable M Either supply values
for this variable with the data or define it on the left hand side of a relation.
which for me makes no sense, since the error is at line 6 even if M already appears at line 4 of the model! What is the actual problem in running this script?
So JAGS is not like R or other programming procedural languages in that it doesn't actually run line by line, it is a declarative language meaning the order of commands doesn't actually matter at least in terms of how the errors pop up. So just because it didn't throw an error on line 4 doesn't mean something isn't also wrong there. Im not positive, but I believe the error is occuring because JAGS tries to build the array first before inputting values, so M is not actually defined at this stage, but nothing you can do about that on your end.
With that aside, there should be a fairly easy work around for this, it is just less efficient. Instead of looping from 1:M make the loop iterate from 1:MMax that way the dimensions don't actually change, it is always an MMax x MMax. Then line 7 just assigns 1:M of those positions to a value. The downside of this is that it will require you to do some processing after the model is fit. So on each iteration, you will need to pull the sampled M and filter the matrix mu to be M x M, but that shouldn't be too tough. Let me know if you need more help.
So, I think the main problem is that you can't change the dimensionality of the stochastic node you're updating. This seems like a problem for reversible jump MCMC, though I don't think you can do this in JAGS.

R : Clustered standard errors in fractional probit model

I need to estimate a fractional (response taking values between 0 and 1) model with R. I also want to cluster the standard errors. I have found several examples in SO and elsewhere and I built this function based on my findings:
require(sandwich)
require(lmtest)
clrobustse <- function(fit, cl){
M <- length(unique(cl))
N <- length(cl)
K <- fit$rank
dfc <- (M/(M - 1))*((N - 1)/(N - K))
uj <- apply(estfun(fit), 2, function(x) tapply(x, cl, sum))
vcovCL <- dfc*sandwich(fit, meat = crossprod(uj)/N)
coeftest(fit, vcovCL)
}
I estimate my model like this:
fit <- glm(dep ~ exp1 + exp2 + exp3, data = df, fam = quasibinomial("probit"))
clrobustse(fit, df$cluster)
Everything works fine and I get the results. However, I suspect that something is not right as the non-clustered version:
coeftest(fit)
gives the exact same standard errors. I checked that Stata reports and that displays different clustered errors. I suspect that I have misspecified the function clrobustse but I just don't know how. Any ideas about what could be going wrong here?

R JAGS: invalid parent value in node (combining dcat and dnorm)

I am a newbie when it comes to using Jags and Rjags.
modelString = "
model {
for (i in 1:N){
y[i] ~ dt(mu,tau,nu)
}
tau <- nuless2/(sig^2)
nuless2 <- nu / nuless
nuless <- (nu-2)
sig ~ dnorm(0.02045457,10000000)
mu ~ dnorm(0.0013942308,4000000000)
nu ~ dcat(pi[])
pi <- c(0,0,3,3,3,3,3,3,3)
}"
writeLines(modelString,con="model.txt")
line_data = list("y"=ret,"N"=length(ret))
init_value = list("mu"=0.003)
model <- jags.model("model.txt", data=line_data, n.chains=2)
I have been struggling to get this code to work and I have no idea while it is giving me the error.
Error: Error in node nuless2
Invalid parent values
I experienced around to see what the problem lies and read most of discussion but I couldnt see any reason.
It would be great if someone can tell me where my stupid mistake is.
Also, I am quite new to JAGS and would like to know how the flow of the system works. For example, while I define y ~ dt(), I also supply data for y. By doing so, am I telling the system this is what the data is and all the parameter in dt() need to be "verified" using this data? It would be great to have a deeper understanding of such system.

R nls2 "invalid model formula" fitting gamma

Working in R 3.1.3 and Rstudio.
I want to fit gamma distributions that include a location parameter to data in order to 'shift' the x values to a new origin.
I am trying to use nls2 with the following code:
library(nls2)
theVals <- data.frame(c(26.76,24.3,34.63,38.05,25.56,21.98,20.62,34,26.75,27.79,28.4,33.31,29.26,18.65,22.77,25.72,25.86,25.32,24.08,27.68,26.2,26.16,25.34,26.91,22.6,23.94,23.3,22.34,41.25,24.83,21.66,30.47,26.53,27.74,29.41,25.65,36.05,18.29,27.2,22.99,25.8,21.9,25.27,30.29,22.72,26.49,18.75,33.57,20.87,21.82,20.73,28.59,19.64,33.21,28.94,27.98,22.2,25.95,30.64,26.56,32.11,26.05,20.66,28.64,22.4,22.4,31.91,21.82,26.82,20.77,24.12,28.83,23.07,26.5,21.14,27.29,19.61,25.28,28.6,27.16,22.46,18.19,22.35,23.79,26.32,26.5,27.39,23.29,25.79,26.35,26.38,24.98,20,37.15,25.61,21.39,21.63,24.12,24.4,27.72,42.74,25.33,17.79,21.33,38.65,25.22,28.39,21.61,23.38,25.25,24.88,23.34,26.26,21.96,22.18,24.78,21.15,24.65,21.23,31.9,28.66,27.66,18.08,22.99,22.46,21.69,28.21,29.8,25.72,27.09,20.02,21.26,21.34,27.18,25.48,20.51,20.96,20.07,20.89,27.56,24.43,21.35,24.3,28.1,26.53,29.03,30.08,19.19,21.27,26.18,23.79,36.52,24.81,26.36,24.44,20.99,19.84,23.32,18.21,26.6,21.48,23.21,29.93,23.4,30.9,23.58,21.58,18.38,25.13,23.03,22.73,24.42,22.89,43.44,23.47,27.09,29.96,23.94,28.51,25.74,28.54,30.41,22.7,29.19,25.66,23.89,21.9,36.26,22.61,19.68,27.85,28.83,28.6,22.68,19.07,20.22,24.35,19.09,37.66,22.55,24.25,22.61,26.09,24.42,26.11,32.15,25.78,21.94,23.93,30.19,23.53,26.49,30.48,25.02,28.14,23.43,20.22,17.57,21.68,36.07,24.92,32.48,32.04,25.86,26.69,22.41,26.4,22.72,28.32,22.82,32.73,28.08,29.16,36.18,21.61,23.9,28.8,23.24,24.89,22.17,27.7,34.75,26.74,29.62,17.46,20.06,22.23,22.09,24.05,22.37,24.98,33.26,30.95,26.24,22.16,30.97,27.22,23.81,42.16,28.2,28.37,26.1,26.28,27.44,20.52,35.02,21.43,23.14,18.37,28.86,25.18,28.15,19.97,24.2,25.91,28.92,23.95,19.48,28.57,21.77,23.46,37.51,22.13,37.18,21.83,23.8,18.93,27.43,26.51,25.64,22.15,22.27,29.21,24.45,18.81,22.62,25.16,24.62,30.53,28.77,27.11,22.07,28.95,26.54,39.23,31.9,33,29.93,24.37,26.4,21.33,25.37,25.9,21.25,19.06,25.69,26.44,26.09,23.24,27.04,20.09,28.73,37.06,32.45,22.93,22.7,24.82,31.23,23.25,22.94,20.47,25.7,23.92,34.71,26.5,20.28,21.78,26.54,30.34,21.97,27.38,27.64,34.08,22.05,27.21,20.11,25.79,33.22,31.24,29.93,21.81,30.68,32.46,30.45,22.62,28.83,33.95,27.12,45.51,25.23,29.61,29.09))
colnames(theVals) <- c("theGamma")
fo <- theGamma ~ dgamma(theX-location, shape=theShape, scale=theScale )
startList <- list(location=5, theShape=3, theScale=3)
theGamma=NULL
theX <- 0:50
mo1 <- nls2(fo, start=startList, data=theVals)
I get an error "invalid model formula in ExtractVars".
Curiosly dgamma works fine:
location<- 5
theShape <- 3
theScale <- 3
dgamma(theX-location, shape=theShape, scale=theScale )
I have search stackoverflow and other sites, but can't find an answer to this one.
Any ideas?

Resources