-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.48 - crawling towards completeness
- importing classes from rstan - various man page updates - fixed rare ensemble bug that occurred when some models has zero weight - fix (again) for map models with single-parameter linear models like mu <- a - fix for duplicate index variable in unusual map2stan models - map2stan better with matrix data inputs - added WAIC, sim, and link methods for lm objects. These are still experimental. link in particular known to behave oddly at times. Still not sure why. So use with caution, if at all.
- Loading branch information
Richard McElreath
committed
Feb 15, 2015
1 parent
0481862
commit c7a624e
Showing
17 changed files
with
408 additions
and
81 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.47 | ||
Date: 2014-12-23 | ||
Version: 1.48 | ||
Date: 2015-02-15 | ||
Author: Richard McElreath | ||
Maintainer: Richard McElreath <[email protected]> | ||
Imports: coda, MASS | ||
|
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,4 +1,5 @@ | ||
exportPattern("^[^x]") | ||
importFrom(coda, HPDinterval) | ||
importFrom(coda, as.mcmc) | ||
importFrom(MASS, mvrnorm) | ||
importFrom(MASS, mvrnorm) | ||
importClassesFrom(rstan) |
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,63 @@ | ||
# build ensemble of samples using DIC/WAIC weights | ||
ensemble <- function( ... , data , n=1e3 , WAIC=TRUE , refresh=0 ) { | ||
# retrieve list of models | ||
L <- list(...) | ||
if ( is.list(L[[1]]) && length(L)==1 ) | ||
L <- L[[1]] | ||
# retrieve model names from function call | ||
mnames <- match.call() | ||
mnames <- as.character(mnames)[2:(length(L)+1)] | ||
if ( length(L)>1 ) { | ||
ictab <- compare( ... , WAIC=WAIC , refresh=refresh , n=n , sort=FALSE ) | ||
rownames(ictab@output) <- mnames | ||
weights <- ictab@output$weight | ||
} else { | ||
ictab <- NA | ||
weights <- 1 | ||
} | ||
|
||
# compute number of predictions per model | ||
idx <- round( weights * n ) | ||
|
||
# generate simulated predictions for each model | ||
# then compose ensemble using weights | ||
if ( missing(data) ) { | ||
link.list <- lapply( L , function(m) link(m , n=n , refresh=refresh ) ) | ||
sim.list <- lapply( L , function(m) sim(m , n=n , refresh=refresh ) ) | ||
} else { | ||
link.list <- lapply( L , function(m) link(m , data=data , n=n , refresh=refresh ) ) | ||
sim.list <- lapply( L , function(m) sim(m , data=data , n=n , refresh=refresh ) ) | ||
} | ||
#print(str(sim.list)) | ||
|
||
# compose weighted predictions | ||
# calculate indices corresponding to each model | ||
idx_start <- rep(1,length(idx)) | ||
idx_end <- idx | ||
if ( length(L)>1 ) | ||
for ( i in 2:length(idx) ) { | ||
idx_start[i] <- min( idx_start[i-1] + idx[i-1] , n ) | ||
idx_end[i] <- min( idx_end[i-1] + idx[i] , n ) | ||
} | ||
#print(idx) | ||
#print(idx_start) | ||
#print(idx_end) | ||
link_out <- link.list[[1]] | ||
sim_out <- sim.list[[1]] | ||
for ( i in 1:length(idx) ) { | ||
if ( idx[i]>0 ) { | ||
idxrange <- idx_start[i]:idx_end[i] | ||
link_out[idxrange,] <- link.list[[i]][idxrange,] | ||
sim_out[idxrange,] <- sim.list[[i]][idxrange,] | ||
} | ||
} | ||
result <- list( link=link_out , sim=sim_out ) | ||
names(weights) <- mnames | ||
idxtab <- cbind( idx_start , idx_end ) | ||
rownames(idxtab) <- mnames | ||
attr(result,"weights") <- weights | ||
attr(result,"indices") <- idxtab | ||
attr(result,"ictab") <- ictab | ||
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
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
Oops, something went wrong.