Description Usage Arguments Details Author(s) See Also Examples
Computes inverse-link linear model values for map
and map2stan
samples.
1 2 3 4 |
fit |
Object of class |
data |
Optional list of data to compute predictions over. When missing, uses data found inside fit object. |
n |
Number of samples to use |
post |
Optional samples from posterior. When missing, |
refresh |
Refresh interval for progress display. Set to |
replace |
Optional named list of samples to replace inside posterior samples. See examples. |
flatten |
When |
... |
Other parameters to pass to someone |
This function computes the value of each linear model at each sample for each case in the data. Inverse link functions are applied, so that for example a logit link linear model produces probabilities, using the logistic transform.
This function is used internally by WAIC
, sim
, postcheck
, and ensemble
.
It is possible to replace components of the posterior distribution with simulated values. The replace
argument should be a named list
with replacement values. This is useful for marginalizing over varying effects. See the examples below for an example in which varying intercepts are marginalized this way.
Richard McElreath
map
, map2stan
, sim
, ensemble
, postcheck
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | ## Not run:
library(rethinking)
data(chimpanzees)
d <- chimpanzees
d$recipient <- NULL # get rid of NAs
# model 4 from chapter 12 of the book
m12.4 <- map2stan(
alist(
pulled_left ~ dbinom( 1 , p ) ,
logit(p) <- a + a_actor[actor] + (bp + bpC*condition)*prosoc_left ,
a_actor[actor] ~ dnorm( 0 , sigma_actor ),
a ~ dnorm(0,10),
bp ~ dnorm(0,10),
bpC ~ dnorm(0,10),
sigma_actor ~ dcauchy(0,1)
) ,
data=d , warmup=1000 , iter=4000 , chains=4 )
# posterior predictions for a particular actor
chimp <- 2
d.pred <- list(
prosoc_left = c(0,1,0,1), # right/left/right/left
condition = c(0,0,1,1), # control/control/partner/partner
actor = rep(chimp,4)
)
link.m12.4 <- link( m12.4 , data=d.pred )
apply( link.m12.4 , 2 , mean )
apply( link.m12.4 , 2 , PI )
# posterior predictions marginal of actor
# here we replace the varying intercepts samples
# with simulated values
# replace varying intercept samples with simulations
post <- extract.samples(m12.4)
a_actor_sims <- rnorm(7000,0,post$sigma_actor)
a_actor_sims <- matrix(a_actor_sims,1000,7)
# fire up link
# note use of replace list
link.m12.4 <- link( m12.4 , n=1000 , data=d.pred ,
replace=list(a_actor=a_actor_sims) )
# summarize
apply( link.m12.4 , 2 , mean )
apply( link.m12.4 , 2 , PI )
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.