-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- man page for dlkjcorr - added rlkjcorr function (Ben's code) - ensemble optionally ignores link or sim - map() now catches another corner case in linear model definitions - map2stan plot() method for traceplots now has window and n_cols arguments - added divergent(), which counts number of divergent iteration in map2stan fit. Automatically warns user. - map2stan better handles missing values in outcome variables. Working towards automatic prediction interface through this method. - some file rearrangement for sanity - precis() checks and warns about divergent iterations - minor updates to README
- Loading branch information
Richard McElreath
committed
Apr 3, 2015
1 parent
1d5b3c4
commit 9704c7d
Showing
15 changed files
with
303 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: rethinking | ||
Type: Package | ||
Title: Statistical Rethinking book package | ||
Version: 1.52 | ||
Date: 2015-03-24 | ||
Version: 1.53 | ||
Date: 2015-04-03 | ||
Author: Richard McElreath | ||
Maintainer: Richard McElreath <[email protected]> | ||
Imports: coda, MASS, mvtnorm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# extracts n_divergent from stan fit | ||
divergent <- function( fit , warmup=FALSE ) { | ||
if ( class(fit)=="map2stan" ) fit <- fit@stanfit | ||
x <- rstan::get_sampler_params(fit) | ||
if ( warmup==FALSE ) { | ||
nwarmup <- fit@stan_args[[1]]$warmup | ||
niter <- fit@stan_args[[1]]$iter | ||
n <- sapply( x , function(ch) sum(ch[(nwarmup+1):niter,5]) ) | ||
} else { | ||
n <- sapply( x , function(ch) sum(ch[,5]) ) | ||
} | ||
sum(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# graphical posterior validation checks for map and map2stan | ||
|
||
postcheck <- function( fit , prob=0.89 , window=20 , n=1000 , col=rangi2 , ... ) { | ||
|
||
undot <- function( astring ) { | ||
astring <- gsub( "." , "_" , astring , fixed=TRUE ) | ||
astring | ||
} | ||
|
||
pred <- link(fit,n=n) | ||
sims <- sim(fit,n=n) | ||
|
||
if ( class(pred)=="list" ) | ||
if ( length(pred)>1 ) pred <- pred[[1]] | ||
|
||
# get outcome variable | ||
lik <- flist_untag(fit@formula)[[1]] | ||
dname <- as.character(lik[[3]][[1]]) | ||
outcome <- as.character(lik[[2]]) | ||
if ( class(fit)=="map2stan" ) outcome <- undot(outcome) | ||
y <- fit@data[[ outcome ]] | ||
|
||
# compute posterior predictions for each case | ||
|
||
mu <- apply( pred , 2 , mean ) | ||
mu.PI <- apply( pred , 2 , PI , prob=prob ) | ||
y.PI <- apply( sims , 2 , PI , prob=prob ) | ||
|
||
# figure out paging | ||
if ( length(window)==1 ) { | ||
window <- min(window,length(mu)) | ||
num_pages <- ceiling( length(mu)/window ) | ||
cases_per_page <- window | ||
} | ||
|
||
# display | ||
ny <- length(fit@data[[ outcome ]]) | ||
ymin <- min(c(as.numeric(y.PI),mu,y)) | ||
ymax <- max(c(as.numeric(y.PI),mu,y)) | ||
|
||
# check for aggregated binomial context | ||
mumax <- max(c(as.numeric(mu.PI))) | ||
if ( ymax > 1 & mumax <= 1 & dname %in% c("dbinom","dbetabinom") ) { | ||
# probably aggregated binomial | ||
size_var <- as.character(lik[[3]][[2]]) | ||
size_var <- fit@data[[ size_var ]] | ||
for ( i in 1:ny ) { | ||
y.PI[,i] <- y.PI[,i]/size_var[i] | ||
y[i] <- y[i]/size_var[i] | ||
} | ||
ymin <- 0 | ||
ymax <- 1 | ||
} | ||
if ( dname=="dordlogit" ) { | ||
# put mu and mu.PI on outcome scale | ||
ymin <- 1 | ||
nlevels <- dim(pred)[3] | ||
mu <- sapply( 1:dim(pred)[2] , | ||
function(i) { | ||
temp <- t(pred[,i,])*1:7 | ||
mean(apply(temp,2,sum)) | ||
} ) | ||
mu.PI <- sapply( 1:dim(pred)[2] , | ||
function(i) { | ||
temp <- t(pred[,i,])*1:7 | ||
PI(apply(temp,2,sum),prob) | ||
} ) | ||
} | ||
|
||
start <- 1 | ||
end <- cases_per_page | ||
|
||
for ( i in 1:num_pages ) { | ||
|
||
end <- min( ny , end ) | ||
window <- start:end | ||
|
||
set_nice_margins() | ||
plot( y[window] , xlab="case" , ylab=outcome , col=col , pch=16 , ylim=c( ymin , ymax ) , xaxt="n" , ... ) | ||
axis( 1 , at=1:length(window) , labels=window ) | ||
|
||
points( 1:length(window) , mu[window] ) | ||
for ( x in 1:length(window) ) { | ||
lines( c(x,x) , mu.PI[,window[x]] ) | ||
points( c(x,x) , y.PI[,window[x]] , cex=0.7 , pch=3 ) | ||
} | ||
mtext( paste("Posterior validation check") ) | ||
|
||
if ( num_pages > 1 ) { | ||
ask_old <- devAskNewPage(ask = TRUE) | ||
on.exit(devAskNewPage(ask = ask_old), add = TRUE) | ||
} | ||
|
||
start <- end + 1 | ||
end <- end + cases_per_page | ||
|
||
} | ||
|
||
# invisible result | ||
result <- list( | ||
mean=mu, | ||
PI=mu.PI, | ||
outPI=y.PI | ||
) | ||
invisible( result ) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.