How to extract output

I used the command below to extract my output which has 5088 estimates I need, but I got 5089 columns, any ideas on how the estimated parameters are arranged in the extracted output? also, I have 3000 rows which are the number of iterations after burn-in, so I need to calculate the means to get the parameter estimates, right? but if I only print the stan, I got the means, SEs, number of effective draws, and R hat, how can I extract this information? Thanks!

output <- extract(fit, permuted = TRUE) 

two packages of interest: posterior and shredder

The posterior R package is intended to provide useful tools for both users and developers of packages for fitting Bayesian models or working with output from Bayesian models. The primary goals of the package are to:

  • Efficiently convert between many different useful formats of draws (samples) from posterior or prior distributions.
  • Provide consistent methods for operations commonly performed on draws, for example, subsetting, binding, or mutating draws.
  • Provide various summaries of draws in convenient formats.
  • Provide lightweight implementations of state of the art posterior inference diagnostics.

haven’t used shredder myself, but so love the logo: GitHub - yonicd/shredder: API for exploring and iterating rstan fit objects

The goal of shredder is to create an API that incites exploration and iteration of rstan simulation objects.

  • With this API users can:
    • Manipulate fit objects without needing to convert arrays into rectangular form.
    • Generate task specifc subsets of the parent fit object for fit diagnostics and post-processing
    • Use pipe operators to create more user-friendly workflows
    • Manipulate various classes that contain a stanfit object, such as rstan and brms .
  • RStudio specific feature: Tab autocomplete in shredder::stan_select and shredder::stan_filter for the parameter names stored in the fit object.

Thanks! it looks to me that posterior is easier to use, so I was trying to use that, but I encountered the following warning when I installed it. And when I library it, it says there is no package called posterior. Any ideas on how to fix it?

The downloaded source packages are in
	‘/private/var/folders/5x/rxc8hsw96rvblp_118zbchwh0000gn/T/RtmpkYQHRP/downloaded_packages’
dyld: lazy symbol binding failed: Symbol not found: _utimensat
  Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libR.dylib (which was built for Mac OS X 10.13)
  Expected in: /usr/lib/libSystem.B.dylib

dyld: Symbol not found: _utimensat
  Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libR.dylib (which was built for Mac OS X 10.13)
  Expected in: /usr/lib/libSystem.B.dylib

/Library/Frameworks/R.framework/Resources/bin/INSTALL: line 34: 46534 Done                    echo 'tools:::.install_packages()'
     46535 Abort trap: 6           | R_DEFAULT_PACKAGES= LC_COLLATE=C "${R_HOME}/bin/R" $myArgs --no-echo --args ${args}

Installation instructions on the README.md:

Installation

The package is not on CRAN yet, but you can install the beta release via

install.packages(“posterior”, repos = c(“Repository for distributing (some) stan-dev R packages | r-packages”, getOption(“repos”)))

or the latest development version from GitHub via

install.packages(“remotes”) remotes::install_github(“stan-dev/posterior”)

yes I followed the instruction and tried both ways and i got the warning above

@jonah - install problems with posterior? instructions in README don’t work - per 3rd message in this thread.

Weird I don’t think any of this is specifically related to the posterior package. My hunch is this would happen installing other R packages (from source) too. To check you could try installing other R packages from GitHub (from any developer) or installing a package from CRAN specifying type="source" when calling install.packages(). I would guess you’d see the same problem.

From those error messages it looks like you have R 4.0 but perhaps Mac OS X less than 10.13? Is that right? On https://cran.r-project.org/ is says that the latest R is for for macOS 10.13 (High Sierra) and higher. So you might be experiencing problems installing packages from source for that reason.

I think the extra column is lp__, which is the total log probability computed in the model block (log prior + log posterior, up to an additive constant).

Probably in the order they’re declared in the Stan program, but they should all have names in the output so you can double check.

Thank you! I changed my R back to older version and installed posterior, but encountered the following error when loaded it:

Error: package or namespace load failed for ‘posterior’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 namespace ‘rlang’ 0.4.6 is already loaded, but >= 0.4.7 is required

Hmm, looks like it’s asking you to upgrade to a later version of the ‘rlang’ package.

You can also just use the summary function from the rstan package:

my_summary <- summary(fit)
estimates <- my_summary$summary
chain_estimates <- my_summary$c_summary

Now estimates contains your parameter estimates and chain_estimates contains them but separated by each Markov chain. If you’re not having any convergence problems then you can just ignore chain_estimates.

ok thanks! I think this is enough

1 Like