stan-dev / stan-dev/projpred

variable selection for uncorrelated random variables

Open
#435 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement exceptions
Dominant language
R
Stars
114
Forks
31
PR merge metrics
No merged PRs in 30d

Description

Hi

I want to fit a Poisson model with uncorrelated random variables and perform projection prediction.
Is it true that this is not yet possible with projpred or can I specify my model in a different way such that I can use uncorrelated random variables?
I also wonder if it matters for projection prediction whether they are specified as correlated or not.

Unexpected number of | characters in group terms. Please contact the package maintainer.

library(brms)
#> Loading required package: Rcpp
#> Warning: package 'Rcpp' was built under R version 4.3.1
#> Loading 'brms' package (version 2.19.0). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: 'brms'
#> The following object is masked from 'package:stats':
#> 
#>     ar
library(projpred)
#> Warning: package 'projpred' was built under R version 4.3.1
#> This is projpred version 2.6.0.
#> 
#> Attaching package: 'projpred'
#> The following object is masked from 'package:brms':
#> 
#>     do_call

# simulate data
species <- paste("species", 1:5, sep = "_")
periods <- paste("period", 1:2, sep = "_")
df <- expand.grid(species, periods)

set.seed(123)
lambdas <- c(rgamma(length(species), 0.7, 0.2),
             rgamma(length(species), 0.5, 0.25))

data_list <- lapply(seq_along(df[, 1]), function(i) {
  out <- tidyr::expand_grid(df[i, ], rpois(30, lambdas[i]))
  names(out) <- c("species", "period", "count")
  return(out)
  })
dat_df <- do.call(rbind.data.frame, data_list)

# MCMC parameters
nchains <- 3 # number of chains
niter <- 4000 # number of iterations (incl. burn-in)
burnin <- niter / 4 # number of initial samples to discard (burn-in)
nparallel <- nchains # number of cores used for parallel computing

## Uncorrelated random random variables
# Fit model
fit1 <- brm(bf(count ~ period + (1 + period || species)),
            data = dat_df,
            family = poisson(),
            chains = nchains, warmup = burnin, iter = niter, cores = nparallel,
            control = list(adapt_delta = 0.99,
                           max_treedepth = 12))
#> Compiling Stan program...
#> Start sampling

# Projection prediction
cvvs_fast1 <- cv_varsel(fit1,
                        method = "forward",
                        nclusters_pred = 20, # speed
                        cv_method = "LOO",
                        # only for the sake of speed
                        validate_search = FALSE)
#> Error in FUN(X[[i]], ...): Unexpected number of `|` characters in group terms. Please contact the package maintainer.

Created on 2023-07-24 with reprex v2.0.2

Kind regards,
Ward

Contributor guide

Open the contributing guide

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

Reproduce the issue with the brms Poisson model using (1 + period || species) and the cv_varsel(fit1, method = "forward") entry point. Trace the group-term parsing that emits the unexpected | error, then establish whether uncorrelated random variables should be supported or documented as unsupported, with a regression test for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.