Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using get_refmodel.brmsfit() with Gamma distribution: supplied argument name 'link' does not match 'x' #1255

Closed
kmorndahl opened this issue Oct 29, 2021 · 6 comments
Labels
Milestone

Comments

@kmorndahl
Copy link

Hello,

I am getting an error when building a Gamma model with brm() and attempting to get the reference model from that object using the get_refmodel() instance for brmsfit objects. I believe this might be due to a small bug. Other distribution families seem to work fine.

Here's a test case for this issue:

library(brms)

# Generate fake data
set.seed(5555)
N <- 100
x <- runif(N, -1, 1)
a <- 0.5
b <- 1.1
y_true <- exp(a + b * x)
shape <- 10
y <- rgamma(N, rate = shape / y_true, shape = shape)
fake_data <- data.frame(x=x,y=y)

# Build brms model with Gamma distribution
# Uses stats family designation with capital 'G'
brm_gamma <- brm(y~x, data=fake_data, family=Gamma(link="log"))

# brm function changes Gamma distribution name to gamma with lowercase 'g'
brm_gamma$family
family <- brm_gamma$family
  
# get_refmodel for brms fit fails with: 
# Error in get(family$family, mode = "function")(link = family$link) : 
#   supplied argument name 'link' does not match 'x'
ref <- get_refmodel(brm_gamma)
ref <- brms::get_refmodel(brm_gamma)

# From get_refmodel.brmsfit source code:
# Gamma family is not polytomous so the following always executes as TRUE:
# if (!(isTRUE(dots$aug_data) && is_polytomous(family)))

# This executes the following line of code, which fails with matching error code
family <- get(family$family, mode = "function")(link = family$link)

# The following excerpt from the code above summons the base R 'gamma' function rather than the stats family 'Gamma' function
get(family$family, mode = "function")

I am on Windows 10, RStudio 1.3.1093, R 4.0.2, brms 2.16.1.

Let me know if you need any additional information!

Many thanks for your work with this package.

paul-buerkner added a commit that referenced this issue Nov 3, 2021
@paul-buerkner
Copy link
Owner

Thank you! This should now be fixed on github. Can you double check that it now works as expected?

@paul-buerkner paul-buerkner added this to the brms 2.16.0++ milestone Nov 3, 2021
@kmorndahl
Copy link
Author

Hi Paul,

Thanks very much!

That error code is now gone.

Unfortunately, however, a new error has cropped up:

Error in dis/matrix(mu) : non-conformable arrays

Here's that same test case again:

# Generate fake data
set.seed(5555)
N <- 100
x <- runif(N, -1, 1)
a <- 0.5
b <- 1.1
y_true <- exp(a + b * x)
shape <- 10
y <- rgamma(N, rate = shape / y_true, shape = shape)
fake_data <- data.frame(x=x,y=y)

# Build brms model with Gamma distribution
brm_gamma <- brm(y~x, data=fake_data, family=Gamma(link="log"))

# get_refmodel for brms fit fails with: 
# Error in dis/matrix(mu) : non-conformable arrays
ref <- get_refmodel(brm_gamma)

Perhaps you have insight on what might cause this error? I can of course send more information if necessary

Thanks!

@hsbadr
Copy link
Contributor

hsbadr commented Nov 4, 2021

@paul-buerkner Related to 753addf and this issue, you may also want to map negbinomial to one of negative.binomial (from MASS) or negbin / nb (from mgcv).

@fweber144
Copy link
Contributor

@AlejandroCatalina, is it correct that the Gamma family is still experimental in projpred? In that case, I think projpred should throw a warning about this.

@hsbadr: The negative binomial family is not supported yet by projpred, see, for example, the discussions at stan-dev/projpred#67 and stan-dev/projpred#70.

@AlejandroCatalina
Copy link

AlejandroCatalina commented Nov 4, 2021 via email

@fweber144
Copy link
Contributor

I opened a corresponding issue at stan-dev/projpred#240.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants