Skip to contents

Posterior summary and extraction from CmdStanFit objects

Usage

precis(fit, depth = 1, pars = NULL, lp = F)

extract(fit, pars = NULL, lp = F, chains = NULL)

erect(d, pars, col = "mean")

Arguments

fit

A CmdStanFit object.

depth

Integer. The level of parameters to display. 0 for scalar; 1 for scalar and vector; 2 for scalar, vector, and matrix; and so on.

pars

Character vector. Names of the variables to include. Passed to the variables argument in cmdstanr's $summary()/$draws().

lp

Boolean. Whether to include log probabilities in output.

chains

Integer vector. Chains to extract. By default, NULL, which extracts all chains.

d

data frame. Posterior summary data frame returned from precis().

Value

A data frame

Examples

# Fitted model
fp = system.file("cases", "wine_network", "wine2_normal_first_level2.RDS", package="stom")
m = readRDS(fp)

# Posterior distribution summary
d = precis( m, depth=2, pars="Int,J" )  # or pars=c("Int", "J")
d
#>    variable        mean        sd         q5        q95     rhat  ess_bulk
#> 1  Int[1,1]  1.46065860 0.4155012  0.7738191  2.1368475 1.005829  596.2273
#> 2  Int[2,1]  1.60776153 0.3527793  1.0167770  2.1725515 1.004828  638.9929
#> 3  Int[1,2] -0.29323172 0.4002178 -0.9390186  0.3660294 1.007534  626.0339
#> 4  Int[2,2]  0.00000000 0.0000000  0.0000000  0.0000000       NA        NA
#> 5      J[1] -0.38478502 0.3799942 -0.9994123  0.2432264 1.005196  698.6300
#> 6      J[2]  1.40204140 0.3881811  0.7713947  2.0574410 1.005678  695.9941
#> 7      J[3]  0.17605415 0.3844786 -0.4439989  0.7984256 1.003867  725.7951
#> 8      J[4]  0.34203082 0.3874797 -0.2701835  0.9911882 1.004315  701.5242
#> 9      J[5] -0.25537684 0.3860228 -0.8714625  0.3889210 1.003792  695.2089
#> 10     J[6] -0.40992786 0.3827055 -1.0339180  0.2290665 1.004490  725.9601
#> 11     J[7]  0.46396758 0.3829940 -0.1486301  1.1153265 1.004482  719.1779
#> 12     J[8] -1.51783270 0.2867946 -1.9874985 -1.0400635 1.005495 1012.9021
#> 13     J[9] -0.94599734 0.2874214 -1.4051815 -0.4679022 1.007909 1066.7047
#> 14    J[10]  0.74630456 0.2868925  0.2910840  1.2200405 1.006798  978.7118
#> 15    J[11] -1.10221230 0.2873724 -1.5657620 -0.6261724 1.004553 1087.6786
#> 16    J[12]  1.80909484 0.2842524  1.3477965  2.2772970 1.005867 1017.2190
#> 17    J[13]  0.06987397 0.2781449 -0.3794031  0.5328615 1.006137  983.5072
#> 18    J[14]  0.24525450 0.2882109 -0.2209305  0.7204153 1.008201  998.5836
#>    ess_tail
#> 1  1065.814
#> 2  1114.637
#> 3  1156.876
#> 4        NA
#> 5  1139.062
#> 6  1036.491
#> 7  1349.344
#> 8  1194.978
#> 9  1208.398
#> 10 1255.918
#> 11 1295.044
#> 12 1396.113
#> 13 1753.447
#> 14 1810.974
#> 15 1557.702
#> 16 1730.892
#> 17 1329.687
#> 18 1487.189

# Reconstruct high-dimensional data structure from posterior means
erect( d, "Int" )
#>          [,1]       [,2]
#> [1,] 1.460659 -0.2932317
#> [2,] 1.607762  0.0000000
erect( d, "Int", "sd" )
#>           [,1]      [,2]
#> [1,] 0.4155012 0.4002178
#> [2,] 0.3527793 0.0000000
erect( d, "Int,J" )
#> $Int
#>          [,1]       [,2]
#> [1,] 1.460659 -0.2932317
#> [2,] 1.607762  0.0000000
#> 
#> $J
#>  [1] -0.38478502  1.40204140  0.17605415  0.34203082 -0.25537684 -0.40992786
#>  [7]  0.46396758 -1.51783270 -0.94599734  0.74630456 -1.10221230  1.80909484
#> [13]  0.06987397  0.24525450
#> 

