error: cannot convert 'NimArr<1, double>' to 'double'

26 views
Skip to first unread message

gesta...@gmail.com

unread,
Feb 14, 2025, 8:25:26 PM2/14/25
to nimble-users

I have a big complicated model that successfully builds and when I do Rmodel$calculate(), I get a finite value as expected. But while I compile, I get Error: Failed to create the shared library.

When I do printErrors(), it displays error: cannot convert 'NimArr<1, double>' to 'double'. It seems the problem might be related to initializing a vector parameter where I want the first values to be fixed to zero, values 3:7 to be informed by a prior, and vale 3 to be the mean of values 3:7.

In the model I specify the parameter as (note that the first value is not filled in - also there are other similar prameters, but I think if I solve this one,  I solve them all):

mu_m_gun ~ dgamma(1,1)
var_mf ~ dgamma(1,1)

for (k in 3:7) {
  beta_m_gun[k] ~ dgamma(mu_m_gun**2 * var_mf, mu_m_gun/var_mf)
}
beta_m_gun[2] <- mean(beta_m_gun[3:7])

In inits and data, following Daniel's recommendation in this post, I do:

inits = list(beta_m_gun = c(NA, rgamma(6,1,1)), …, other stuff)

data = list(beta_m_gun = c(0, rep(6,NA)), …, other stuff)

I'd appreciate if anyone can point out how or if I'm not specifiying this parameter correctly, or if something else might be going one. 

Below is the relevant output from printErrors(), if it is helpful. Also this parameter is used insdie a nimble fuction, and I do correctly specify the type as double(1). 

Thanks,
Glenn

> printErrors()
P_15_modelcode_nfCode.cpp: In member function 'virtual void y_icap_L78_UID_733::simulate(const indexedNodeInfo&) const':
P_15_modelcode_nfCode.cpp:13830:167: error: cannot convert 'NimArr<1, double>' to 'double'
13830 | (**model_y_icap)[0] = rcFun_R_GlobalEnv14(1, 63, Interm_4152, Interm_4153, Interm_4154, Interm_4155, Interm_4156, Interm_4157, Interm_4158, Interm_4159, Interm_4160, Interm_4161, Interm_4162, Interm_4163, Interm_4164, Interm_4165, Interm_4166, Interm_4167, Interm_4168, Interm_4169, Interm_4170, Interm_4171, Interm_4172, Interm_4173, Interm_4174, Interm_4175, Interm_4176, Interm_4177);
      |                                                                                                                                                                       ^~~~~~~~~~~
      |                                                                                                                                                                       |
      |                                                                                                                                                                       NimArr<1, double>
P_15_modelcode_nfCode.cpp:13250:326: note:   initializing argument 12 of 'int rcFun_R_GlobalEnv14(int, int, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, double, double, double, double, double, double, double, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, NimArr<1, double>&, double)'
13250 | int  rcFun_R_GlobalEnv14 ( int ARG1_n_, int ARG2_n_samples_, NimArr<1, double> & ARG3_e_, NimArr<1, double> & ARG4_r_, NimArr<1, double> & ARG5_s_, NimArr<1, double> & ARG6_z_, NimArr<1, double> & ARG7_sex_, NimArr<1, double> & ARG8_age2date_, double ARG9_beta0_sus_, double ARG10_beta0_inf_, double ARG11_beta_male_, double ARG12_beta_m_gun_, double ARG13_beta_f_gun_, double ARG14_beta_m_ng_, double ARG15_beta_f_ng_, NimArr<1, double> & ARG16_age_effect_surv_, NimArr<1, double> & ARG17_period_effect_surv_f_, NimArr<1, double> & ARG18_period_effect_surv_m_, NimArr<1, double> & ARG19_period_lookup_, NimArr<1, double> & ARG20_gun_lookup_, NimArr<1, double> & ARG21_ng_lookup_, NimArr<1, double> & ARG22_f_age_foi_, NimArr<1, double> & ARG23_m_age_foi_, NimArr<1, double> & ARG24_age_lookup_f_, NimArr<1, double> & ARG25_age_lookup_m_, NimArr<1, double> & ARG26_sect_, NimArr<1, double> & ARG27_space_, double ARG28_p_sens_ )  {
      | 

heather...@gmail.com

unread,
Feb 14, 2025, 9:59:33 PM2/14/25
to gesta...@gmail.com, nimble-users
Have you tried setting that value to a constant temporarily? something like beta-m_gun[2] <- 1  and then running the model? If you find you still have the error, then I expect it’s coming from some other part of your code. 

-Heather 


Sent from my iPhone; please excuse any typos

On Feb 14, 2025, at 8:25 PM, gesta...@gmail.com <gesta...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/fac283a7-5259-47e8-8ba2-471d3161b344n%40googlegroups.com.

Perry de Valpine

unread,
Feb 15, 2025, 7:44:01 AM2/15/25
to heather...@gmail.com, gesta...@gmail.com, nimble-users
Dear Glenn,
This error message would arise if somewhere you are trying to assign (or pass) a vector where a scalar is expected. If you have a variable in your model named y_icap on (approximately) line 78 of your model code, that is where the error is arising. (I can see that from the C++ generated class name "y_icap_L78_UID_733".)
HTH
Perry

gesta...@gmail.com

unread,
Feb 15, 2025, 12:59:04 PM2/15/25
to nimble-users
Thanks Perry (and Heather). 

I think I found the problem. Initially I mistakenly had the type specification as double(0) for that vector parameter in my nimble function, which generated a different error. I fixed the type specification to double(1), which is when the error mentioned above cropped up. This was really confusing, because I double and triple checked that all the type specifications were correct and that I was feeding a vector to a function that expected a vector. 
But I missed the following error when I re-registered my custom distribution (nimble function) after fixing the type specification:

[Warning] checkAndPrepareDistributionInfo: parameter arguments not the same amongst density and simulation functions for dIcap. Continuing anyway based on arguments to the density function; algorithms using the simulation function are unlikely to function properly.

Seeing this (and paying attention to it!)  made me realize that even though I fixed the type designation in my distribution function, it was the simulation function (which was previously auto-generated behind the scene) that apparently still expected a scalar. Closing R and starting over with everything correctly specified resolved the problem. So simple, but it took me quite awhile to realize it. 

Glenn

Reply all
Reply to author
Forward
0 new messages