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

piping weight_draws / resample_draws #310

Open
avehtari opened this issue Nov 9, 2023 · 6 comments
Open

piping weight_draws / resample_draws #310

avehtari opened this issue Nov 9, 2023 · 6 comments
Labels
feature New feature or request

Comments

@avehtari
Copy link
Collaborator

avehtari commented Nov 9, 2023

Currently weight_draws(x, weights, ...) requires weights to be a numeric vector. Would it be possible to have a piping version so that when the draws object already has a variable that contains the log weights e.g. named lw, we could pipe

x <- x |> mutate_varibles(lw = lp__-lp_approx__) |> weight_draws(weights=lw, log=TRUE)

and even better if we can pipe as

x <- x |> weight_draws(weights=lp__-lp_approx__, log=TRUE)

Piping resample_draws would also be useful (with added option for log) so that we could skip the weight_draws

x1 <- x |> resample_draws(weights=lp__-lp_approx__, log=TRUE, ndraws=1)
@avehtari avehtari changed the title piping weight_draws piping weight_draws / resample_draws Nov 9, 2023
@paul-buerkner
Copy link
Collaborator

It all depends on where we search for the weights vector. Currently, we search for it in the parent environment but we could of course change this to search in the draws object first and only then in the parent environment. This is not really related to whether we pipe or not, but in any case making this work would be a backwards compatibility breaking change that we should approach carefully.

@paul-buerkner paul-buerkner added the feature New feature or request label Nov 9, 2023
@avehtari
Copy link
Collaborator Author

avehtari commented Nov 9, 2023

I think this is related to piping in that way that the idea of piping is that we don't need to store temporary variables in the parent environment. I understand the possibility of breaking something, but I assume not many have used these functions yet.

Now that CmdStanR supports Laplace and Pathfinder, I'm going to use weight_draws() and resample_draws() often, but then would like to have a simple looking approach.

@avehtari
Copy link
Collaborator Author

avehtari commented Nov 9, 2023

I guess this is what I need to do now (assuming draws in matrix format)

draws8rhs <- draws8rhs |> mutate_variables(lw=lp__-lp_approx__)
draws8rhs1 <- draws8rhs |> 
    weight_draws(weights=extract_variable(draws8rhs,"lw"), log=TRUE) |>
    resample_draws(ndraws=1)

@paul-buerkner
Copy link
Collaborator

paul-buerkner commented Nov 9, 2023

Yes, good point. We should definitely consider the weights argument to search for variables in the draws object first.

(EDIT: Not working) As for the current approach, the following short work too, I think (not tested myself):

draws8rhs <- draws8rhs |> 
  mutate_variables(lw=lp__-lp_approx__) |> 
  weight_draws(weights=extract_variable(draws8rhs,"lw"), log=TRUE) |>
  resample_draws(ndraws=1)

@avehtari
Copy link
Collaborator Author

avehtari commented Nov 9, 2023

That doesn't work

+ Error in `[.default`(x, , i2, drop = FALSE) : subscript out of bounds

@paul-buerkner
Copy link
Collaborator

Ah right. Makes sense. Overlooked that one. Your approach is probably the currently sensible one and I agree it's ugly.

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

No branches or pull requests

2 participants