Strange error: no slot of name "fieldPrototypes" for this object of class "classRepresentation"

Operating System: Windows 10 Enterprise
Interface Version: rstan 2.18.1
Compiler/Toolkit: I’m not sure what to write. I’m using “stan” function in R, and “options(error=recover)” is used to debug the error.

The following error comes when using “stan” function.

A strange thing: When I enter “2” to debug my own “FUN” function where “stan” is called in every iteration, and manually run the sentence of “stan” again, it works. Then, when I click “stop” (equivalently, enter “Q” into console and run), the iteration continues. What is the problem?

Thank you.

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :

no slot of name “fieldPrototypes” for this object of class “classRepresentation”

In addition: There were 50 or more warnings (use warnings() to see the first 50)

Enter a frame number, or 0 to exit

1: lapply(X = GMM.inner.parallel, FUN = MCMC.GMM)

2: FUN(X[[i]], …)

3: #485: stan(file = “GMM.stan”, data = list(V = length(x.prewhiten), C = Ck.mid, s = x.prewhiten, z = z.mid, alpha_w = delta.w,

4: sampling(sm, data, pars, chains, iter, warmup, thin, seed, init, check_data = TRUE, sample_file = sample_file, diagnostic_fil

5: sampling(sm, data, pars, chains, iter, warmup, thin, seed, init, check_data = TRUE, sample_file = sample_file, diagnostic_fil

6: .local(object, …)

7: object@mk_cppmodule(object)

8: eval(call("$", mod, paste(“stan_fit4”, model_cppname, sep = “”)))

9: eval(call("$", mod, paste(“stan_fit4”, model_cppname, sep = “”)))

10: new(“Module”, .xData = <environment>)$stan_fit4model7407add3a39_GMM

11: .getModulePointer(x)

12: Module(module, mustStart = TRUE)

13: methods::setRefClass(clname, fields = fields, contains = “C++Object”, methods = methods, where = where)

14: setClass(Class, contains = superClasses, where = where, …)

15: is(try(setIs(Class, class2, classDef = classDef, where = where)), “try-error”)

16: try(setIs(Class, class2, classDef = classDef, where = where))

17: tryCatch(expr, error = function(e) {

call <- conditionCall(e)

if (!is.null(call)) {

if (identical(call[[1]],

18: tryCatchList(expr, classes, parentenv, handlers)

19: tryCatchOne(expr, names, parentenv, handlers[[1]])

20: doTryCatch(return(expr), name, parentenv, handler)

21: setIs(Class, class2, classDef = classDef, where = where)

22: completeSubclasses(classDef2, class1, obj, where)

23: .transitiveExtends(class2, classDef@className, extensionDef, classDef@contains, strictBy)

24: .combineExtends(byExt, toExt, by, to, strictBy)

25: (function (cl, name, valueClass)

{

ClassDef <- getClass(cl)

slotClass <- ClassDef@slots[[name]]

if (is.null(slot

26: getClass(cl)

27: (function (x)

x$.self$finalize())(<environment>)

28: x$.self$finalize

29: envRefInferField(x, what, getClass(class(x)), selfEnv)

Don’t know. What is the error message you encounter when you just run it without the debugger and with cores = 1?

In “stan” function, cores is by default 1 so I dit not set it.

The previous 265 calls are successful, and then the 266th comes the same error (I clicked on “Show traceback”)

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
no slot of name “fieldPrototypes” for this object of class “classRepresentation”
30.
envRefInferField(x, what, getClass(class(x)), selfEnv)
29.
x$.self$finalize
28.
(function (x)
x$.self$finalize())()
27.
getClass(cl)
26.
(function (cl, name, valueClass)
{
ClassDef <- getClass(cl)
slotClass <- ClassDef@slots[[name]] …
25.
.combineExtends(byExt, toExt, by, to, strictBy)
24.
.transitiveExtends(className, by, ext[[i]], exti, strictBy)
23.
.walkClassGraph(ClassDef, “contains”, where, attr(ext, “conflicts”))
22.
completeExtends(classDef, class2, obj, where = where)
21.
setIs(Class, class2, classDef = classDef, where = where)
20.
doTryCatch(return(expr), name, parentenv, handler)
19.
tryCatchOne(expr, names, parentenv, handlers[[1L]])
18.
tryCatchList(expr, classes, parentenv, handlers)
17.
tryCatch(expr, error = function(e) {
call <- conditionCall(e)
if (!is.null(call)) {
if (identical(call[[1L]], quote(doTryCatch))) …
16.
try(setIs(Class, class2, classDef = classDef, where = where))
15.
is(try(setIs(Class, class2, classDef = classDef, where = where)),
“try-error”)
14.
setClass(Class, contains = superClasses, where = where, …)
13.
methods::setRefClass(clname, fields = fields, contains = “C++Object”,
methods = methods, where = where)
12.
Module(module, mustStart = TRUE)
11.
.getModulePointer(x)
10.
new(“Module”, .xData = )stan_fit4model11885f7d41da_GMM 9. eval(call("", mod, paste(“stan_fit4”, model_cppname, sep = “”)))
8.
eval(call("$", mod, paste(“stan_fit4”, model_cppname, sep = “”)))
7.
object@mk_cppmodule(object)
6.
.local(object, …)
5.
sampling(sm, data, pars, chains, iter, warmup, thin, seed, init,
check_data = TRUE, sample_file = sample_file, diagnostic_file = diagnostic_file,
verbose = verbose, algorithm = match.arg(algorithm), control = control,
check_unknown_args = FALSE, cores = cores, open_progress = open_progress, …
4.
sampling(sm, data, pars, chains, iter, warmup, thin, seed, init,
check_data = TRUE, sample_file = sample_file, diagnostic_file = diagnostic_file,
verbose = verbose, algorithm = match.arg(algorithm), control = control,
check_unknown_args = FALSE, cores = cores, open_progress = open_progress, …
3.
stan(file = “GMM.stan”, data = list(V = length(x.prewhiten),
C = Ck.mid, s = x.prewhiten, z = z.mid, alpha_w = delta.w,
sd_prior_mu_prime = sqrt(1/gamma.mu), shape_gamma_prime = kappa.sigma,
scale_gamma_prime = xi.sigma), init = list(list(w = c(w.mid), …
2.
FUN(X[[i]], …)
1.
lapply(X = GMM.inner.parallel, FUN = MCMC.GMM)


My R code to use stan function:

MH<-stan(file = ‘GMM.stan’, data=list(V=length(x.prewhiten),C=Ck.mid,s=x.prewhiten,z=z.mid,
alpha_w=delta.w,sd_prior_mu_prime=sqrt(1/gamma.mu),
shape_gamma_prime=kappa.sigma,scale_gamma_prime=xi.sigma),
init=list(list(w=c(w.mid),mu_prime=c(mu.prime.mid),sigma_prime=c(sigma.prime.mid))),
iter=NUTS.iter, warmup=NUTS.warmup, chains = 1, control=list(adapt_delta=NUTS.adapt_delta))

My stan code. I’m fresher.
If convenient for you, feel free to point out error if any.
Thank you.

data {
int<lower=0> V; // number of data points
int<lower=0> C; // number of GMM components
vector[V] s; // source vector
int<lower=0,upper=C> z[V];
real<lower=0> alpha_w;
real<lower=0> sd_prior_mu_prime; //mu’~N(0,sd_prior_mu_prime^2)
real<lower=0> shape_gamma_prime;
real<lower=0> scale_gamma_prime;
}
parameters {
simplex[C] w;
vector[C] mu_prime;
vector<lower=0>[C] sigma_prime;
}
transformed parameters {
vector[C] mu;
vector[C] mu_prime_prime;
vector<lower=0>[C] sigma;
real<lower=0> r;
mu_prime_prime=(mu_prime-mean(mu_prime))./ w;
r=sqrt(sum(w .* (mu_prime_prime .* mu_prime_prime+sigma_prime)));
mu=mu_prime_prime/r;
sigma=sqrt(sigma_prime)/r;
}
model {
w ~ dirichlet(rep_vector(alpha_w,C));
mu_prime ~ normal(0,sd_prior_mu_prime);
sigma_prime ~ inv_gamma(shape_gamma_prime,scale_gamma_prime);
z ~ categorical(w);
for(v in 1:V){
target += normal_lpdf(s[v] | mu[z[v]],sigma[z[v]]);
}
}

If it worked the first 255 times of your loop, then there is probably nothing wrong with your syntax. But you need to be very careful to not reload the compiled Stan model more than once by compiling it once outside the loop with stan_model and then inside the loop with sampling rather than stan.

1 Like

Thank you quite much, Dr. Goodrich.

It works now, and more efficient.