Giter Club home page Giter Club logo

Comments (8)

sjwild avatar sjwild commented on August 16, 2024

For this one, we need to use a custom family. We can use the Stan's normal_lpdf and normal_rng if we replace sigma with 0.001.

This code should work. It's based off the brms vignette on custom families.

d  <- 
  tibble(species = c("afarensis", "africanus", "habilis", "boisei", "rudolfensis", "ergaster", "sapiens"), 
         brain   = c(438, 452, 612, 521, 752, 871, 1350), 
         mass    = c(37.0, 35.5, 34.5, 41.5, 55.5, 61.0, 53.5)) %>% 
  mutate(mass_std  = (mass - mean(mass)) / sd(mass),
         brain_std = brain / max(brain))

custom_normal <- custom_family(
  "custom_normal", dpars = "mu",
  links = "identity",
  type = "real"
)

stan_funs  <- "real custom_normal_lpdf(real y, real mu) {
  return normal_lpdf(y | mu, 0.001);
}
real custom_normal_rng(real mu) {
  return normal_rng(mu, 0.001);
}
" 

stanvars <- stanvar(scode = stan_funs, block = "functions")

m7.6 <- brm(data = d,
            family = custom_normal,
            formula = brain_std ~ 1 + poly(mass_std, 6),
            prior = c(prior(normal(0.5, 1), class = Intercept),
                      prior(normal(0, 10), class = b)),
            stanvars = stanvars,
            chains = 4,
            cores = 4, 
            iter = 2000,
            warmup = 1000)


expose_functions(m7.6, vectorize = TRUE)


posterior_epred_custom_normal <- function(prep) {
  mu <- prep$dpars$mu
  mu 
}


mass_std <- seq(-1.5, 1.5, length.out = 1000)
fits <- fitted(m7.6, newdata = list(mass_std = mass_std))
fits <- data.frame(fits, 
                   mass_std = mass_std)

ggplot() + geom_line(data = fits, 
                     mapping = aes(x = mass_std, 
                                   y = Estimate),
                     colour = "blue") +
  geom_ribbon(data = fits, mapping = aes(x = mass_std,
                                         ymin = Q2.5,
                                         ymax = Q97.5),
              fill = "blue",
              alpha = 0.5) +
  geom_point(d, mapping = aes(x = mass_std,
                              y = brain_std),
             colour = "blue") +
  theme_minimal()

I hope that helps!

Steve

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

ASKurz avatar ASKurz commented on August 16, 2024

Dude, you're on fire. So I can acknowledge you in the thank you section of the next update, what's your author name?

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

sjwild avatar sjwild commented on August 16, 2024

My name is Stephen Wild. I'm happy to contribute a little. I found your first edition helpful when I started learning bayes and Stan, so I figured a little help is the least I can do. If all goes well, I may (or may not, most likely) succeed with the ODE models.

Steve

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

ASKurz avatar ASKurz commented on August 16, 2024

Right on. If you can get those ODE's, it'd be a major help.

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

ASKurz avatar ASKurz commented on August 16, 2024

The current implementation lives here.

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

sjwild avatar sjwild commented on August 16, 2024

That's awesome! The ODE problem is interesting, because we're actually dealing with two issues: ODEs and a state-space model (at least for lynx-hare). This one's going to require some thinking.

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

ASKurz avatar ASKurz commented on August 16, 2024

I'd also be interested in an example or two of a much simpler ODE, something easier for us novices to grapple with.

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

sjwild avatar sjwild commented on August 16, 2024

I agree. My knowledge of ODEs is limited to 2 things: 1) they exist, and 2) I couldn't solve one if my life depended on it.

from statistical_rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.