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

pretty_names = TRUE do not fully work for "repeated rows" #723

Open
iago-pssjd opened this issue Jun 2, 2022 · 1 comment
Open

pretty_names = TRUE do not fully work for "repeated rows" #723

iago-pssjd opened this issue Jun 2, 2022 · 1 comment
Labels
3 investigators ❔❓ Need to look further into this issue

Comments

@iago-pssjd
Copy link

iago-pssjd commented Jun 2, 2022

In the table outputs of the next reprex, we should see Indirect Effect (ACME) in each file where ACME appears and the same for Direct Effect (ADE) with ADE, however, for each table they only appear in the first ACME/ADE rows. In particular I added the line attr(m, "pretty_names") <- c(attr(m20, "pretty_names"), attr(m70, "pretty_names")) to be sure the pretty_names are for all the involved rows.

library(mediation)
#> Loading required package: MASS
#> Loading required package: Matrix
#> Loading required package: mvtnorm
#> Loading required package: sandwich
#> mediation: Causal Mediation Analysis
#> Version: 4.5.0
library(parameters)

data(jobs)
# Allowing ``moderated mediation'' with respect to age
b.int <- lm(job_seek ~ treat*age + econ_hard + sex, data=jobs)
d.int <- lm(depress2 ~ treat*job_seek*age + econ_hard + sex, data=jobs)
contcont.age20 <- mediate(b.int, d.int, sims=50, treat="treat", mediator="job_seek",
            covariates = list(age = 20))
contcont.age70 <- mediate(b.int, d.int, sims=50, treat="treat", mediator="job_seek",
            covariates = list(age = 70))
m20 <- model_parameters(contcont.age20)
m70 <- model_parameters(contcont.age70)
m20[["age level"]] <-  20
m70[["age level"]] <-  70
m <- rbind(m20, m70)
attr(m, "pretty_names") <- c(attr(m20, "pretty_names"), attr(m70, "pretty_names"))
str(m)
#> Classes 'parameters_model', 'see_parameters_model' and 'data.frame': 20 obs. of  8 variables:
#>  $ Parameter: chr  "ACME (control)" "ACME (treated)" "ADE (control)" "ADE (treated)" ...
#>  $ Estimate : num  -0.0459 -0.0274 -0.0502 -0.0318 -0.0776 ...
#>  $ CI       : num  0.95 0.95 0.95 0.95 0.95 0.95 0.95 0.95 0.95 0.95 ...
#>  $ CI_low   : num  -0.1078 -0.0656 -0.1868 -0.1682 -0.2264 ...
#>  $ CI_high  : num  0.00768 0.00874 0.06707 0.10573 0.06468 ...
#>  $ p        : num  0.12 0.12 0.48 0.68 0.36 0.32 0.32 0.12 0.56 0.32 ...
#>  $ Component: chr  "control" "treated" "control" "treated" ...
#>  $ age level: num  20 20 20 20 20 20 20 20 20 20 ...
#>  - attr(*, "object_name")= chr "contcont.age20"
#>  - attr(*, "pretty_names")= Named chr [1:20] "Indirect Effect (ACME)" "Indirect Effect (ACME)" "Direct Effect (ADE)" "Direct Effect (ADE)" ...
#>   ..- attr(*, "names")= chr [1:20] "ACME" "ACME" "ADE" "ADE" ...
#>  - attr(*, "ci")= num 0.95
#>  - attr(*, "verbose")= logi TRUE
#>  - attr(*, "exponentiate")= logi FALSE
#>  - attr(*, "ordinal_model")= logi FALSE
#>  - attr(*, "linear_model")= logi FALSE
#>  - attr(*, "mixed_model")= logi FALSE
#>  - attr(*, "model_class")= chr "mediate"
#>  - attr(*, "bootstrap")= logi FALSE
#>  - attr(*, "iterations")= num 1000
#>  - attr(*, "robust_vcov")= logi FALSE
#>  - attr(*, "ignore_group")= logi TRUE
#>  - attr(*, "ran_pars")= logi TRUE
#>  - attr(*, "show_summary")= logi FALSE
#>  - attr(*, "weighted_nobs")= num 899
#>  - attr(*, "coefficient_name")= chr "Coefficient"
#>  - attr(*, "zi_coefficient_name")= chr "Log-Odds"
#>  - attr(*, "digits")= num 2
#>  - attr(*, "ci_digits")= num 2
#>  - attr(*, "p_digits")= num 3
#>  - attr(*, "footer_digits")= num 3
print_md(m)
Parameter Estimate 95% CI p age level
Indirect Effect (ACME) -0.05 (-0.11, 7.68e-03) 0.120 20.00
Direct Effect (ADE) -0.05 (-0.19, 0.07) 0.480 20.00
Prop. Mediated 0.33 (-4.73, 2.78) 0.320 20.00
ACME 0.03 (-0.06, 0.13) 0.720 70.00
ADE -0.08 (-0.34, 0.18) 0.600 70.00
Prop. Mediated 0.04 (-3.08, 5.61) 0.880 70.00

control

Parameter Estimate 95% CI p age level
Indirect Effect (ACME) -0.03 (-0.07, 8.74e-03) 0.120 20.00
Direct Effect (ADE) -0.03 (-0.17, 0.11) 0.680 20.00
Prop. Mediated 0.25 (-2.45, 1.33) 0.320 20.00
ACME 0.02 (-0.05, 0.10) 0.600 70.00
ADE -0.09 (-0.34, 0.16) 0.640 70.00
Prop. Mediated 0.05 (-1.54, 3.22) 0.920 70.00

