Skip to content

Unable to convert projected model to matrix in develop #57

@andrewdigby

Description

@andrewdigby

Hi,

I've been using the develop branch of projpred with some success, but have hit a snag trying to plot the projected posteriors for my own logistic regression model, with interactions and random effects. I'm unsure if this is a bug, or something wrong with my model.

The command as.matrix(proj) fails when applying to my own model (fit - see below):

priorh <- set_prior("horseshoe(scale_global = tau0, scale_slab=1)", class="b")
cfull_formula_fixed <-  as.formula( y ~ v1_cts * f1 + v2_cts * f2 + f1:f2 + f3 + v3_cts + v4_cts   )
fit <- brm(cfull_formula_fixed, data=data, family="bernoulli", iter=5000, prior =  priorh,  sample_prior = T, control=list(adapt_delta=0.999,max_treedepth = 15), save_all_pars = F, refresh=0)
#   Data: data (Number of observations: 186) 

vs <- varsel(fit, method='forward') # fails without method='forward' (using method='L1')
proj <- project(vs, nterms = 3, ns = 500)
mcmc_areas(as.matrix(proj)) +  coord_cartesian(xlim = c(-2, 2))

Error:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

I've been unable to diagnose the problem by applying as.matrix.projection directly, since the line:

res <- t(do.call(cbind, lapply(proj$sub_fit, as.matrix.lm)))

returns

Error in t.default(do.call(cbind, lapply(proj$sub_fit, as.matrix.lm))) : 
  argument is not a matrix

since proj$sub_fit is a list.

Apologies for not providing a reproducible example; I can upload the projected (proj) or fitted model (fit) if necessary. The logistic regression example at the bottom of this post (adapted from the vignettes) works as expected, so this must be an issue with my model. I suspect it may be because my model contains multi-level factors and interactions?

Have you seen similar behaviour elsewhere? Any suggestions of how to diagnose this issue would be very much appreciated. Many thanks in advance for your help and the great package.

This works:

library(projpred)
library(brms)
library(tidyr)
library(dplyr)
library(ggplot2)
library(bayesplot)

# Binomial example
data('df_binom', package = 'projpred')
split_structure <- break_up_matrix_term(y ~ x, data = df_binom)
df_binom <- split_structure$data
formula <- split_structure$formula
d <- df_binom
n <- nrow(df_binom) # 100
D <- ncol(df_binom[, -1]) # 20
p0 <- 5 # prior guess for the number of relevant variables
tau0 <- p0/(D-p0) * 1/sqrt(n) # scale for tau 

priorh <- set_prior("horseshoe(scale_global = tau0, scale_slab=1)", class="b")
fit2 <- brm(formula, data=df_binom, family=bernoulli(), iter=500,  chains=2, prior =  priorh,  
                  sample_prior = T, control=list(adapt_delta=0.999,max_treedepth = 15), save_all_pars = TRUE, refresh=0)
vs2 <- cv_varsel(fit2)
plot(vs2, stats = c('elpd', 'rmse'))
proj2 <- project(vs2, nterms = 3, ns = 500)
mcmc_areas(as.matrix(proj2)) +  coord_cartesian(xlim = c(-2, 2))

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions