Learn R Programming

rethinking (version 2.13)

link-methods: Predictions for map and map2stan models

Description

Computes inverse-link linear model values for map and map2stan samples.

Usage

link( fit , data , n=1000 , ... )
# S4 method for map2stan
link( fit , data , n=1000 , post , refresh=0.1 , 
    replace=list() , flatten=TRUE , ... )

Arguments

fit

Object of class map or map2stan

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, link extracts the samples using extract.samples.

refresh

Refresh interval for progress display. Set to refresh=0 to suppress display.

replace

Optional named list of samples to replace inside posterior samples. See examples.

flatten

When TRUE, removes linear model names from result

...

Other parameters to pass to someone

Value

Details

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.

It is easy to substitute zero values for any varying effect parameters in a model. In the data passed to link, either omit the relevant index variable or set the index variable to value zero. This will cause link to replace with zero all samples for any parameters corresponding the index variable in a prior. For example, the prior declaration aid[id] ~ dmvnorm2(0,sigma,Rho) defines a vector of parameters aid that are indexed by the variable id. If id is absent from data when calling link, or set to value zero, then link will replace all samples for aid with value zero. This effectively removes the varying effect from posterior predictions. If the prior were instead c(aid,bid)[id] ~ dmvnorm(0,sigma,Rho), then both aid and bid would be set to zero in all samples.

See Also

map, map2stan, sim, ensemble, postcheck

Examples

Run this code
# 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 )
# }

Run the code above in your browser using DataLab