rmcelreath/rethinking

Extending manual implementation of link to multivariable models

Closed this issue · 1 comments

The Overthinking section in Chapter 4 entitled "How link works" (p. 107) introduces a manual method for computing the value of a linear model with one predictor variable. I am struggling to extend this method to the posterior predictive check for the multivariable divorce model in Chapter 5, substituting the manual method for the link function in R code box 5.15 (p. 138).

The following part of the setup for the multivariable model flows clearly from R code 4.58 for the single-predictor model on p. 107:

post <- extract.samples(m5.3)
mu.link <- function(M, A) post$a + post$bM*M + post$bA*A

At this point, my main problem is simply conceptual -- I can generate plausible values of A.seq and M.seq (e.g., A.seq <- seq(from = -3, to = 3, length = 50), but I can't quite figure out what to do with them.

I also anticipate that once I figure out what to do with A.seq and M.seq, I may have trouble implementing the sapply function call, so any suggestions about implementation would be much appreciated.

I do think that, beyond resolving my own confusion, providing a manual implementation method for the multivariable model might have pedagogical value, since it would encourage (and enable) students to make sure that they fully understand the conceptual foundations of the link function.

Closing this issue, because I realize that I neglected to understand the importance of the first comment of code box 5.15 ("call link without specifying new data so it uses original data"). I'm providing code for the manual method (using purrr:map2_dfc instead of sapply) on the off chance that anyone else had the same problem and finds this closed issue:

post <- extract.samples(m5.3, n = 1000)
M <- d$M
A <- d$A
mu.link <- function(M, A) post$a + post$bM*M + post$bA*A
library(purrr)
library(dplyr)
mu <- purrr::map2_dfc(M, A, mu.link) %>% set_names(paste0("V",1:50))