# Extract posterior samples
post = extract(m)
str(post)
#> draws_df [4,000 × 92] (S3: draws_df/draws/tbl_df/tbl/data.frame)
#>  $ Int_raw[1]: num [1:4000] 1.59 1.4 1.85 1.65 1.58 ...
#>  $ Int_raw[2]: num [1:4000] -0.2375 -0.6001 -0.0834 -0.37 -0.2706 ...
#>  $ Int_raw[3]: num [1:4000] 1.83 1.74 1.89 2.05 1.75 ...
#>  $ zJ[1]     : num [1:4000] -0.754 -0.637 -0.46 0.112 -0.142 ...
#>  $ zJ[2]     : num [1:4000] 0.584 0.972 1.64 1.944 1.94 ...
#>  $ zJ[3]     : num [1:4000] -0.1859 0.0664 0.2757 0.491 0.8079 ...
#>  $ zJ[4]     : num [1:4000] 0.0275 0.2999 0.6461 0.5709 0.8344 ...
#>  $ zJ[5]     : num [1:4000] -0.416 -0.455 -0.332 -0.104 -0.49 ...
#>  $ zJ[6]     : num [1:4000] -0.7102 -0.4128 -0.2841 -0.4882 -0.0785 ...
#>  $ zJ[7]     : num [1:4000] 0.0724 0.3601 1.0988 1.3 0.871 ...
#>  $ zJ[8]     : num [1:4000] -1.62 -1.78 -1.8 -1.55 -1.38 ...
#>  $ zJ[9]     : num [1:4000] -1.329 -1.167 -0.819 -1.125 -1.291 ...
#>  $ zJ[10]    : num [1:4000] 0.237 0.323 1.166 0.786 0.43 ...
#>  $ zJ[11]    : num [1:4000] -1.13 -1.55 -1.55 -1.27 -1.3 ...
#>  $ zJ[12]    : num [1:4000] 1 1.39 2.54 2.67 2.8 ...
#>  $ zJ[13]    : num [1:4000] -0.25 0.0125 0.0777 0.066 0.3985 ...
#>  $ zJ[14]    : num [1:4000] -0.226 -0.21 0.634 0.621 0.455 ...
#>  $ zW[1]     : num [1:4000] -0.0158 -0.1981 -0.8422 -0.6991 -0.8114 ...
#>  $ zW[2]     : num [1:4000] -0.0842 0.4905 -0.3685 -0.663 -0.4034 ...
#>  $ zW[3]     : num [1:4000] -1.25 -0.566 -1.707 -1.705 -1.793 ...
#>  $ zW[4]     : num [1:4000] 1.531 1.186 0.578 0.292 0.592 ...
#>  $ zW[5]     : num [1:4000] 0.315 -0.143 -0.885 -0.956 -1.06 ...
#>  $ zW[6]     : num [1:4000] 1.04567 0.67864 0.00414 -0.22782 0.07864 ...
#>  $ zW[7]     : num [1:4000] 0.8674 0.777 -0.0235 0.146 0.233 ...
#>  $ zW[8]     : num [1:4000] 0.89 0.589 -0.226 -0.468 -0.247 ...
#>  $ zW[9]     : num [1:4000] 1.332 1.1 0.478 0.552 0.387 ...
#>  $ zW[10]    : num [1:4000] 0.297 0.385 -0.359 -0.206 -0.162 ...
#>  $ zW[11]    : num [1:4000] -0.328 -0.204 -0.569 -1.11 -0.847 ...
#>  $ zW[12]    : num [1:4000] 0.94 1.198 0.68 0.861 1.008 ...
#>  $ zW[13]    : num [1:4000] 0.8707 0.7284 0.0419 0.1579 0.5282 ...
#>  $ zW[14]    : num [1:4000] -0.752 -0.951 -1.812 -2.244 -1.951 ...
#>  $ zW[15]    : num [1:4000] -0.00293 -0.26146 -1.0109 -1.29183 -1.16576 ...
#>  $ zW[16]    : num [1:4000] -1.77 -1.06 -2.53 -2.45 -2.13 ...
#>  $ zW[17]    : num [1:4000] 1.991 2.027 1.453 1.367 0.845 ...
#>  $ zW[18]    : num [1:4000] 2.03 1.75 1.6 1.71 1.81 ...
#>  $ zW[19]    : num [1:4000] 1.67 1.83 1.41 1.48 1.34 ...
#>  $ zW[20]    : num [1:4000] 0.0272 0.3317 -0.3306 -0.2045 -0.3151 ...
#>  $ zW[21]    : num [1:4000] 0.3112 0.0807 -0.662 -0.5965 -0.4068 ...
#>  $ zW[22]    : num [1:4000] 1.3 1.325 0.347 0.343 0.292 ...
#>  $ zW[23]    : num [1:4000] -0.664 -1.147 -1.508 -1.979 -1.867 ...
#>  $ zW[24]    : num [1:4000] -1.04 -1.52 -2.15 -2.21 -1.73 ...
#>  $ zW[25]    : num [1:4000] -0.293 -0.249 -1.054 -0.784 -0.626 ...
#>  $ zW[26]    : num [1:4000] 0.318 0.414 -0.243 -0.336 -0.391 ...
#>  $ sigma_J   : num [1:4000] 1.247 1.131 0.775 0.769 0.785 ...
#>  $ sigma_W   : num [1:4000] 0.999 0.944 0.871 0.806 0.724 ...
#>  $ Int[1,1]  : num [1:4000] 1.59 1.4 1.85 1.65 1.58 ...
#>  $ Int[2,1]  : num [1:4000] 1.83 1.74 1.89 2.05 1.75 ...
#>  $ Int[1,2]  : num [1:4000] -0.2375 -0.6001 -0.0834 -0.37 -0.2706 ...
#>  $ Int[2,2]  : num [1:4000] 0 0 0 0 0 0 0 0 0 0 ...
#>  $ J[1]      : num [1:4000] -0.9396 -0.7205 -0.3567 0.0858 -0.1114 ...
#>  $ J[2]      : num [1:4000] 0.729 1.099 1.271 1.494 1.522 ...
#>  $ J[3]      : num [1:4000] -0.2317 0.0751 0.2137 0.3774 0.6341 ...
#>  $ J[4]      : num [1:4000] 0.0343 0.3392 0.5008 0.4388 0.6549 ...
#>  $ J[5]      : num [1:4000] -0.518 -0.514 -0.258 -0.08 -0.385 ...
#>  $ J[6]      : num [1:4000] -0.8853 -0.4669 -0.2202 -0.3753 -0.0616 ...
#>  $ J[7]      : num [1:4000] 0.0902 0.4072 0.8518 0.9992 0.6837 ...
#>  $ J[8]      : num [1:4000] -2.02 -2.02 -1.4 -1.19 -1.08 ...
#>  $ J[9]      : num [1:4000] -1.657 -1.32 -0.635 -0.864 -1.013 ...
#>  $ J[10]     : num [1:4000] 0.295 0.365 0.904 0.604 0.338 ...
#>  $ J[11]     : num [1:4000] -1.403 -1.748 -1.201 -0.978 -1.018 ...
#>  $ J[12]     : num [1:4000] 1.25 1.57 1.97 2.06 2.2 ...
#>  $ J[13]     : num [1:4000] -0.3117 0.0141 0.0603 0.0507 0.3128 ...
#>  $ J[14]     : num [1:4000] -0.281 -0.238 0.491 0.478 0.357 ...
#>  $ W[1]      : num [1:4000] -0.0158 -0.187 -0.734 -0.5636 -0.5874 ...
#>  $ W[2]      : num [1:4000] -0.0841 0.463 -0.3212 -0.5345 -0.292 ...
#>  $ W[3]      : num [1:4000] -1.248 -0.534 -1.488 -1.374 -1.298 ...
#>  $ W[4]      : num [1:4000] 1.529 1.119 0.504 0.235 0.429 ...
#>  $ W[5]      : num [1:4000] 0.314 -0.135 -0.771 -0.771 -0.768 ...
#>  $ W[6]      : num [1:4000] 1.04423 0.64068 0.00361 -0.18366 0.05693 ...
#>  $ W[7]      : num [1:4000] 0.8662 0.7335 -0.0205 0.1177 0.1687 ...
#>  $ W[8]      : num [1:4000] 0.889 0.556 -0.197 -0.377 -0.179 ...
#>  $ W[9]      : num [1:4000] 1.33 1.038 0.416 0.445 0.28 ...
#>  $ W[10]     : num [1:4000] 0.297 0.364 -0.313 -0.166 -0.117 ...
#>  $ W[11]     : num [1:4000] -0.327 -0.192 -0.496 -0.895 -0.613 ...
#>  $ W[12]     : num [1:4000] 0.939 1.131 0.593 0.694 0.73 ...
#>  $ W[13]     : num [1:4000] 0.8695 0.6877 0.0365 0.1273 0.3824 ...
#>  $ W[14]     : num [1:4000] -0.751 -0.898 -1.579 -1.809 -1.412 ...
#>  $ W[15]     : num [1:4000] -0.00292 -0.24683 -0.88096 -1.04147 -0.84397 ...
#>  $ W[16]     : num [1:4000] -1.77 -1 -2.21 -1.97 -1.54 ...
#>  $ W[17]     : num [1:4000] 1.988 1.913 1.266 1.102 0.612 ...
#>  $ W[18]     : num [1:4000] 2.03 1.65 1.4 1.38 1.31 ...
#>  $ W[19]     : num [1:4000] 1.664 1.73 1.233 1.194 0.969 ...
#>  $ W[20]     : num [1:4000] 0.0272 0.3132 -0.2881 -0.1649 -0.2281 ...
#>  $ W[21]     : num [1:4000] 0.3107 0.0762 -0.5769 -0.4809 -0.2945 ...
#>  $ W[22]     : num [1:4000] 1.298 1.25 0.303 0.276 0.211 ...
#>  $ W[23]     : num [1:4000] -0.664 -1.083 -1.314 -1.595 -1.352 ...
#>  $ W[24]     : num [1:4000] -1.04 -1.43 -1.87 -1.78 -1.25 ...
#>  $ W[25]     : num [1:4000] -0.293 -0.235 -0.918 -0.632 -0.453 ...
#>  $ W[26]     : num [1:4000] 0.318 0.391 -0.212 -0.271 -0.283 ...
#>  $ .chain    : int [1:4000] 1 1 1 1 1 1 1 1 1 1 ...
#>  $ .iteration: int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...
#>  $ .draw     : int [1:4000] 1 2 3 4 5 6 7 8 9 10 ...