Page 1 of 1

Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Sun Nov 22, 2020 7:17 pm
by kofidlehman
Hi,
I am having trouble running Stat-jr 2LevelUnordered template in MCMCglmm and Rstan. I am able to run the Regression2 template in MCMCglmm but when I switch to the 2LevelUnordered template and try with MCMCglmm that fails. Switching to Rrstan also fails. I have attached the comments in the command file to see if I can get some help.
Stat-jr errors in MCMCglmm and Rstan.txt
(16.04 KiB) Downloaded 1027 times
Thanks for any help and best.
Kofi

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Mon Nov 23, 2020 2:49 pm
by ChrisCharlton
For your first model it looks like there was a problem generating the R script:

Code: Select all

Error: unexpected symbol in "Traceback (most recent"
Execution halted
Would you be able to post what is generated (it should be in an output with a name like script.R)?

For the Stan model it looks like the data is not being transferred in the expected way:

Code: Select all

Error in relevel.default(mydata$ideal, 3) :
  'relevel' only for (unordered) factors
Calls: relevel -> relevel.default
Execution halted
The relevel command is being used here to set which value is used as the base category, however the variable in question is not a factor variable. This may be a problem with your original data, or something that has changed in the way that R imports data since the original template was written. I would suggest testing what happens when you load the dataset directly into R to see how the different variables are imported.

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Fri Nov 27, 2020 12:06 pm
by kofidlehman
Hello Chris,
Thank you for the as usual quick prompt. Script R for the MCMCglmm is pasted below. But I am not sure I understand your comment about Rstan.

"The relevel command is being used here to set which value is used as the base category, however the variable in question is not a factor variable. This may be a problem with your original data, or something that has changed in the way that R imports data since the original template was written. I would suggest testing what happens when you load the dataset directly into R to see how the different variables are imported."

I am able to upload the data into Rstudio so I am not clear what I have to do to my data to get stat-jr's interface with rstan to read it. I am working to run the model in the rstudio's shell for rstan and hopefully gain some more insight about what might be the problem, nevertheless any additional clarity on your comment will be appreciated. Thanks a great deal for the superb assistance.
Kofi

Mcmcglmm Script R

Traceback (most recent call last):
File "C:\Program Files\StatJR\packages\R_MCMCglmm.py", line 232, in init
script = MakoTemplate(script).render(**args)
File "C:\Python27\lib\site-packages\mako\template.py", line 475, in render

File "C:\Python27\lib\site-packages\mako\runtime.py", line 882, in _render

File "C:\Python27\lib\site-packages\mako\runtime.py", line 919, in _render_context

File "C:\Python27\lib\site-packages\mako\runtime.py", line 946, in _exec_template

File "memory:0x17172da0L", line 100, in render_body
denom = sum(mydata$${n}) / length(mydata$${n})
File "C:\Python27\lib\site-packages\mako\runtime.py", line 229, in __str__

NameError: Undefined

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Fri Nov 27, 2020 1:20 pm
by ChrisCharlton
Looking at the template history it looks like we removed a question specifying a denominator variable as none of the packages called by the template support this, however we appear to have missed removing a piece of code that assumes that this has been specified. I believe that if you remove the following lines from the template:

Code: Select all

denom = sum(mydata$${n}) / length(mydata$${n})
if (denom == 1) {
write.dta(data.frame(${y}.new),file="newy.dta")
}
you should be able to eliminate the error when running MCMCglmm.

To clarify my comments about the Stan model:
  1. To set the base category for the response variable we use the relevel command.
  2. This requires the variable in question to be of type factor.
  3. We read the data from Stat-JR into R using the read.dta function in the foreign package.
  4. To automatically create factor variables from variables imported in this way (using the convert.factors
    option) they need to have value labels associated with them.

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Tue Dec 01, 2020 7:04 pm
by kofidlehman
Hello Chris,
Thank you again for the prompt response. This is where I am now.

Mcmcglmm:

1). For the MCMCglmm, I went into the template and entered the code you provided and that fixed the problem.
2). Unfortunately, It run into another problem having to do with the omitted category in the dependent and complained about relevel() not working . Same complaint that stan was giving. So using your comments on my Rstan problem as guide I changed this entry in the template "mydata$${y} <- relevel(mydata$${y}, ${base})" to "mydata$${y} <- relevel(as.factor(mydata$${y}), ${base})" right before "myModel <- MCMCglmm(${y} ~ -1 + trait + ${Rmmult(xs)}, random=~us(trait):${L2ID}, rcov = ~us(trait):units, family="categorical", prior=myprior, burnin=${burnin}, nitt=${iterations + burnin}, thin=${thinning}, data=mydata)" and that got me past that block to some results. Is that acceptable? I dont know r very well and google to find solutions so I need some assurance here. Thank you.

3). After getting results from Mcmcglmm, I noticed that MCMCglmm in stat-jr estimates the multinomial logit keeping the effect of the independent variable constrained across response categories. Is there a way to have the effect of the independent variable be unconstrained across responses in the 2levelUnordered template or do I have to look for another template?

Rstan:
1). For the rstan problem about relevel, I made the same modification that I made for MCMCglmm(namely added as.factor) to the relevel function and that seems to get me to some results. Again can you please let me know if that is acceptable? Best wishes.

Kofi

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Wed Dec 02, 2020 2:14 pm
by ChrisCharlton
Adding the as.factor() call into R should be fine as an alternative to having the variable converted when loaded into R. You'd just have to make sure that the variable is suitable for this yourself (i.e. contains a set of discrete integer values). The advantage of using value labels is that the different factor levels would have string values attached to them, which usually carry through to the outputs. It would of course be a good idea to check the output generated by R to ensure that it is working as expected.

For the unconstrained independent variable I think you would need to write your own R code, which you could base on that generated by the template. You would need to look at the documentation for MCMCglmm to see whether this is supported there, otherwise you should be able to fit the models with MLwiN via R2MLwiN (https://www.bristol.ac.uk/cmm/software/r2mlwin/). See chapter 10 of the User guide replication examples and chapter 12 of the MCMC guide replication examples at https://www.bristol.ac.uk/cmm/software/ ... /examples/ for more details.

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Wed Dec 16, 2020 9:14 am
by kofidlehman
Hi Chris,
This is a bit delayed but I did want to thank you for all the help provided

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Thu Mar 16, 2023 1:40 am
by plopblurt
Hi funny shooter 2
Keeping the effect of the independent variable limited across response categories, I found that MCMCglmm in stat-jr estimates the multinomial logit. Does the 2levelUnordered template allow for independent variable effects to be unbound among responses, or should I seek elsewhere?

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Mon Mar 20, 2023 10:19 am
by ChrisCharlton
Are you able to provide any further details regarding the models that you are trying to fit? Is the goal to fit them within MCMCglmm?

Re: Error running 2LevelUnordered Template in MCMCglmm and Rrstan

Posted: Tue May 23, 2023 7:13 am
by laurasmith
kofidlehman wrote: Tue Dec 01, 2020 7:04 pm
3). After getting results from Mcmcglmm, I noticed that MCMCglmm in stat-jr estimates the multinomial logit keeping the effect of the independent variable constrained across response categories. Is there a way to have the effect of the independent variable be unconstrained across responses in the 2levelUnordered template or do I have to look for another template?mapquest
It's good to know that modifying the 'relevel()' function by adding ,as.factor(), helped you progress in RStan as well. This modification should be acceptable, similar to what you did in MCMCglmm. By converting the variable to a factor before using 'relevel()', you ensure that the levels are correctly handled.