Condition must be able to be evaluated based on values in 'constants

已查看 34 次
跳至第一个未读帖子

Wala Draidi Areed

未读,
2022年11月14日 22:13:192022/11/14
收件人 nimble-users
Hi all,

The explanatory variables in my code consist of 4 continuous variables and two categorical variables; each one has five levels. I converted the categorical variables to dummy variables where the baseline in both cases is level 1.

So I ended up with four continuous covariates, four dummy variables related to the first categorical variable and four dummy variables related to the second categorical variable in the model.
So b1,b2,b3, and b4 are the coefficients related to the continuous covariates.
b5,b6,b7,b8 related to the first categorical variable
b9,b10,b11,b12 related to the second categorical variable

I would like to have a local regression model for each region $i$ where the draws will be computed for the continuous and non-zero dummy variable coefficients.

I wrote the attached code, but I got an error.

Defining model
Error in eval(code[[2]], constantsEnv) : object 'hope' not found
Error in codeProcessIfThenElse(code[[i]], constants, envir) :
  Cannot evaluate condition of 'if' statement: hope[1] != 0.
Condition must be able to be evaluated based on values in 'constants'.



HOPE <- nimbleFunction(
  setup = function(i=double(0),x5=double(1),x6=double(1),x7=double(1),x8=double(1),x9=double(1),x10=double(1),x11=double(1),x12=double(1)){
    expr_beta <- c()
   
    if(x5[i]!=0){
      expr_beta <- c(expr_beta,5)
    }
    if(x6[i]!=0){
      expr_beta <- c(expr_beta,6)
    }
    if(x7[i]!=0){
      expr_beta <- c(expr_beta,7)
    }
    if(x8[i]!=0){
      expr_beta <- c(expr_beta,8)
    }
    if(x9[i]!=0){
      expr_beta <- c(expr_beta,9)
    }
    if(x10[i]!=0){
      expr_beta <- c(expr_beta,10)
    }
    if(x11[i]!=0){
      expr_beta <- c(expr_beta,11)
    }
    if(x12[i]!=0){
      expr_beta <- c(expr_beta,12)
    }
   
   
    # Three cases:
    # (a) first cat=1 & second cat=1
    if(length(expr_beta)==0){
      expr_beta <- c(0,0)
    }
    # (b) One non-baseline
    if(length(expr_beta)==1){
      expr_beta <- c(expr_beta,0)
    }
    # (c) Both non-baseline
   
    expr_beta
  },
 
  run = function(P = double(1)){
    return(P)
    returnType(double(1))
  })




GWRCode <- nimbleCode({
  for (i in 1:S){
   
    dn_inp <-
      b[i, 1] * x1[1:N] +
      b[i, 2] * x2[1:N] +
      b[i, 3] * x3[1:N] +
      b[i, 4] * x4[1:N]
   
    hope <- HOPE(i,x5,x6,x7,x8,x9,x10,x11,x12)
   
    if(hope[1] != 0){
      for(k in hope){
        if(k == 5){
          dn_inp <- dn_inp + b[i,k] * x5[1:N]
        } else if(k == 6){
          dn_inp <- dn_inp + b[i,k] * x6[1:N]
        } else if(k == 7){
          dn_inp <- dn_inp + b[i,k] * x7[1:N]
        } else if(k == 8){
          dn_inp <- dn_inp + b[i,k] * x8[1:N]
        }
        if(k == 9){
          dn_inp <- dn_inp + b[i,k] * x9[1:N]
        } else if(k == 10){
          dn_inp <- dn_inp + b[i,k] * x10[1:N]
        } else if(k == 11){
          dn_inp <- dn_inp + b[i,k] * x11[1:N]
        } else if(k == 12){
          dn_inp <- dn_inp + b[i,k] * x12[1:N]
        }
      }
    }
   
    y[1:N,i] ~ dmnorm(dn_inp, prec[1:N,1:N,i])
   
    for(j in 1:12){
      b[i, j] ~ dnorm(0, tau)
   
    }
    b0[i] ~ dnorm(0, 1)
  }
  tau ~ dgamma(0.001, 0.001)
 
})



Could you please advise in this regard?

Thanks,
Wala

Wei Zhang

未读,
2022年11月15日 04:32:032022/11/15
收件人 Wala Draidi Areed、nimble-users
Hi Wala,

The problem here is that you cannot use a user-defined function with the setup part in your model code. Your HOPE nimbleFunction has setup code and cannot be used. To avoid this issue, you could try to remove the setup part and put it into the run function if possible. Hope this helps. 

Best wishes,
Wei

--
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 on the web visit https://groups.google.com/d/msgid/nimble-users/4a39da93-4304-4c48-8fd6-88d45c1a4f02n%40googlegroups.com.

Wala Draidi Areed

未读,
2022年11月15日 17:26:542022/11/15
收件人 Wei Zhang、nimble-users
Hi Wei,

Thank you for your reply.

Could you please explain your point further.

I need to run HOPE function for each region $i$

So inside the nimblemodel I used

hope <- HOPE(i,x5,x6,x7,x8,x9,x10,x11,x12), but it seems that the nimble does not accept this line.

Thanks,
Wala

Perry de Valpine

未读,
2022年11月15日 19:11:332022/11/15
收件人 Wala Draidi Areed、Wei Zhang、nimble-users
Dear Wala,

I think it might be helpful for you to work through some of the model and nimbleFunction programming content of the User Manual at r-nimble.org.

A setup function executes *once*, in R (not compiled to C++) and does not need type declarations in the arguments.  But, as Wei noted, a nimbleFunction to be called from a model cannot have setup code at all.

It looks like what you have written as setup code is what you want to have executed each time it is called, so that would be appropriate to move to your run code.

