Skip to content

Rename the 'fitted' method #644

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

Closed
paul-buerkner opened this issue Apr 15, 2019 · 29 comments
Closed

Rename the 'fitted' method #644

paul-buerkner opened this issue Apr 15, 2019 · 29 comments

Comments

@paul-buerkner
Copy link
Owner

I know that multiple people including @jgabry and @bgoodri dislike the name fitted used in brms to compute expected values of the posterior predictive distribution, and I agree. However, we still need a good new name to take the place of fitted. How does, for instance, posterior_expect sound to you? I am open to suggestions.

@bgoodri
Copy link

bgoodri commented Apr 15, 2019 via email

@paul-buerkner
Copy link
Owner Author

That would be ideal, yes.

@bgoodri
Copy link

bgoodri commented Apr 15, 2019 via email

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Apr 15, 2019

What do you mean by "expectation of the coefficients"? It's not that fitted.brmsfit took the means of the coeffiecients and transformed them somehow, but I guess this is not what you ment.

In any case, I am open to other names, as long as we get one that can be used consistently across packages.

@bgoodri
Copy link

bgoodri commented Apr 15, 2019 via email

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Apr 15, 2019

What about pp_expect or pp_expectation? We are using pp as abbreviation for posterior predictions in other places anyway.

@bgoodri
Copy link

bgoodri commented Apr 15, 2019 via email

@paul-buerkner
Copy link
Owner Author

Great! Of those two, I would prefer pp_expect, I think. @jgabry what is your opinion on this?

@jgabry
Copy link
Contributor

jgabry commented Apr 15, 2019

Is this just colMeans(posterior_predict(fit))? If so I’m not sure it needs a method itself, but I’m not opposed to it and I’m fine with that name.

@paul-buerkner
Copy link
Owner Author

It's like posterior_linpred(..., transform = TRUE) so it should be easy to make a method in rstanarm as well.

@jgabry
Copy link
Contributor

jgabry commented Apr 16, 2019

Oh I see. Yeah I’m fine with that. Can you open an rstantools issue? Or feel free to just add it to rstantools if if you want.

@paul-buerkner
Copy link
Owner Author

Sure. I guess I just make a PR for the generic.

@statwonk
Copy link

I think the lognormal might be an example of what @bgoodri means. mu is the median not the expected value.

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Apr 28, 2019 via email

@elliottmorris
Copy link

elliottmorris commented Jan 24, 2020

Hi @paul-buerkner.

Sorry, but after these changes I cannot figure out how to recreate the old output from posterior_linpred using pp_expect.

Specifically, I used to run:

draws <- posterior_linpred(model,
    newdata=df,
    draws=1000,
    allow_new_levels=T,
    transform=T)

But the following is not yielding the same results:

draws <- brms::pp_expect(model,
    newdata=df,
    nsamples=1000,
    allow_new_levels =T,
    scale='linear')

I'd be grateful for any help you can provide.

E

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Jan 24, 2020 via email

@elliottmorris
Copy link

You're saying I shouldn't make the switch over? Warnings say it's deprecated right?

Either way, shouldn't I be able to recreate posterior predictions for the linear predictor with the new function?

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Jan 24, 2020 via email

@elliottmorris
Copy link

I will put one together if by next week I still can't replicate across models. Thanks Paul

@elliottmorris
Copy link

Yes it seems that the posterior_linpred function has changed pretty dramatically over the past few months.

It used to return a draw of the linear predictor for every specified run. Now it looks to be returning a draw from the distribution of the average of the linear predictor. That's pretty different functionality.

What am I missing? Are these changes documented somewhere?

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Jan 24, 2020 via email

@elliottmorris
Copy link

Excellent. Thanks again.

@elliottmorris
Copy link

Sorry just an FYI again. It seems like the behavior changed between 2.9 and 2.10, not 2.11.

@paul-buerkner
Copy link
Owner Author

pp_expect(..., scale = "linear") does not work because pp_expect is supposed to always return the expected value of the posterior predictive distribution and thus has no (user facing) scale argument. Existing functions contiue to work in the same way. For instance,

posterior_linpred(model)
fitted(model, scale = "linear", summary = FALSE)

yield the same results.

@elliottmorris
Copy link

elliottmorris commented Jan 25, 2020 via email

@paul-buerkner
Copy link
Owner Author

paul-buerkner commented Jan 25, 2020 via email

@elliottmorris
Copy link

elliottmorris commented Jan 25, 2020

Yes, the issue I'm having is with categorical models.

I'm pasting some code below and attaching a zip for the example. (The typical reprex::reprex() approach was having issues loading different brms versions).

You can see that the outcome has changed. I just can't figure out how to recreate the old predictions for the linear predictor. These new ones are wrong.

Thanks again for all the help and dev!

Restarting R session...

> library(tidyverse)
── Attaching packages ────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.2.1     ✓ purrr   0.3.3
✓ tibble  2.1.3     ✓ dplyr   0.8.3
✓ tidyr   1.0.0     ✓ stringr 1.4.0
✓ readr   1.3.1     ✓ forcats 0.4.0
── Conflicts ───────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
> library(brms)
Loading required package: Rcpp
Loading 'brms' package (version 2.10.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').

> # read in generated data files 
> targets <- read_rds('targets.rds')
> cces <- read_rds('sample_cces.rds')
> # just read it in if already run
> vote_model <- read_rds("vote_model.rds")

> # draws
> pred <- posterior_linpred(object=vote_model,
+                           newdata=targets,
+                           allow_new_levels=TRUE,
+                           nsamples=500,
+                           transform = TRUE)

> # look at range of preds for clinton
> # they go from roughly 0 to 1 in brms 2.10.0 but much smaller range in brms 2.11.0
> summary(colMeans(pred[,,1]))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.03755 0.33496 0.57316 0.56024 0.80095 0.99251 


> # now compare colmeans to 2.11 -----
> detach("package:brms", unload=TRUE)
> remove.packages('brms')
Removing package from ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library’
(as ‘lib’ is unspecified)
> devtools::install_version("brms", version = "2.11.0", repos = "http://cran.us.r-project.org")
Downloading package from url: http://cran.us.r-project.org/src/contrib/Archive/brms/brms_2.11.0.tar.gz
* installing *source* package ‘brms’ ...
* DONE (brms)
> library(brms)
Loading required package: Rcpp
Loading 'brms' package (version 2.11.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').

> # draws
> pred <- posterior_linpred(object=vote_model,
+                           newdata=targets,
+                           allow_new_levels=TRUE,
+                           nsamples=500,
+                           transform = TRUE)
Warning message:
posterior_linpred(transform = TRUE) is deprecated.Please use pp_expect() instead. 

> # look at range of preds for clinton
> # they go from roughly 0 to 1 in brms 2.10.0 but much smaller range in brms 2.11.0
> summary(colMeans(pred[,,1]))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.2838  0.4708  0.5115  0.4965  0.5371  0.5515 

EXAMPLE ZIP: paul_reprex.zip

@paul-buerkner
Copy link
Owner Author

Thank you for providing a reproducible example. I don't know what caused the issue but it seems to be working correctly again in the latest github version of brms (2.11.4).

@elliottmorris
Copy link

elliottmorris commented Jan 27, 2020 via email

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

No branches or pull requests

5 participants