【问题标题】:tab_corr, tab_df, and psych::describe on a mice mids objecttab_corr、tab_df 和 psych::describe 在鼠标 mids 对象上
【发布时间】:2022-01-16 02:53:35
【问题描述】:

我已将估算数据保存为 mids 对象,并且正在尝试围绕估算数据调整我通常的工作流程。但是,我无法弄清楚如何在 mids 对象上使用 sjPlot 的 tab_corr()tab_df() 和 psych 的 describe

我的目标是生成一个描述性统计表和一个相关矩阵,而不是对估算的数据集进行平均。我能够使用miceadds::micombine.cor 生成相关性,但输出的格式不像典型的相关矩阵。我还可以单独计算来自mids 对象的变量的平均值、SD 等,但我正在寻找能够生成表格的东西。

library(mice)
library(miceadds)
library(sjPlot)
library(tidyverse)
library(psych)
set.seed(123)

## correlation matrix

data(nhanes)
imp <- mice(nhanes, print = FALSE)
head(micombine.cor(mi.res = imp)) # ugly
#>   variable1 variable2           r       rse    fisher_r fisher_rse        fmi
#> 1       age       bmi -0.38765907 0.1899398 -0.40904214  0.2234456 0.09322905
#> 2       age       hyp  0.51588273 0.1792162  0.57071301  0.2443348 0.25939786
#> 3       age       chl  0.37685482 0.2157535  0.39638877  0.2515615 0.30863126
#> 4       bmi       hyp -0.01748158 0.2244419 -0.01748336  0.2245067 0.10249784
#> 5       bmi       chl  0.29082393 0.2519295  0.29946608  0.2752862 0.44307791
#> 6       hyp       chl  0.30271060 0.1984525  0.31250096  0.2185381 0.04935528
#>             t          p     lower95   upper95
#> 1 -1.83061192 0.06715849 -0.68949235 0.0288951
#> 2  2.33578315 0.01950255  0.09156846 0.7816509
#> 3  1.57571320 0.11509191 -0.09636276 0.7111171
#> 4 -0.07787455 0.93792784 -0.42805131 0.3990695
#> 5  1.08783556 0.27666771 -0.23557593 0.6852881
#> 6  1.42996130 0.15272813 -0.11531056 0.6296450

data(iris)
iris %>% 
  select(-c(Species)) %>% 
  tab_corr() # pretty
    Sepal.Length    Sepal.Width Petal.Length    Petal.Width
Sepal.Length        -0.118  0.872\*\*\* 0.818\*\*\*
Sepal.Width -0.118      -0.428\*\*\*    -0.366\*\*\*
Petal.Length    0.872\*\*\* -0.428\*\*\*        0.963\*\*\*
Petal.Width 0.818\*\*\* -0.366\*\*\*    0.963\*\*\*  
Computed correlation used pearson-method with listwise-deletion.
## descriptive statistics

psych::describe(imp) # error
#> Warning in mean.default(x, na.rm = na.rm): argument is not numeric or logical:
#> returning NA
#> Error in is.data.frame(x): 'list' object cannot be coerced to type 'double'
mean(imp$data$age) # inefficient
#> [1] 1.76

iris %>% 
  select(-c(Species)) %>% 
  psych::describe() %>% 
  select(-(c(vars, n, median, trimmed, mad))) %>%
  tab_df() # pretty
mean    sd  min max range   skew    kurtosis    se
5.84    0.83    4.30    7.90    3.60    0.31    -0.61   0.07
3.06    0.44    2.00    4.40    2.40    0.31    0.14    0.04
3.76    1.77    1.00    6.90    5.90    -0.27   -1.42   0.14
1.20    0.76    0.10    2.50    2.40    -0.10   -1.36   0.06
Created on 2021-12-11 by the reprex package (v2.0.1)

【问题讨论】:

    标签: r r-mice psych sjplot


    【解决方案1】:

    我现在看到了我的错误 - 我在使用 imp 时应该使用 imp$data。这适用于tab_dftab_corr,但不适用于tab_model

    library(tidyverse)
    library(sjPlot)
    library(mice)
    library(psych)
    
    set.seed(123)
    
    # Imputed data
    data(nhanes)
    imp <- mice(nhanes, print = FALSE)
    
    ## tab_df
    imp$data %>% 
      select(age, bmi, chl) %>%
      psych::describe() %>%
      tab_df(.)
    vars    n   mean    sd  median  trimmed mad min max range   skew    kurtosis    se
    1   25  1.76    0.83    2.00    1.71    1.48    1.00    3.00    2.00    0.44    -1.47   0.17
    2   16  26.56   4.22    26.75   26.38   4.60    20.40   35.30   14.90   0.39    -0.83   1.05
    3   15  191.40  45.22   187.00  190.31  28.17   113.00  284.00  171.00  -0.09   -0.43   11.67
    
    
    ## tab_corr
    imp$data %>%
      select(age, bmi, chl) %>% 
      tab_corr(.)
        age bmi chl
    age     -0.337  0.599\*
    bmi -0.337      0.373
    chl 0.599\* 0.373    
    Computed correlation used pearson-method with listwise-deletion.
    
    
    ## tab_model
    mod <- with(imp, lm(age ~ bmi)) %>% pool()
    summary(mod)
    #>          term    estimate  std.error statistic       df     p.value
    #> 1 (Intercept)  3.85793713 1.12871574  3.417988 16.93708 0.003292004
    #> 2         bmi -0.07826576 0.04182269 -1.871371 16.64170 0.078980518
    tab_model(mod) # error...
    #> Error in fam.info$is_linear || identical(fam.info$link_function, "identity"): invalid 'x' type in 'x || y'
    

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多