rmcelreath / rethinking

Statistical Rethinking course and book package
2.14k stars 603 forks source link

WAIC not computing from ulam() model, even with log_lik = TRUE #428

Open szmoe opened 6 months ago

szmoe commented 6 months ago

Hello, I've been trying to solve the practice problem from Monte Carlo Chapter, and I can't compute WAIC from ulam model.

N <- 100
height <- rnorm(N,10,2)
leg_prop <- runif(N,0.4,0.5)
leg_left <- leg_prop*height +
    rnorm( N , 0 , 0.02 )
leg_right <- leg_prop*height +
    rnorm( N , 0 , 0.02 )
d <- data.frame(height,leg_left,leg_right)

output <- capture.output(m5.8s2 <- ulam(
  alist(
    height ~ dnorm( mu , sigma ) ,
    mu <- a + bl*leg_left + br*leg_right ,
    a ~ dnorm( 10 , 100 ) ,
    bl ~ dnorm( 2 , 10 ) ,
    br ~ half_normal(1, 10),
    sigma ~ dcauchy( 0 , 1 )
  ),
  data=d, chains=4, start=list(a=10,bl=0,br=1,sigma=1), log_lik = TRUE))

print(WAIC(m5.8s2))

When I run the above code, I got error message:

Error in UseMethod("WAIC") : no applicable method for 'WAIC' applied to an object of class "ulam" . How do I solve this, please?

rmcelreath commented 6 months ago

I don't know what's wrong on your end, but this works fine for me:

m <- ulam(
  alist(
    height ~ dnorm( mu , sigma ) ,
    mu <- a + bl*leg_left + br*leg_right ,
    a ~ dnorm( 10 , 100 ) ,
    bl ~ dnorm( 2 , 10 ) ,
    br ~ half_normal(1, 10),
    sigma ~ dcauchy( 0 , 1 )
  ),
  data=d, chains=4, cores=4 , log_lik = TRUE)
szmoe commented 6 months ago

Thank you for the reply. I can run the model and get the precis output, but other than that, I can't do a thing- can't compute waic or do traceplots, etc. So, I have been using brms for the chapter at the moment.

Not sure if it is related but I did this https://discourse.mc-stan.org/t/problem-with-cmdstan-unpacking/29041/5 to get ulam() to work on my Rstudio. Other than that, I followed the book. But, I am using 1st edition of the book, so maybe I miss some new snippets here and there for it to work. Thank you very much.

rmcelreath commented 6 months ago

Since map2stan is no longer supported, and the code in the 1st edition is for map2stan, you are going to have some problems. You can't just replace map2stan with ulam unfortunately.

szmoe commented 6 months ago

Got it! I will do the exercises with brms at the moment and will try this again when I can get the new edition. Thank you very much :)