Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: matjags #214

Open
slewn opened this issue Apr 15, 2021 · 7 comments
Open

Error: matjags #214

slewn opened this issue Apr 15, 2021 · 7 comments

Comments

@slewn
Copy link

slewn commented Apr 15, 2021

Hi, I'm not sure whether this is still an active thread. I am currently trying to use your script to analyse some delay discounting data but I cant for the life of me seem to get it to work. I have both git and JAGS installed, the three dependencies installed and located in the MATLAB folder. I will leave you with the error message, if you can help me at all I'd be very appreciative as this is for my final year disseration study! :)

Error:File: matjags.m Line: 257 Column: 67
Identifier 'durationSeconds' is not a function or a shared variable. To share 'durationSeconds' with nested function,
initialize it in the current scope. For more information, see Sharing Variables Between Parent and Nested Functions.

Error in sampleWithMatjags (line 18)
[samples, stats] = matjags(...

Error in Model/conductInference (line 116)
obj.coda = samplerFunction(...

Error in ModelHierarchicalME (line 16)
obj = obj.conductInference();

Error in run_me (line 107)
model = ModelHierarchicalME(...

@drbenvincent
Copy link
Owner

Hi. So basically I've stopped using Matlab and have switched to Python and Julia. So I've stopped maintaining this package. It's also because it's vastly more complicated that it needs to be.

Having said that, we may still be able to get something sorted. The core thing is the JAGS model and the data. Not sure if you are familiar, but you can now run JAGS models in JASP. I've been meaning to try this out, so your message might be the catalyst.

If you are interested, then I'd be happy to message back and forth to get something working using JAGS within JASP. Let me know.

@slewn
Copy link
Author

slewn commented Apr 16, 2021 via email

@drbenvincent
Copy link
Owner

If you wanted to give it a go in Python, then I've got some example scripts (in Jypyter notebook form) which I've used for previous papers. I'm actually thinking that way simpler analysis scripts are the way to go - as opposed to toolboxes. It keeps the code simple and entirely transparent. Toolboxes get complex and opaque, perhaps overcoming the benefits of a simple API to ask for results. Examples of Python based analyses for some papers are:

  1. https://github.com/drbenvincent/Xu-GonzalezVallejo-Vincent
  2. https://github.com/drbenvincent/skrynka-vincent
  3. https://github.com/drbenvincent/Veillard-Vincent/blob/master/data/mcq_wcq_scoring/SCORING.ipynb

If you want to try the JASP+JAGS approach, I'd be happy to do that. If you are able to send me the data (just email me at my university of Dundee address) then I could work on that. If you can't (or don't feel like it would be appropriate) then just let me know:

  • are you interested in log(k) scores for people, or are you interested in the magnitude effect model which gives you m and c parameters?

Regardless of Python vs JAGS/JASP, you'd need your data in the following form:

  • all in one .csv file
  • each row is an experimental trial
  • column for participant number
  • columns for discounting: reward sizes A, B, and corresponding delays DA, DB, and response R (0 for immediate choice A, 1 for delayed choice B).
  • it doesn't matter if there are additional columns for things like conditions or other participant measures. And in fact that could be useful for doing more complex extended models.

@slewn
Copy link
Author

slewn commented Apr 21, 2021 via email

@drbenvincent
Copy link
Owner

drbenvincent commented Apr 21, 2021

I understand about the data, no problem. I've given this a go with a basic hyperbolic discounting model that gives you log(k) discount rates. If you've got your data sorted, then these steps should be simple, but let me know how you get on...

  1. Download latest version of JASP (I have 0.14.1)
  2. Open JASP. Click the big "+" icon in the top right and tick the JAGS box.
  3. Import your data with Open > Computer > Browse and point it towards your .csv file. Mine looks like this:

Screenshot 2021-04-21 at 19 36 59

  1. Create a JAGS analysis block by clicking on the JAGS icon in the top ribbon of JASP.
  2. Enter the following JAGS model code.
model{
  # set up priors for each participant
  for (p in 1:max(id))
  {
    logk[p] ~ dnorm(log(1/50), 1/(2.5^2))
    alpha[p] ~ dexp(0.01)
    epsilon[p] ~ dbeta(1.1, 10.9) T(,0.5)
  }

  # loop over trials (rows in the dataset)
  for (t in 1:length(R))
  {
    VB[t] <- B[t] / (1+(exp(logk[id[t]])*DB[t]))
    P[t] <- epsilon[id[t]] + (1-2*epsilon[id[t]]) * phi( (VB[t]-A[t]) / alpha[id[t]] )
    R[t]  ~ dbern(P[t])
  }  
}
  1. Press command-enter (for Mac) or whatever the PC equivalent it. This might run or it might ask for what variables you want to plot. The main one of interest is logk. I have something that looks like this:

Screenshot 2021-04-21 at 19 33 51

  1. After selecting those variables, you might need to command-enter again to make it work. With the default parameters it didn't take long on my laptop at all, but that was just with a quick demo file of 4 participants. If there are errors, make sure that the column headers which define the variables match up with what is in the JAGS model text. For example I have my participant column labelled as id (in lowercase)
  2. In the Plots tab in the JAGS analysis, click "Density" and it will show you the posterior distributions over the log discount rates, or whatever variables you have asked it to display.
  3. You also get a table of results. The main things of interest will be the logk values. There doesn't seem to be a way to export this into a file, so you might have to manually enter the posterior mean values of logk into a file for final analysis or further statistical tests you want to do. Obviously the logk[1], ..., logk[N] will correspond to the id, so make sure you don't mix up the order of participants if you are doing further stats on these scored log discount rates. The results for my test file of 4 participants looks like this.

Screenshot 2021-04-21 at 19 39 28

NOTE: The JAGS model assumes that the sooner reward is immediate (ie. DA=0). If that's not the case then let me know, but if you've used the Kirby set of questions then it should be. Also, this is a simple model, it doesn't do hierarchical inference.

I obviously don't know what your research question is about - but I'd suggest focussing on log discount rates (logk) to have a single measure of discounting for each person. But if your research questions require knowing about the magnitude effect, just let me know and I can adapt the JAGS model for that.

If you use this, can you cite this paper?

Vincent, B. T. (2016). Hierarchical Bayesian estimation and hypothesis testing for delay discounting tasks. Behavior Research Methods. 48(4), 1608-1620.

Just let me know if you run into issues.

@slewn
Copy link
Author

slewn commented Apr 22, 2021 via email

@drbenvincent
Copy link
Owner

For anyone else coming across this in the future, I have developed clearer and more extensive instructions here: https://github.com/drbenvincent/jasp_discounting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants