Invalid parent values jagsUI using beta distribution - r

I am trying to run a model in jags using jagsUI and I am getting this error message:
Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains, :
Error in node rec_sim[1]
Invalid parent values
I don't understand where is my mistake, any help is welcome.
Here is the data for my model
# Data
set.seed(1234)
year<-c(2017,2016,2008,2007)
n<-c(27,57,37,31)
preg<-c(5,24,12,12)
not.preg<-c(22,33,25,19)
prop.preg<-preg/n
mean.preg<-mean(prop.preg) #media de hembras preñadas de mis datos
fem.prop<-0.6 #proporción de sexos
sup<-0.607 #supervivencia
gua.den<-rlnorm(100,2.7,0.8)
recr<- function (ab2) {(mean.preg*fem.prop*sup*ab2)}
var<-0.1
#número de añales
rec2<- rlnorm(100, log(recr(gua.den),var))
#recruitment calculus
dens.an<-c(4.08,1.20,5.25,3.67)
real.ab.tot<- c(19.07,22.09,27.54,33.39)
ab.ant<-c(22.77,19.07,15.93,27.54)
an.fun<- mean.preg*ab.ant*fem.prop*sup
fem_rat<-gua.den*fem.prop
rec_sim<-rec2/fem_rat
rec_dens<-dens.an/(real.ab.tot*fem.prop)
rec_fun<-an.fun/real.ab.tot*fem.prop
And here is my model
library(jagsUI)
cat(file="rick.bug","
model{
for(i in 1:100){
rec_sim[i]~dbeta(a[i],b)
a[i]<-alpha*gua.den[i]*exp((-beta)*gua.den[i])
}
#priors
alpha ~dnorm(0,0.00001)
beta ~dnorm(0,0.00001)
b~dgamma(5,3)
}
")
d.jags<-c("rec_sim","gua.den")
params<-c("alpha","beta","logsigma")
inits <- function() {list(alpha = runif(1,0.1, 1),
beta = runif(1, 0.1, 1))}
ni<-1000; nc<-1; nt<-1; nb<-500
rick.sim <- jags(data = d.jags, parameters.to.save = params, model.file = "rick.bug",
n.chains = nc, n.iter = ni, n.burnin = nb, n.thin = nt)
print(rick.sim)

Related

XGBoost custom evaluation function causing "cannot coerce type closure to vector of type"

I have tried a number of different things but cannot get rid of this error message. Do not see how my code differs from numerous other scripts.
y_train = train$y
train$y = c()
train= as.matrix(train)
train = xgb.DMatrix(data = train, label = y_train)
MSE = function(yhat,train){
y = getinfo(train, "label")
err = mean((y-yhat)^2)
return(list(metric = "RMSE", value = err))
}
params = list(
eta = 0.1,
max_depth = 3,
tweedie_variance_power = 1.5,
objective = "reg:tweedie",
feval = MSE
)
model = xgb.cv(
data = train,
nfold = 3,
params = params,
nrounds = 2000
)
I get the following error:
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
I find the traceback a bit odd (see below). I use custom folds and xgb.cv is runnable if I remove the fevl and instead use the built in nloglike eval metric.
> traceback()
7: FUN(X[[i]], ...)
6: lapply(p, function(x) as.character(x)[1])
5: `xgb.parameters<-`(`*tmp*`, value = params)
4: xgb.Booster.handle(params, list(dtrain, dtest))
3: FUN(X[[i]], ...)
2: lapply(seq_along(folds), function(k) {
dtest <- slice(dall, folds[[k]])
dtrain <- slice(dall, unlist(folds[-k]))
handle <- xgb.Booster.handle(params, list(dtrain, dtest))
list(dtrain = dtrain, bst = handle, watchlist = list(train = dtrain,
test = dtest), index = folds[[k]])
})
1: xgb.cv(data = train, folds = folds, params = params, nrounds = 2000)
Any suggestions?
For what you need, passing it in params, through metric would work:
MSE = function(yhat,train){
y = getinfo(train, "label")
err = mean((y-yhat)^2)
return(list(metric = "MSEerror", value = err))
}
params = list(
eta = 0.1,
max_depth = 3,
tweedie_variance_power = 1.5,
objective = "reg:tweedie",
eval_metric = MSE
)
Using an example:
library(xgboost)
train = mtcars
colnames(train)[1] = "y"
y_train = train$y
train$y = c()
train= as.matrix(train)
train = xgb.DMatrix(data = train, label = y_train)
model = xgb.cv(
data = train,
nfold = 3,
params = params,
nrounds = 2000
)
head(model$evaluation_log)
iter train_MSEerror_mean train_MSEerror_std test_MSEerror_mean
1: 1 415.5046 20.92919 416.7119
2: 2 410.6576 20.78001 411.8646
3: 3 404.9321 20.59901 406.1391
4: 4 398.2114 20.38003 399.4192
5: 5 390.3808 20.11609 391.5902
6: 6 381.3338 19.79950 382.5464
test_MSEerror_std
1: 62.18317
2: 61.77277
3: 61.28819
4: 60.71951
5: 60.05671
6: 59.29019
There's something weird about passing it through params (you can try outside of params, it will work), can update later when I see how it's passed.

Problem adjusting a linear model with JAGS

I am trying to adjust a linear model with JAGS but I'm having trouble with the code. I'm writing:
library(R2jags)
library(BEST)
base<-data.table::data.table(read.csv("/Users/franco/Documents/Todo/UNAM/Facultad\ de\ Ciencias/Asignaturas\ Actuaría/Análisis\ Bayesiano\ de\ Datos/Tareas/Tarea-Examen\ 2/FootballLeague.csv"))
X <- cbind(1,as.matrix(base[,-c(1,2,12)]))
y <-as.matrix(base[,2])
n <- length(y)
m <- ncol(X)
model.jags <- function(){
tau ~ dgamma(0.01, 0.01)
for(i in 1:m){
beta[i] ~ dnorm(0,0.001)
}
for (i in 1:n){
y[i] ~ dnorm(x[i,]%*%beta,tau)
}
sigma <- pow(tau,-1)
}
jags.params <- c("beta","sigma")
jags.modelo <- jags(model.file=model.jags,parameters.to.save=jags.params,
data = list('n' = n,
'y' = y,
'x' = X,
'm'=m),
n.chains = 2,
n.thin=1,
DIC=FALSE,
n.burnin = 10000,
n.iter = 20000)
And R throws this error:
Error in jags.model(model.file, data = data, inits = init.values,
n.chains = n.chains, : RUNTIME ERROR: Compilation error on line 8.
Dimension mismatch in subset expression of y.
I don't know which is the error :/ Can someone help me, please.

Forecasting with GARCH(1,1) using ARMA(1,1) forecasting error

I am currently using the package rugarch to predict future volatility using out of sample data and keep on getting the errors:
When using the spec:
ugarchforecast(ug_spec, n.ahead = 1, n.roll = 499, data = ForeRFTSE[1:758, , drop = FALSE], out.sample = 500)
Error:
ugarchforecast-->error: parameters names do not match specification
Expected Parameters are: mu ar1 ma1 omega alpha1 beta1
or when use the fitted Parameter:
ugarchforecast(RFTSE_GARCH, n.ahead = 1, n.roll = 499, data = ForeRFTSE[1:758, , drop = FALSE], out.sample = 500)
Error:
Error in .sgarchforecast(fitORspec = fitORspec, data = data, n.ahead = n.ahead, :
ugarchforecast-->error: n.roll must not be greater than out.sample!
This is my ug_spec code
ug_spec <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1,1)),
mean.model = list(armaOrder=c(1,1),
include.mean=TRUE),
distribution.model = "norm" )
This is my RFTSE_GARCH
RFTSE_GARCH = ugarchfit(spec = ug_spec,solver = 'hybrid', data = RFTSE)

