Hi all, I am new to stan while I got the error message like this when initializing.

Chain 3: Rejecting initial value:

Chain 3: Error evaluating the log probability at the initial value.

Chain 3: Exception: normal_lpdf: Location parameter is nan, but must be finite! (in ‘model339674e1a53_f0754ff79b50e64febd5efec7c18a911’ at line 87)

Any suggestion would be great appreciated!

New_Stan.stan (2.1 KB) Rstan_NewStan.R (1.8 KB)

line 87 is :

y_ijk[(n-1)*5+k] ~ lognormal(mu[(n-1)*5+k], sigma);

*my Stan file is:*

//

// This Stan program defines a simple model, with a

// vector of values ‘y’ modeled as normally distributed

// with mean ‘mu’ and standard deviation ‘sigma’.

//

// Learn more about model development with Stan at:

//

// http://mc-stan.org/users/interfaces/rstan.html

// https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

//

// The input data.

data {

// Define variables in data

// Number of level-1 observations (an integer)

int<lower=1> N; //number of units

// Number of level-2 clusters

int<lower=1> K; //number of conditions

// RH from the data

real RH[N*K];
// Temp from the data
int<lower=1> T[N*K];

// Continuous outcome

vector[N

*K] y_ijk;//number of the outputs*

// Continuous predictor

real t_ijk[NK];

// Continuous predictor

real t_ijk[N

}

//Prepocessing of the data

transformed data{

real x1[N*K];
real x2[N*K];

x1[N

*K] = log(RH[N*K]);

x2[N

*K] = 11605/(T[N*K]+273.15);

}

// The parameters accepted by the model.

parameters {

// Define parameters to estimate

// Random effect

matrix[N,2] mat;

// Level-1

real<lower=1> sigma;

// Hyperparameters

vector[2] mu_mat;

real<lower=100> A[N*K];
real<lower=1> B[N*K];

real<lower=1> delta_H[N*K];

corr_matrix[2] sigma_mat;

corr_matrix[2] sigma_for_prior;

}

//Parameters processing before the postier is computed

transformed parameters{

// Random effect

real beta0[N];

real gamma[N];

row_vector[N*K] beta1;
row_vector[N*K] mu;

for(n in 1:N){

beta0[n] = mat[n,1];

gamma[n] = mat[n,2];

}

// Population slope

for(k in 1:K){

for(n in 1:N){

beta1[(n-1)

*5+k] = exp(log(A[n*k]) + B[n*k]*x1[(n-1)

*5+k] + delta_H[n*k]*x2[(n-1)*5+k]);

mu[(n-1)*5+k] = beta0[n] + beta1[(n-1)*5+k] * (t_ijk[(n-1)*5+k]^gamma[n]);

}

}

}

model {

sigma ~ gamma(1e-3,1e-3);

mu_mat[1] ~ normal(1e-3,1e-3);

mu_mat[2] ~ normal(1e-3,1e-3);

A ~ normal(0,1e-3);

B ~ normal(0, 1e-3);

delta_H ~ normal(0,1e-3);

mat[2] ~ multi_normal(mu_mat,sigma_mat);

sigma_mat ~ wishart(3,sigma_for_prior);

sigma_for_prior ~ lkj_corr(1);

for (k in 1:K){

for (n in 1:N){

y_ijk[(n-1)*5+k] ~ lognormal(mu[(n-1)*5+k], sigma);

}

}

}

in the R:

#load libraries

library(ggplot2)

library(StanHeaders)

library(rstan)

library(RColorBrewer)

#where the STAN model is saved

setwd("~/GitHub/Optical_media/")

## Data generation

set.seed(3117)

#set up the model data

N <- 90

K <- 5

# t_ijk <- ISO_data$Hours # Predictors

t_ijk <- rep(seq(100,2500,500),90)

y_ijk<- ISO_data$`Deg. data`

T <- ISO_data$Temp.

RH <- ISO_data$HR

logRH <- log(RH,base=exp(1))

#set the transformed data x1 and x2

x1_stan <- (logRH-log(40))/(log(85)-log(40))

head(x1_stan)

x2_stan <- (11605/(T+273.15)-11605/(15+273.15))/(11605/(85+273.15)-11605/(15+273.15))

head(x2_stan)

# Set up the initial value of parameters

mu <- rnorm(450,1,1)

sigma <- rnorm(450,7,2)

mat <- matrix(runif(450,1,3),nrow=N, ncol=2)

mu_mat <- matrix(runif(450,1,3),nrow=N,ncol=2)

sigma_mat <- matrix(runif(450,0,2),nrow=2, ncol=2)

A <- rnorm(90,100,1)

B <- rnorm(90,10,3)

delta_H <- rnorm(90,10,1.5)

sigma_for_prior <- matrix(c(1,1,1,1),2,2)

for(n in 1:N){

for(k in 1:K){

beta1[(n-1)*5+k] <- exp(log(A[n]) + B[n]*x1_stan[(n-1)*5+k] + delta_H[n]*x2_stan[(n-1)*5+k])

}

}

beta0 <- mat[,1]

gamma <- mat[,2]

# Set up the initial value of outcome

for(n in 1:N){

for(k in 1:K){

mu[(n-1)*5+k] <- beta0[n] + beta1[(n-1)*5+k] * (t_ijk[(n-1)*5+k]^gamma[n])

y_ijk[(n-1)*5+k] <- mu[(n-1)*5+k] + sigma[(n-1)*5+k]

}

}

# Set model data

stan_data <- list(N=N,

K=K,

t_ijk=t_ijk,

y_ijk=y_ijk,

RH=RH,

T=T,

x1=x1_stan,

x2=x2_stan)

# Load Stan file

fileName <- “New_Stan.stan”

stan_code <- readChar(fileName,file.info(fileName)$size)

# cat(stan_code)

# Run Stan

runStan <- stan(model_code=stan_code,data=stan_data,

chains = 3, iter = 3000, warmup = 500, thin = 10, init_r = .1)

print(runStan, pars=c(“mat”,“A”,“B”,“delta_H”,“sigma_mat”,“sigma”))