stan-dev / stan-dev/rstan

Feature Request: Function to strip stanfit of extra parameters

Open
#426 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

I often find myself capturing the entire output of all parameters, transformed parameters, and generated data when running a stan model. However, this object is very large, too large in fact for things like shinystan. It would be nice to have a built in way strip the stanfit object down to the essential variables.

Description:

I wrote the following function, which could be potentially integrated into the RStan package. I would do a pull request, but I'm not too familiar with S4 classes so don't know how to integrate it.

cleanObject <- function(object,pars){
  pars <- c(pars,'lp__')
  nn <- paste0('^',pars,'(\\[|$)',collapse="|")
  ids <-  grep(nn,  object@sim$fnames_oi)
  ids.2 <- which(names(object@par_dims) %in% pars)
  for(i in 1:4){
    a <- attributes(object@sim$samples[[i]])
    x <- object@sim$samples[[i]][ids]
    for(j in c('names','inits','mean_pars'))
      a[[j]] <- a[[j]][ids]
    attributes(x) <- a
    object@sim$samples[[i]] <- x
  }
  object@par_dims <- object@par_dims[ids.2]
  object@sim$dims_oi <-   object@sim$dims_oi[ids.2]  
  object@sim$pars_oi<- object@sim$pars_oi[ids.2]
  object@sim$fnames_oi <-  object@sim$fnames_oi[ids]
  object@sim$n_flatnames <- length(object@sim$fnames_oi)
  object
}
RStan Version:

packageVersion("rstan")
[1] ‘2.15.1’

R Version:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the stanfit S4 class and the cleanObject function described in the issue, including the sim and par_dims slots it modifies. Define the supported parameter-selection behavior and verify that the resulting object retains the requested variables and lp__ while remaining usable; the issue names no implementation file or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.