I have this simple model and the generated quantities is returning and error: expression is ill formed around this line: vector[N] x;. what is happening here?
x= seq(1:100)
y = 3+.7*x + rnorm(100,2,10)
group = rep(c("A","B"),each=50)
dat = data.frame(x = x, y = y, group = group)
dat
plot(x,y)
summary(lm(y~x))
library(rstan)
standat <- list(
N = nrow(dat),
y = dat$y,
x = dat$x
)
stanmodelcode = '
data {
int<lower=1> N;
real y[N];
real x[N];
}
transformed data{
}
parameters {
real intercept;
real beta;
real<lower=0> sigma;
}
transformed parameters { // none needed
}
model {
real mu;
// priors
intercept~ normal(0,10);
beta ~ normal(0,10);
sigma ~ normal(0,10);
// likelihood
for(i in 1:N){
mu = intercept + beta*x[i];
y[i] ~ normal(mu, sigma);
}
}
generated quantities{
vector[N] x;
vector[N] yhat; // linear predictor
real<lower=0> rss; // residual sum of squares
real<lower=0> totalss; // total SS
real Rsq; // Rsq
yhat = x * beta;
rss = dot_self(y-yhat);
totalss = dot_self(y-mean(y));
Rsq = 1 - rss/totalss;
}
'
stan(model_code=stanmodelcode, data=standat, iter=10000, warmup=2000, chains = 1)