cjvanlissa / tidySEM

55 stars 7 forks source link

Feature request: Rename variables in output #56

Open sda030 opened 2 years ago

sda030 commented 2 years ago

Feature request: One of the greater advantages of the tidySEM-structure (dictionary, data, syntax) is the possibility to reattach variable labels (and even categorical value labels, though a lot harder) to output. For instance, I would have expected this to somehow show the original variable labels in the table_results():

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidySEM)
#> Loading required package: OpenMx
library(labelled)
data_mix_ordinal %>%
    rename_with(.fn = ~paste0("u_", 1:4)) %>%
    set_variable_labels(u_1="Self-efficacy 1", u_2="Self-efficacy 2", u_3="Self-efficacy 3", u_4="Self-efficacy 4") %>%
    tidy_sem() %>%
    measurement() %>%
    estimate_mx() %>%
    table_results()
#> Running model with 12 parameters
#>                label  est_sig         se        confint
#> 1  Loadings.u.BY.u_2     0.18 0.11553327  [-0.05, 0.40]
#> 2  Loadings.u.BY.u_3 -0.82*** 0.21537285 [-1.24, -0.40]
#> 3  Loadings.u.BY.u_4 -0.69*** 0.18447266 [-1.05, -0.33]
#> 4      Variances.u_1  0.77*** 0.03335609   [0.70, 0.83]
#> 5      Variances.u_2  0.87*** 0.01768705   [0.84, 0.90]
#> 6      Variances.u_3  0.81*** 0.02500562   [0.76, 0.86]
#> 7      Variances.u_4  0.83*** 0.02213540   [0.79, 0.88]
#> 8        Variances.u   0.10** 0.03064678   [0.04, 0.16]
#> 9          Means.u_1  0.90*** 0.01314860   [0.88, 0.93]
#> 10         Means.u_2  0.89*** 0.01321310   [0.86, 0.91]
#> 11         Means.u_3  0.91*** 0.01324928   [0.89, 0.94]
#> 12         Means.u_4  0.91*** 0.01326363   [0.89, 0.94]

Created on 2022-07-17 by the reprex package (v2.0.1)

cjvanlissa commented 2 years ago

This seems out of scope for tidySEM.. my focus is on developing the mixture model capabilities. Although you/someone else is welcome to contribute a pull request!

haozhou1988 commented 2 years ago

Could we add tidy_sem two methods which extract data variable name label and data value? I think it's much easier for users to build the final report table if tidy_sem function can provide those label infomation

library(labelled)
library(tidyverse)
library(tidySEM)
#> Loading required package: OpenMx
KF <-
mosaicData::KidsFeet %>%
rename_with(.fn = ~paste0("u_", 1:8)) %>%
set_variable_labels(u_1="Self-efficacy 1",
u_2="Self-efficacy 2",
u_3="Self-efficacy 3",
u_4="Self-efficacy 4")
tidy_sem_test <- KF %>% tidy_sem() 
# X indicates the data set doesn't have data value labels
tidy_sem_test
#> A tidy_sem object
#> v    $dictionary
#> v    $data
#> o    $syntax
#> v    $var_labelled
#> X    $val_labelled
tidy_sem_test$var_labelled
#>   var_name     var_lablled
#> 1      u_1 Self-efficacy 1
#> 2      u_2 Self-efficacy 2
#> 3      u_3 Self-efficacy 3
#> 4      u_4 Self-efficacy 4
#> 5      u_5                
#> 6      u_6                
#> 7      u_7                
#> 8      u_8
tidy_sem_test$val_labelled
#> NULL

Created on 2022-09-22 with reprex v2.0.2

cjvanlissa commented 1 year ago

@sda030 if you use table_results(columns = NULL) you will get ALL available information, including variable labels. I think that will help you.

@haozhou1988 I think this is the way to go. A relabeling function that can be called at the end of an analysis pipeline.