Problem with fitting a model

Hi. I am new to rstan and have some problem with the fitting of my model. I would appreciate if anyone have some input and some advice to help me. It was possible for me to run this code with the normal distribution but when I try to do it with student’s t-distribution I receive an error. I have the following code for my model:

write("// Stan model for simple linear regression

data {
 int < lower = 1 > N; // Sample size
 real<lower=0> nu; //Degrees of freedom
 vector[N] x; // Predictor
 vector[N] y; // Outcome
}

parameters {
 real alpha; // Intercept
 real beta; // Slope (regression coefficients)
 real < lower = 0 > sigma; // Error SD
 
    
}

model {
 y ~ student_t(nu, alpha + x * beta , sigma);
}

generated quantities {
} // The posterior predictive distribution",

"stan_model1.stan")

stanc("stan_model1.stan")

stan_model1 <- "stan_model1.stan"

fit <- stan(file = stan_model1, data = stan_data, warmup = 500, iter = 1000, chains = 4, cores = 2, thin = 1)

I receive the following error:
"Error in mod$fit_ptr() :
Exception: variable does not exist; processing stage=data initialization; variable name=nu; base type=double (in ‘modelcb6c70f3ed69_stan_model1’ at line 5)

failed to create the sampler; sampling not done".

Anyone knows what is wrong when I try to define my model? This is my first post here and I apologize if this is something that’s been discussed before, I couldn’t find any similar post.

The error is saying that you don’t have a variable called nu in the data that you’re passing to Stan, so you need to double-check your input data

Thanks for input! I have defined nu just like I did with x, y and N.

´´´
x ← (vanster$Valresultat_foregaende_val)
y ← vanster$Valresultat
N ← length(vanster$Valresultat)
nu ← 3
´´´
I know that I need to have the degrees of freedom above 2. But is this the right way to define nu?

Regards

How did you define the stan_data object that you’re passing to rstan?

“stan_data” was the problem. I have fixed it now. Thanks for the help!

1 Like