Error with sim when using shorthand in quap

Open
#473 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
r
Domain
data

Research direction

Start by reproducing the error: build the quap model with the shorthand c(bM, bA) ~ dnorm(0, 0.5) in place of the separate priors, using straight quotes in the vars argument, then call sim on it. The error text if (left == var) { suggests the code that parses the model formulas assumes a single left-hand name, so look there. Done means the shorthand model runs through sim and gives the same results as the expanded form.

Written by the indexing model from the issue text.

Description

I was completing the exercise 5H2 which makes reference to a function on page 140:

data(WaffleDivorce)
d <- list()
d$A <- standardize( WaffleDivorce$MedianAgeMarriage )
d$D <- standardize( WaffleDivorce$Divorce )
d$M <- standardize( WaffleDivorce$Marriage )
m5.3_A <- quap(
 alist(
  ## A -> D <- M
  D ~ dnorm( mu , sigma ) ,
  mu <- a + bM*M + bA*A ,
  a ~ dnorm( 0 , 0.2 ) ,
  bM ~ dnorm( 0 , 0.5 ) ,
  bA ~ dnorm( 0 , 0.5 ) ,
  sigma ~ dexp( 1 ),
 ## A -> M
  M ~ dnorm( mu_M , sigma_M ),
  mu_M <- aM + bAM*A,
  aM ~ dnorm( 0 , 0.2 ),
  bAM ~ dnorm( 0 , 0.5 ),
  sigma_M ~ dexp( 1 )
 ) , data = d )

A_seq <- seq( from=-2 , to=2 , length.out=30 )

# prep data
sim_dat <- data.frame( A=A_seq )
 
# simulate M and then D, using A_seq
s <- sim( m5.3_A , data=sim_dat , vars=c(“M”,”D”)

And wrote my own code using the shorthand c(bM, bA) ~ dnorm(0, 0.5), however on calling sim I got the following error:
Error in if (left == var) { : the condition has length > 1

Dominant language
R
Stars
2.4k
Forks
632
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rmcelreath/rethinking

All issues in rmcelreath/rethinking

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.