I’m following along with this vignette to perform K-fold cross validation for a logistic regression model. I’ve included my model code at the end of this post. At one point, the author does this:
fit <- sampling(stanmodel, data = data_train, seed = seed, refresh = 0)
gen_test <- gqs(stanmodel, draws = as.matrix(fit), data= data_test)
I do something similar, except I use stan
instead of sampling
:
fit <- stan(model_code = logisticRegressionModel, data = trainList,
iter=10000)
gen_test <- gqs(logisticRegressionModel, draws = as.matrix(fit), data= data_test)
When I do this, I get:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘gqs’ for signature ‘"character"’
I don’t quite understand what’s going wrong. But essentially what I’m trying to do is to compute the accuracy that I defined in generated quantities
for the test set using the model I fit using the train set.
logisticRegressionModel <- "
data {
int<lower=0> N; // number of data items
int<lower=0> K; // number of predictors
matrix[N, K] X; // predictor matrix
int y[N]; // outcome vector
}
parameters {
real alpha; // intercept
vector[K] gamma;
vector<lower=0>[K] tau;
vector[K] beta; // coefficients for predictors
}
model {
// Priors:
gamma ~ normal(0, 5);
tau ~ cauchy(0, 2.5);
alpha ~ normal(gamma, tau);
beta ~ normal(gamma, tau);
// Likelihood
y ~ bernoulli_logit(alpha + X * beta);
}
generated quantities {
vector[N] y_preds;
real correct = 0;
real accuracy;
for (n in 1:N) {
y_preds[n] = bernoulli_logit_rng(alpha + X[n] * beta);
correct += logical_eq(y_preds[n], y[n]);
}
accuracy = correct / N;
}
"