treated

Parameter Estimate 95% CI p age level
Indirect Effect (ACME) -0.04 (-0.09, 8.21e-03) 0.120 20.00
Direct Effect (ADE) -0.04 (-0.18, 0.08) 0.560 20.00
Prop. Mediated 0.29 (-3.55, 1.88) 0.320 20.00
ACME 0.02 (-0.05, 0.11) 0.600 70.00
ADE -0.08 (-0.34, 0.17) 0.680 70.00
Prop. Mediated 0.04 (-2.04, 4.03) 0.920 70.00

average

Parameter Estimate 95% CI p age level
Total Effect -0.08 (-0.23, 0.06) 0.360 20.00
Total Effect -0.06 (-0.38, 0.22) 0.800 70.00

Total Effect

sessionInfo()
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 11 (bullseye)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.13.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] parameters_0.18.1 mediation_4.5.0   sandwich_3.0-1    mvtnorm_1.1-3    
#> [5] Matrix_1.4-1      MASS_7.3-57      
#> 
#> loaded via a namespace (and not attached):
#>  [1] splines_4.2.0       R.utils_2.11.0      datawizard_0.4.1   
#>  [4] Formula_1.2-4       assertthat_0.2.1    latticeExtra_0.6-29
#>  [7] yaml_2.3.5          bayestestR_0.12.1   pillar_1.7.0       
#> [10] backports_1.4.1     lattice_0.20-45     glue_1.6.2         
#> [13] digest_0.6.29       RColorBrewer_1.1-3  checkmate_2.1.0    
#> [16] minqa_1.2.4         colorspace_2.0-3    htmltools_0.5.2    
#> [19] R.oo_1.24.0         lpSolve_5.6.15      pkgconfig_2.0.3    
#> [22] xtable_1.8-4        purrr_0.3.4         scales_1.2.0       
#> [25] jpeg_0.1-9          lme4_1.1-29         emmeans_1.7.4-1    
#> [28] tibble_3.1.7        htmlTable_2.4.0     styler_1.7.0       
#> [31] generics_0.1.2      ggplot2_3.3.6       ellipsis_0.3.2     
#> [34] TH.data_1.1-1       withr_2.5.0         nnet_7.3-17        
#> [37] cli_3.3.0           survival_3.3-1      magrittr_2.0.3     
#> [40] crayon_1.5.1        estimability_1.3    evaluate_0.15      
#> [43] R.methodsS3_1.8.1   fs_1.5.2            fansi_1.0.3        
#> [46] R.cache_0.15.0      nlme_3.1-157        foreign_0.8-82     
#> [49] tools_4.2.0         data.table_1.14.2   multcomp_1.4-19    
#> [52] lifecycle_1.0.1     stringr_1.4.0       munsell_0.5.0      
#> [55] reprex_2.0.1        cluster_2.1.3       compiler_4.2.0     
#> [58] rlang_1.0.2         grid_4.2.0          nloptr_2.0.3       
#> [61] rstudioapi_0.13     htmlwidgets_1.5.4   base64enc_0.1-3    
#> [64] rmarkdown_2.14      boot_1.3-28         codetools_0.2-18   
#> [67] gtable_0.3.0        DBI_1.1.2           R6_2.5.1           
#> [70] gridExtra_2.3       zoo_1.8-10          knitr_1.39         
#> [73] dplyr_1.0.9         fastmap_1.1.0       utf8_1.2.2         
#> [76] Hmisc_4.7-0         insight_0.17.1      stringi_1.7.6      
#> [79] Rcpp_1.0.8.3        vctrs_0.4.1         rpart_4.1.16       
#> [82] png_0.1-7           coda_0.19-4         tidyselect_1.1.2   
#> [85] xfun_0.31

Created on 2022-06-02 by the reprex package (v2.0.1)

@strengejacke strengejacke added the 3 investigators ❔❓ Need to look further into this issue label Jun 3, 2022
@iago-pssjd
Copy link
Author

iago-pssjd commented Jun 3, 2022

A question on pretty_names. I see they can be used for the model_parameters of certain objects/classes, like regressions, while cannot be used with the model_parameters of others, like htest.

Indeed, I see that

model <- t.test(iris$Sepal.Width, iris$Sepal.Length)
mmp <- model_parameters(model)
attr(mmp, "pretty_names") <- "mpg"
mmp |> print_md()
Error in `$<-.data.frame`(`*tmp*`, "Parameter", value = character(0)) : 
  replacement has 0 rows, data has 1

as the model_parameters output does not include Parameter, however

model <- t.test(mtcars$mpg ~ mtcars$vs)
mmp <- model_parameters(model)
attr(mmp, "pretty_names") <- "mpg"
mmp |> print_md()


Table: Welch Two Sample t-test

|Parameter  |     Group | mtcars$vs = 0 | mtcars$vs = 1 | Difference |          95% CI | t(22.72) |      p |
|:----------|:---------:|:-------------:|:-------------:|:----------:|:---------------:|:--------:|:------:|
|mtcars$mpg | mtcars$vs |         16.62 |         24.56 |      -7.94 | (-11.46, -4.42) |    -4.67 | < .001 |

has no effect.

Then, my question is if is it some place where it is specified for which models pretty_names is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 investigators ❔❓ Need to look further into this issue
Projects
None yet
Development

No branches or pull requests

2 participants