if (FALSE) {
library (neodistr)
library(rstan)
#inputting data
set.seed(136)
dt <- neodistr::rmsnburr2a(100,0,1,0.5) # random generating MSNBurr-IIA data
dataf <- list(
n = 100,
y = dt
)
#### not vector
##Calling the function of the neo-normal distribution that is available in the package.
func_code<-paste(c("functions{",neodistr::stanf_msnburr2a(vectorize=FALSE),"}"),collapse="\n")
#define stan model code
model<-"
data {
int n;
vector[n] y;
}
parameters {
real mu;
real sigma;
real alpha;
}
model {
for(i in 1:n){
y[i]~msnburr2a(mu,sigma,alpha);
}
mu~cauchy(0,1);
sigma~cauchy(0,2.5);
alpha~cauchy(0,1);
}
"
#merge stan model code and selected neo-normal stan function
fit_code<-paste(c(func_code,model,"\n"),collapse="\n")
# Create the model using stan function
fit1 <- stan(
model_code = fit_code, # Stan program
data = dataf, # named list of data
chains = 2, # number of Markov chains
#warmup = 5000, # number of warmup iterations per chain
iter = 10000, # total number of iterations per chain
cores = 2 # number of cores (could use one per chain)
)
# Showing the estimation results of the parameters that were executed using the Stan file
print(fit1, pars=c("mu", "sigma", "alpha", "lp__"), probs=c(.025,.5,.975))
# Vector
##Calling the function of the neo-normal distribution that is available in the package.
func_code_vector<-paste(c("functions{",neodistr::stanf_msnburr2a(vectorize=TRUE),"}"),collapse="\n")
# define stan model as vector
model_vector<-"
data {
int n;
vector[n] y;
}
parameters {
real mu;
real sigma;
real alpha;
}
model {
y~msnburr2a(rep_vector(mu,n),sigma,alpha);
mu~cauchy(0,1);
sigma~cauchy(0,2.5);
alpha~cauchy(0,1);
}
"
#merge stan model code and selected neo-normal stan function
fit_code_vector<-paste(c(func_code_vector,model_vector,"\n"),collapse="\n")
# Create the model using stan function
fit2 <- stan(
model_code = fit_code_vector, # Stan program
data = dataf, # named list of data
chains = 2, # number of Markov chains
#warmup = 5000, # number of warmup iterations per chain
iter = 10000, # total number of iterations per chain
cores = 2 # number of cores (could use one per chain)
)
# Showing the estimation results of the parameters that were executed using the Stan file
print(fit2, pars=c("mu", "sigma", "alpha", "lp__"), probs=c(.025,.5,.975))
}
Run the code above in your browser using DataLab