Also you seem to want to return a vector, in which case you need explicit indexing in the model code, something like:

hope[1:2] <- HOPE(i,x5,x6,x7,x8,x9,x10,x11,x12)

Also the for loop over k in the model code does not look like it is going to work because you can't have if statements that use a model variable or an index as a condition.  The model language is declarative and declarations can't be conditional in that way.  And the extent of a for loop in model code definitely cannot use a model variable (e.g. for(k in hope) ).  And nodes (e.g. dn_inp) can't be declared more than once and can't use themselves (e.g. dn_inp <- dn_inp + ... ).  That all looks like imperative-style (i.e. most) programming.

If this is all still not clear after exploring the User Manual, I'd suggest sending a more minimal example to this list and explaining more thoroughly what you want.  I get that it is a piecewise model, but I'm not sure how you are trying to set it up.   Another resource is the Examples page at r-nimble.org or any resources on WinBUGS or JAGS.  There are many examples in these languages that show idioms for creating different kinds of models.

HTH
Perry




Wala Draidi Areed

未读,
2022年11月28日 02:11:392022/11/28
收件人 Perry de Valpine、Wei Zhang、nimble-users
Hi perry and Wei,

Thanks for your sugestions, I have tried to fix the issue in the code. But still the code does not work well.

I have attached the code and the data set. In the attached code, I have two categorical variables, each one consists from 5 levels, I have converted these categorical variables to dummy variables. so I endup with 14 explanatory variables in the data set,
b1,b2,b3,b4 related to the continuous variables.
b5,b6,b7,b8,b9 related to the first categorical variable.
b10,b11,b12,b13,b14 related to the second categorical variable.

For each region $i$, In need the code to run for the first 4 continuous variables ( b1,b2,b3,b4 ) and with one of ( b5,b6,b7,b8,b9) and one of ( b10,b11,b12,b13,b14) when the categorical variables non-zeros.
I got this error:
Error in rle(isScalar) : 'x' must be a vector of an atomic type

Could you please advice in this regard?

Thanks
clean_code_v2.R
NEW_DATA.csv

Wei Zhang

未读,
2022年11月30日 06:20:462022/11/30
收件人 Wala Draidi Areed、Perry de Valpine、nimble-users
Hi Wala,

I think the problem is still on the HOPE function. It seems that in your code the dimensionality of the output of HOPE is not for sure to be double(0) or double(1). NIMBLE is strict with dimensionality. If I am following, for each index i the dummy variables x1, x2, x3, x4, x5 will only have one x[i] that is not equal to zero. I rewrote this function a bit and now the code works. I also made some changes to your model code so that some deterministic nodes are not declared repeatedly in your for loop. See the attached files. Let me know if it gives you what you need. Hope it helps. 

Best wishes,
Wei
clean_code_v2_WZ.r

Wala Draidi Areed

未读,
2022年12月1日 23:25:452022/12/1
收件人 Wei Zhang、Perry de Valpine、nimble-users
Hi Wei,

Thanks you your help!

I am still struggling in the same issue, from your code I obtained draws even when the categorical variable is zero. I need the draws for the categorical variables when just equal to 1.  from the 14 covarites I should have draws for the first 4 continuous variable and one related to the first categorical variable and one related to the second categorical variable. So in total for each region $i$ I need to have 6 posteriors.
I could not find a way to add a constrain to the prior.
Also I have tried another way to solve the problem. but still I obtained the draws for all categorical variables even when the value is zero. Please find the attached file.

Could you please advice in this regard?

Thanks
Wala
cat_jags_way.R

Wei Zhang

未读,
2022年12月2日 06:18:332022/12/2
收件人 Wala Draidi Areed、Perry de Valpine、nimble-users
Hi Wala,

I do not get your thoughts here. In total you have 4 continuous variables (4 coefficients) and 2 categorical variables, but for the 2 categorical variables you define 10 dummy variables (5 for each). IMO, you should have 10 coefficients for your 10 dummy variables, and thus you should have 14 posterior distributions (i.e. b in your code), for each region i. Am I missing anything here? 

Best wishes,
Wei

Wala Draidi Areed

未读,
2022年12月3日 05:37:442022/12/3
收件人 Wei Zhang、Perry de Valpine、nimble-users
Hi Wei,
I am looking for a local linear model, For each region $i$ I need to get draws for the first four continuous variables and 2 categorical variables which are non-zero  ( or where the region belongs) so for each region, I should get 6 posteriors.

So in this case, I will not get draws that occurred when the categorical variables is zero.

In my first code, I use the HOPE function to make a list for each region $i$ to specify the covariates that I need to include.

In my second code, I tried to use index i.e b5[i, Rem[i]] to specify that to which covariate the draws should be obtained.

In both cases, I could not it use the if condition for the prior related to the categorical variables, and this is where I am still struggling.

Could you please advise in this regard?
Thanks,
Wala

Wei Zhang

未读,
2022年12月5日 05:46:572022/12/5
收件人 Wala Draidi Areed、Perry de Valpine、nimble-users
Hi Wala,

Does it make sense to use the following in your model code?
    dn_inp[i,1:N] <-

      b[i, 1] * x1[1:N] +
      b[i, 2] * x2[1:N] +
      b[i, 3] * x3[1:N] +
      b[i, 4] * x4[1:N] +
      b[i, 5] * RemX[1:N,hope1[i]] +
      b[i, 6] * IRSDX[1:N,hope2[i]]

and 
    for(j in 1:6){
      b[i, j] ~ dnorm(0, tau=sigmainv)
    }
This will only give you six posteriors for each region, and seems to match what you described. 

Best wishes,
Wei

回复全部
回复作者
转发
0 个新帖子