rstanarm - error when running Bayesian model - r

EDIT - I have managed to resolve this. See my comment below
I am running a Bayesian regression in R using rstanarm using priors I have set, using the following code:
priors <- rstanarm::normal(location = c(-1, 0.5, 2), scale = c(1, 2, 0.5))
bmd <- stan_glm(s01_1 ~ pc + a03 + l01, data=bes19, prior = priors, iter=1000, seed=6942)
This outputs the following error, which I have no idea how to rectify:
Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=prior_scale; position=0; dims declared=(19); dims found=(3) (in '/data/hyperparameters.stan' at line 2; included from 'model_continuous' at line 56)
failed to create the sampler; sampling not done
Error in check_stanfit(stanfit) :
Invalid stanfit object produced please report bug
I would be grateful for any solutions, please. Thank you.

Related

Error in running Logit model clustered by contry code

I am just starting out in R and am trying to run a logit model with clutered country codes. My attempt is to replicate a model from this paper with the following replication information :
Data available here (data file): https://drive.google.com/file/d/1O2Gvf7sqxYN7IsOI2zTRZANlbLnVQvvw/view
*Model 1: base
logit mk l.wtrivalry mkyear mkyear2 mkyear3, cluster(ccode)
*Model 2: Controls -- no conflict
logit mk l.wtrivalry l.lnrgdppc l.polity2 l.exclid l.lnpopWB l.coupdummy mkyear mkyear2 mkyear3, cluster(ccode)
I have been trying variations of code, but keep failing to cluster correctly (unless I do so, the significance of the variables does not align with that of the paper). My attempts have been:
simp.logit.general. <- glm(mk ~ wtrivalry + mkyear + mkyear2 + mkyear3, data = core.data, family="binomial")
summary(simp.logit.general.)
robcov(simp.logit.general)
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'as.matrix': 'arg' should be one of “deviance”, “pearson”, “working”, “response”, “partial”
I have also tried:
coeftest(simp.logit.general, vcov. = vcovCL(simp.logit.general, cluster = core.data$ccode, type = "HC0"))
IF anyone could help with this, I would really appreciate it.

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.

Error Message when plotting ROC of H2O Model Object in R

Trying to plot ROC curve for H2O Model Object in R, however, I keep receiving the following error message:
"Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'"
My code is as follows:
drf1 <- h2o.randomForest(x=x,y=y,training_frame = train,validation_frame = valid, nfolds = nfolds, fold_assignment = "Modulo",keep_cross_validation_predictions = TRUE,seed = 1)
plot((h2o.performance(drf1,valid = T)), type = "roc")
I followed suggestions found here: How to directly plot ROC of h2o model object in R
Any help would be greatly appreciated!
From the error, I think your response variable is not binary. You can change your response variable to factor before putting it into model. i.e.
df$y <- as.factor(df$y)
"ROC is a graphical plot that illustrates the diagnostic ability of a binary classifier system as its discrimination threshold is varied".
source:
ROC wiki

r - Error message while using h2o.deeplearning

ERROR MESSAGE:
Illegal argument(s) for DeepLearning model: dl_model_faster.
Details: ERRR on field: _stopping_metric: Stopping metric cannot be misclassification for regression.
I am getting this error but actually I am using h2o.deeplearning for a classification problem, I don't want to run regression model. How can I specify that?
I had the same error for h2o.deeplearning(). Converting the dependent variable to factor and then feeding the data to h2o.deeplearning() fixed it for me.
dataset$dependent_variable= factor(dataset$dependent_variable,levels = c(0, 1), labels = c(0, 1))

Error with Generalized portmanteau tests for garch [gBox] in R

I am trying to use the gBox() function from the TSA package in R. I want to test the goodness of fit for a GARCH model. But when I try to run the function I get this error message:
Error in filter(M, filter = beta, method = "recursive", sides = 1,
init = rep(sigma2,dims [product 2] do not match the length of object
[1]**
The funny thing is that this is from an exact replica of the example that the package instructions provide, so there really shouldn't be any errors one would think. I get the same error message for my own data as well and I just don't know what to do.
Here is the example code:
library(TSA)
library(tseries)
data(CREF)
r.cref=diff(log(CREF))*100
m1=garch(x=r.cref,order=c(1,1))
summary(m1)
gBox(m1,x=r.cref,method='squared')
The length of the time series r.cref is 500 and the length of the garch m1 is 10, so they're obviously not same length, but how do I fix this error?

Resources