Error while using function 'get_posterior_mean'

Hello, all,

I am getting this strange error in R when I use: get_posterior_mean(fit,par=c(“sigma”))

Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘get_posterior_mean’ for signature ‘“matrix”’

I am really not sure what is happening here. Could anyone please help?

Regards
Antony.

Can anyone please help me here :)

The error pretty much tells you what the problem is. You are trying to apply the function to a matrix object, but there is no implementation that can work with matrix objects. I would need to see the rest of your code to help you further. But you probably want to use either a fit or draws object I’d guess.

Hello Scholz,

Thanks very much for your reponse.

Here is the Stan Code:

data {
int<lower=0> N;// Number of observations
int<lower=0> P; // Number of predictors in main regression
int<lower=0> J; // Number of groups
int<lower=0> T; // time
int<lower=0> K; // time
real Y[N]; // dependent variables
matrix[N,P] X; // matrix of independent variables
int<lower=1,upper=J> ID[N];
int<lower=1,upper=K> SIZE[N];
int<lower=1,upper=T> TIME[N];
}

parameters {
real alpha;
real<lower = 0> sigma_re;
real<lower = 0> sigma;
vector[J] re_raw;
real<lower = 0> sigma_beta;
vector[P] beta_raw;
real<lower = 1>nu;
real<lower = 0> sigma_mu_u;
real<lower = 0> sigma_u;
vector[N] mu_u_raw;

real<lower =0> max_u;
vector<lower=0.01, upper=max_u>[N] u;

real<lower = 0> sigma_mu_u_pe; 
vector[J] mu_u_pe_raw; 

real<lower =0> sigma_pe;
real<lower =0> max_u_pe;
vector<lower=0.01, upper=max_u_pe>[J]pe;

}

transformed parameters {
vector[P] beta = beta_raw*sigma_beta;

}

model {
vector[J] mu_u_pe = mu_u_pe_rawsigma_mu_u_pe;
vector[J] re = alpha + re_raw
sigma_re;
vector[J] RP = re + pe;
vector[N] mu_u = mu_u_rawsigma_mu_u;
vector[N] yhat = X
beta - RP[ID] - u;

sigma_beta ~ normal(0,5);

beta_raw ~ normal(0,1);

sigma ~ normal(0,1.5);

sigma_re ~ normal(0.01,1);

alpha ~ normal(0,3);

re_raw ~ normal(0,1);

sigma_mu_u ~ normal(0.01,1);

mu_u_raw ~ normal(0,1);

sigma_u ~ normal(0,1.2);

max_u ~ normal(0.10,1);

for (i in 1:N) {
u[i] ~ normal(mu_u[i], sigma_u) T[0,max_u];
}

sigma_mu_u_pe ~ normal(0.01,0.60);

mu_u_pe_raw ~ normal(0,1);

sigma_pe ~ normal(0,1.9);

max_u_pe ~ normal(0.01,0.50);

for (j in 1:J) {
pe[j] ~ normal(mu_u_pe[j], sigma_pe) T[0,max_u_pe];
}

nu ~ gamma(2,0.10);

Y ~student_t(nu,yhat, sigma);

}
generated quantities {
vector[N] eff_t;
vector[N] eff_pe;
vector[N] eff_total;
for (i in 1:N) {
eff_t[i] = exp(-u[i]);
eff_pe[i] = exp(-pe[ID[i]]);
eff_total[i] = eff_t[i]*eff_pe[i];
}
}

Then I run as usual in R:

FIT<- stan(
file = “C:/Users/anton/code_1.stan”, #
data = data_EE_TL,
chains =4,
iter=1000, # 70,000 will be suffice
cores= 4,
thin =1,refresh = 50,control = list( max_treedepth=12))

So code runs fine

but when I try to extract from generated quantities, using the following codes :

extract_eff= get_posterior_mean(FIT(“eff_pe”))
extract_EFF = (as.matrix(extract_eff[,5]))
summary(extract_EFF)

I get this error

Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘get_posterior_mean’ for signature ‘“matrix”’

Please help :)