R Jags Multinomial error "length mismatch in node:setValue"

I am trying to run a multinomial model in rjags and I keep getting this error
Error in jags.model(model.file, data = data, inits = init.values,
n.chains = n.chains, :
Error in node dmulti(b,639)
Length mismatch in Node::setValue
Here is the code that creates the error:
n = c(294, 307,38)
m= c(288, 332,19)
x=1
y=1
z=1
model_string <-"model {
for(j in 1:3) {
n[j] ~ dmulti( a[1:3], 639)
m[j] ~ dmulti(b[1:3], 639)
}
a[1:3] ~ ddirich(c(x,y,z))
b[1:3]~ ddirich(c(x,y,z))
prob = step(b[1]-a[1])
RD = b[1]-a[1]
}"
jags.param <- c("a[1]","b[1]", "prob", "RD")
jags.data <- list(n=n,m=m, x=x, y=y,z=z)
jagsfit4 <- jags(data=jags.data, n.chains = 4, inits=NULL, parameters.to.save = jags.param,
model.file=textConnection(model_string), n.thin = 1 ,n.iter=5000,
n.burnin=1500, DIC=TRUE)
Thank you!

R: error with jags

library(rjags)
library(pcnetmeta)
data(smoke)
set.seed(1234)
hom.eqcor.out <- nma.ab.bin(s.id, t.id, r, n, data = smoke,
param = c("AR"), model = "het_cor", prior.type = "chol", c = 10,
higher.better = TRUE, n.adapt = 5000, n.iter = 100000, n.chains = 2)
I'm using the nma.ab.bin function in pcnetmet. However, I get the following error:
"Error in update.default(jags.m, n.iter = n.burnin) :
need an object with call component"
I've tried playing around with the n.adapt, and n.iter. Using n.adapt = 400 and n.iter = 1000 has no problem. But increasing those values seems to lead to the above error?

Resources