微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将“ ppd”类的R对象转换为干净的小标题?

如何解决如何将“ ppd”类的R对象转换为干净的小标题?

rstanarm :: posterior_predict()创建“ ppd”,“ matrix”,“ array”类的对象。我想将此类对象转换为干净的小标题。我尝试过:

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 61.1… 61.3… 58.1… 64.7… 59.4… 63.4… 63.9… 64.3… 68.9… 61.3… 65.7… 65.5… 65.5…
#>  2 68.2… 66.8… 68.5… 67.5… 66.9… 67.4… 67.4… 60.5… 62.8… 65.6… 70.8… 64.3… 63.7…
#>  3 66.3… 64.2… 73.2… 68.7… 60.6… 60.7… 64.3… 70.8… 65.0… 68.7… 65.0… 65.2… 60.2…
#>  4 65.4… 61.9… 64.6… 71.1… 59.6… 60.6… 63.0… 57.5… 69.2… 64.5… 63.7… 64.5… 57.0…
#>  5 62.8… 63.1… 59.8… 60.5… 67.8… 60.0… 52.1… 72.1… 66.8… 62.3… 58.0… 68.0… 67.4…
#>  6 70.8… 61.4… 57.8… 69.6… 63.1… 55.9… 67.5… 67.6… 73.8… 57.6… 60.4… 74.6… 64.6…
#>  7 61.7… 61.5… 69.3… 67.7… 70.8… 63.2… 63.5… 65.6… 64.4… 71.6… 67.9… 70.8… 68.2…
#>  8 66.6… 62.0… 74.1… 70.4… 63.9… 58.8… 58.5… 62.5… 70.0… 57.5… 53.4… 62.4… 54.5…
#>  9 67.4… 61.6… 62.7… 69.0… 64.0… 65.4… 62.3… 69.8… 72.0… 61.5… 67.1… 76.0… 70.4…
#> 10 71.6… 65.1… 72.7… 68.9… 57.5… 63.9… 64.9… 65.4… 63.5… 55.1… 71.9… 67.1… 65.7…
#> # … with 3,990 more rows,and 2 more variables: `14` <ppd>,`15` <ppd>

reprex package(v0.3.0)于2020-10-16创建

我希望每列都是dbl,就像pp是一个简单矩阵一样。但是,正如您所看到的,每一列本身就是ppd类的对象。如何将“ ppd”,“矩阵”,“数组”类的对象转换为带有简单数字列的小标题

解决方法

一种解决方案:

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  63.7  67.3  72.8  65.0  72.5  69.3  63.1  74.2  65.7  68.1  62.3  56.5  59.6
#>  2  63.1  63.0  55.1  60.6  66.7  69.2  69.2  65.8  65.9  61.5  70.3  63.4  63.9
#>  3  63.2  60.3  65.8  70.2  63.7  65.3  56.2  62.5  62.2  56.2  69.1  63.3  66.3
#>  4  67.6  59.8  62.3  59.1  61.1  68.6  65.4  63.6  65.4  72.0  71.1  61.8  69.4
#>  5  72.8  62.3  70.8  65.5  66.7  69.2  63.6  71.3  60.2  68.3  67.7  64.8  69.0
#>  6  60.2  66.1  66.5  69.8  64.9  60.7  63.6  69.9  64.8  65.7  64.9  61.2  59.6
#>  7  70.5  60.3  68.0  67.3  75.2  59.0  72.5  67.0  70.7  64.1  55.5  70.5  67.7
#>  8  53.5  64.3  62.9  73.1  68.4  59.2  68.7  67.8  67.0  77.9  68.0  70.8  71.6
#>  9  72.6  65.0  74.3  71.7  65.2  69.1  64.6  64.9  67.0  66.9  56.1  52.3  73.8
#> 10  60.7  62.5  67.3  63.8  64.6  65.1  65.9  64.1  66.5  64.3  63.1  50.0  70.1
#> # … with 3,990 more rows,and 2 more variables: `14` <dbl>,`15` <dbl>

reprex package(v0.3.0)于2020-10-29创建

当然有一种更简单的方法。 。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。