Skip to content

FIX: update.brmsfit returns error when updating an empty fit#1835

Open
trinhdhk wants to merge 8 commits intopaul-buerkner:masterfrom
trinhdhk:FIX-update-method
Open

FIX: update.brmsfit returns error when updating an empty fit#1835
trinhdhk wants to merge 8 commits intopaul-buerkner:masterfrom
trinhdhk:FIX-update-method

Conversation

@trinhdhk
Copy link
Copy Markdown

@trinhdhk trinhdhk commented Nov 13, 2025

An empty fit is useful when users make post-hoc edit on Stancode but still want to leverage brmsmethods like standata.
Currently update.brmsfit throws S4 error when trying to access object$fit@sim. I think this is R's design inconsistency when $ is a safe call but @ isn't.

This pull request fixes this issue by implementing a check on object$fit.

empty_fit <- !isS4(object$fit)

Test:

m0 <- brm(mpg~cyl, data=mtcars, empty=TRUE)
mtcars2 <- dplyr::mutate(mtcars, cyl=1)
m1 <- update(m0, newdata=mtcars2, empty=TRUE)
m1; head(m1$data)

Expected output:

  • m1 should be printed properly as an empty fit
  • m1$data$cyl should be 1
 Family: gaussian 
  Links: mu = identity 
Formula: mpg ~ cyl 
   Data: mtcars2 (Number of observations: 32) 

The model does not contain posterior draws.
                   mpg cyl
Mazda RX4         21.0   1
Mazda RX4 Wag     21.0   1
Datsun 710        22.8   1
Hornet 4 Drive    21.4   1
Hornet Sportabout 18.7   1
Valiant           18.1   1

Test for recomplication:

m1 <- update(m0, newdata=mtcars2)

Expected output:

  • The message "The desired updates require recompiling the model" should not appear.
Compiling Stan program...

FIXME:

I also put some safeguard in update.brmfit_mutiple but I did not use this function extensively enough to assure that is sufficient. From the surface I think it would err out when calling brm_multiple(empty=TRUE):

Compiling the C++ model
Fitting imputed model 1
Warning: Caught simpleError. Canceling all iterations ...
Error:
! unable to find an inherited method for function ‘get_stanmodel’ for signature ‘object = "NULL"’

I think it is not even allowed. Given the ancient cat "Compiling the C++ model" I believe it has been long since this function is updated.

@paul-buerkner
Copy link
Copy Markdown
Owner

Thanks! I will review this PR soon.

@paul-buerkner paul-buerkner added this to the brms 2.24.0 milestone Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants