0

I am reading Statistical Rethinking (2nd edition) from Richard McElreath and in the 4th chapter there is example of B-Spline, which is solved by quap (quadratic approximation) and I would like to solve it by ulam (HMC).

data("cherry_blossoms")
d = cherry_blossoms
d2 = d[ complete.cases(d$temp), ]
num_knots = 15
knot_list = quantile(d2$year, probs = seq(0,1,length.out = num_knots))
library(splines)
B = bs(d2$year,knots = knot_list[-c(1,num_knots)],degree = 3, intercept = TRUE) #matrix 1124 x 17
m4.7 = quap(
  alist(
    T ~ dnorm(mu, sigma),
    mu <- a + B %*% w,
    a ~ dnorm(6,10),
    w ~ dnorm(0,1),
    sigma ~ dexp(1)
  ), data = list(T= d2$temp,B=B),start = list(w = rep(0,ncol(B)))
)

But it seems that I can't multiply matrix of predictors (B) by parameter vector (w) in ulam interface. Can someone advise me how to do this in ulam interface? (mu <- a + B %*% w)

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • I'm not sure whether ulam can handle spline models, but if you're willing to build the model directly in stan, here is a tutorial [on spline models in stan](https://mc-stan.org/users/documentation/case-studies/splines_in_stan.html). I think you can also fit spiine models in stan using the `rstanarm` and `brms` packages. – eipi10 Jun 30 '20 at 18:37
  • If you don't find a solution here, you could post your question as an issue at the [github development page for the `rethinking` package](https://github.com/rmcelreath/rethinking/issues) – eipi10 Jun 30 '20 at 18:51

0 Answers0