Combines the chain and iteration columns of a tidy data frame of draws from a Bayesian model fit into a new column that can uniquely identify each draw. Generally speaking not needed for pure tidybayes code, as tidybayes functions now automatically include a .draw column, but can be useful when interacting with packages that do not provide such a column.

combine_chains(data, chain = .chain, iteration = .iteration, into = ".draw")

Arguments

data

Tidy data frame of draws with columns representing the chain and iteration of each draw.

chain

Bare name of column in data indicating the chain of each row. The default (.chain) is the same as used by other functions in tidybayes.

iteration

Bare name of column in data indicating the iteration of each row. The default (.iteration) is the same as used by other functions in tidybayes.

into

Name (as a character vector) of the column to combine chains into. The default, NULL, replaces the chain column with NAs and writes the combined chain iteration numbers into iteration. If provided, chain and iteration will not be modified, and the combined iteration number will be written into a new column named into.

Value

A data frame of tidy draws with a combined iteration column

Author

Matthew Kay

Examples


library(magrittr)
library(coda)

data(line, package = "coda")

# The `line` posterior has two chains with 200 iterations each:
line %>%
  tidy_draws() %>%
  summary()
#>      .chain      .iteration         .draw           alpha      
#>  Min.   :1.0   Min.   :  1.00   Min.   :  1.0   Min.   :0.858  
#>  1st Qu.:1.0   1st Qu.: 50.75   1st Qu.:100.8   1st Qu.:2.732  
#>  Median :1.5   Median :100.50   Median :200.5   Median :3.019  
#>  Mean   :1.5   Mean   :100.50   Mean   :200.5   Mean   :2.988  
#>  3rd Qu.:2.0   3rd Qu.:150.25   3rd Qu.:300.2   3rd Qu.:3.242  
#>  Max.   :2.0   Max.   :200.00   Max.   :400.0   Max.   :7.173  
#>       beta             sigma        
#>  Min.   :-1.5662   Min.   : 0.3262  
#>  1st Qu.: 0.6039   1st Qu.: 0.6158  
#>  Median : 0.7963   Median : 0.7912  
#>  Mean   : 0.7992   Mean   : 0.9681  
#>  3rd Qu.: 0.9925   3rd Qu.: 1.0752  
#>  Max.   : 1.8320   Max.   :11.2331  

# combine_chains combines the chain and iteration column into the .draw column.
line %>%
  tidy_draws() %>%
  combine_chains() %>%
  summary()
#>      .chain      .iteration         .draw           alpha      
#>  Min.   :1.0   Min.   :  1.00   Min.   :  1.0   Min.   :0.858  
#>  1st Qu.:1.0   1st Qu.: 50.75   1st Qu.:100.8   1st Qu.:2.732  
#>  Median :1.5   Median :100.50   Median :200.5   Median :3.019  
#>  Mean   :1.5   Mean   :100.50   Mean   :200.5   Mean   :2.988  
#>  3rd Qu.:2.0   3rd Qu.:150.25   3rd Qu.:300.2   3rd Qu.:3.242  
#>  Max.   :2.0   Max.   :200.00   Max.   :400.0   Max.   :7.173  
#>       beta             sigma        
#>  Min.   :-1.5662   Min.   : 0.3262  
#>  1st Qu.: 0.6039   1st Qu.: 0.6158  
#>  Median : 0.7963   Median : 0.7912  
#>  Mean   : 0.7992   Mean   : 0.9681  
#>  3rd Qu.: 0.9925   3rd Qu.: 1.0752  
#>  Max.   : 1.8320   Max.   :11.2331