How to convert objects of class "ppd" to a clean tibble?

rstanarm::posterior_predict() creates objects of class "ppd" "matrix" "array" . I want to convert such objects to clean tibbles. I tried:

library(tidyverse)
library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.21.1
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())

obj <- stan_glm(data = women, height ~ 1, refresh = 0)

pp <- posterior_predict(obj)

pp %>% 
  as_tibble() 
#> # A tibble: 4,000 x 15
#>    `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`   `10`  `11`  `12`  `13` 
#>    <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd>
#>  1 66.4… 61.5… 67.9… 59.4… 64.8… 60.4… 60.5… 63.9… 67.1… 66.1… 66.6… 65.7… 67.0…
#>  2 70.3… 60.0… 57.9… 58.2… 67.6… 63.1… 52.7… 62.5… 67.3… 51.3… 59.9… 63.7… 56.9…
#>  3 65.4… 65.1… 59.6… 69.3… 73.6… 63.8… 69.2… 72.8… 71.9… 61.6… 73.8… 64.0… 68.1…
#>  4 67.3… 63.7… 67.9… 66.5… 60.1… 63.1… 61.1… 71.1… 71.9… 63.7… 64.2… 61.4… 70.9…
#>  5 66.3… 62.2… 61.1… 66.5… 63.3… 68.3… 61.2… 59.3… 63.4… 69.5… 65.3… 61.0… 67.4…
#>  6 77.7… 55.4… 64.5… 67.2… 72.8… 55.1… 60.2… 67.3… 53.8… 59.5… 67.8… 65.6… 53.0…
#>  7 67.4… 68.8… 68.3… 70.9… 67.4… 65.4… 66.5… 64.7… 65.1… 69.9… 60.5… 68.3… 60.9…
#>  8 63.6… 81.7… 73.0… 61.7… 69.3… 69.9… 68.7… 69.4… 64.8… 62.0… 61.5… 65.3… 65.0…
#>  9 58.8… 63.4… 64.5… 67.5… 64.9… 60.1… 66.8… 65.6… 62.5… 63.8… 65.4… 58.5… 63.0…
#> 10 62.5… 60.3… 70.7… 72.0… 72.2… 76.3… 71.4… 65.9… 66.6… 72.3… 67.4… 64.1… 68.5…
#> # … with 3,990 more rows, and 2 more variables: `14` <ppd>, `15` <ppd>

Created on 2020-10-14 by the reprex package (v0.3.0)

I would like each column to be a double, as we would get if pp were a simple matrix. But, as you can see, each column is, itself, an object of class ppd. How can I convert an object of class "ppd" "matrix" "array" to a clean tibble?

For converting to a tibble, I believe the tidybayes package has you covered (see e.g., https://cran.r-project.org/web/packages/tidybayes/vignettes/tidy-rstanarm.html#posterior-fits )

Also, you can use as.matrix on the results of posterior_predict to convert to matrix.

Does that answer your question?

Thanks for your help! But adding as.matrix() does not solve the problem. The resulting object still has "ppd" as the type for each column when what I want is a simple dbl.

library(tidyverse)
library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.21.1
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())


obj <- stan_glm(data = women, height ~ 1, refresh = 0)

pp <- posterior_predict(obj)

pp %>% 
  as.matrix() %>% 
  as_tibble() 
#> # A tibble: 4,000 x 15
#>    `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`   `10`  `11`  `12`  `13` 
#>    <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd> <ppd>
#>  1 63.5… 70.2… 64.4… 55.8… 62.3… 65.7… 57.5… 63.3… 65.4… 58.5… 68.0… 61.8… 60.4…
#>  2 58.1… 55.8… 70.7… 70.0… 69.1… 73.1… 68.9… 51.4… 75.9… 69.2… 76.1… 69.8… 68.9…
#>  3 68.3… 69.4… 64.0… 57.3… 65.2… 64.5… 61.3… 64.4… 76.7… 64.9… 61.0… 62.0… 63.9…
#>  4 66.1… 71.4… 62.1… 59.0… 61.3… 61.0… 53.2… 72.4… 64.6… 64.4… 66.9… 62.0… 71.6…
#>  5 70.7… 63.4… 67.2… 63.3… 65.6… 65.6… 67.9… 63.3… 63.5… 62.1… 59.7… 61.3… 71.5…
#>  6 63.9… 52.4… 71.5… 59.7… 67.0… 69.1… 74.2… 64.5… 60.9… 58.2… 66.1… 69.6… 66.2…
#>  7 67.6… 61.0… 64.8… 64.9… 69.0… 72.3… 64.0… 72.5… 70.4… 71.8… 70.1… 66.6… 68.1…
#>  8 63.6… 74.4… 70.6… 62.3… 63.0… 65.9… 69.4… 75.7… 65.2… 58.5… 69.9… 62.6… 65.8…
#>  9 68.7… 65.7… 66.4… 62.7… 67.5… 63.0… 73.9… 62.9… 60.5… 65.5… 67.4… 70.6… 56.6…
#> 10 65.9… 60.5… 62.3… 67.9… 69.1… 66.8… 72.5… 67.6… 55.8… 66.3… 62.4… 60.3… 61.4…
#> # … with 3,990 more rows, and 2 more variables: `14` <ppd>, `15` <ppd>

Created on 2020-10-15 by the reprex package (v0.3.0)

And thanks for the link to tidybayes, but the section you link to is for doing the equivalent of rstanarm::posterior_linpred(), when I am creating poster_predict() objects.

Here is an option that seems to work:

library(tidyverse)
library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.21.1
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())

obj <- stan_glm(data = women, height ~ 1, refresh = 0)

posterior_predict(obj) %>% 
  as_tibble() %>% 
  mutate(across(everything(), as.numeric))
#> # A tibble: 4,000 x 15
#>      `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`  `10`  `11`  `12`  `13`
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  58.9  63.9  49.2  58.6  75.0  62.4  67.4  51.4  62.8  67.1  67.7  57.8  54.2
#>  2  75.6  65.2  63.5  65.7  60.1  63.6  74.4  57.3  55.6  60.2  61.9  60.7  57.2
#>  3  56.5  64.8  65.0  52.7  59.9  70.0  64.6  57.9  70.8  68.5  75.0  49.7  62.3
#>  4  50.0  64.3  73.4  56.4  58.2  70.3  57.9  59.1  57.0  65.3  54.8  55.1  69.8
#>  5  64.1  62.0  64.4  58.8  58.6  62.3  63.9  65.2  62.2  66.7  61.7  59.4  70.4
#>  6  70.6  67.3  63.6  68.3  55.1  62.5  61.9  70.6  68.9  65.9  67.1  69.4  61.6
#>  7  62.8  65.9  63.7  69.3  65.1  67.1  67.0  66.9  71.9  75.2  69.7  67.5  65.2
#>  8  58.6  69.8  65.8  67.2  66.4  62.0  64.9  68.2  78.3  67.9  70.4  71.7  57.8
#>  9  65.5  66.4  68.0  68.1  63.4  62.0  68.8  61.2  67.6  71.3  70.0  67.2  68.2
#> 10  67.4  66.7  64.9  64.9  68.1  60.2  59.9  64.9  62.0  58.4  64.6  70.4  65.7
#> # … with 3,990 more rows, and 2 more variables: `14` <dbl>, `15` <dbl>

Created on 2020-10-29 by the reprex package (v0.3.0)

I hate it because, for teaching purposes, that mutate(across nonsense is very hard for students to grasp. Are there better solutions?

alternatively

mutate_all(as.numeric)

Thanks! That is much better!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.