Hi everyone,
I’m a beginner and I need some help extracting fitted values from a CmdStanR model. Here is my model code:
data {
int n; //number of observations in the data
vector[n] mpg; //vector of length n for the car's MPG
vector[n] weight_c; //vector of length n for the car's weight
vector[n] cylinders_c; ////vector of length n for the car's cylinders
vector[n] hp_c; //vector of length n for the car's horsepower
}
parameters {
real alpha; //the intercept parameter
real beta_w; //slope parameter for weight
real beta_cyl; //slope parameter for cylinder
real beta_hp; //slope parameter for horsepower
real<lower = 0> sigma; //variance parameter and restrict it to positive values
}
model {
//linear predictor mu
vector[n] mu;
//write the linear equation
mu = alpha + beta_w * weight_c + beta_cyl * cylinders_c + beta_hp * hp_c;
//likelihood function
mpg ~ normal(mu, sigma);
}
generated quantities {
//replications for the posterior predictive distribution
real y_rep[n] = normal_rng(alpha + beta_w * weight_c + beta_cyl *
cylinders_c + beta_hp * hp_c, sigma);
}
With Rstan, I would just run:
y.rep <- extract(linear.fit.3)[["y_rep"]]
Then I can use bayesplot to plot the posterior predictive densities:
ppc_dens_overlay(y = cars.data$mpg, yrep = y.rep[1:100, ])
Where I’m stuck is that I’m not sure what the equivalent of extract
is for CmdStanR.
I appreciate any help and your patience!