Home > OS >  How to exract specific values from the lists created by a statistical model (t-test)?
How to exract specific values from the lists created by a statistical model (t-test)?

Time:09-18

How can extract statistics from this model. To conduct several T-tests I used this:

A<-lapply(merged_DF_final[2:6], function(x) t.test(x ~ merged_DF_final$Group))

How can I extract information about the p-value, t statistics, confidence interval, and group means for each specific subtest and output on a single table?

This is what is saved on A:

$HC_HC_L_amygdala_baseline

    Welch Two Sample t-test

data:  x by merged_DF_final$Group t = 0.039543, df = 47.412, p-value =
0.9686 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -0.4694404  0.4882694 sample estimates: mean in group CONN   mean in group HC 
         0.2954200          0.2860055 


$HC_HC_L_culmen_baseline

    Welch Two Sample t-test

data:  x by merged_DF_final$Group t = 0.81387, df = 53.695, p-value =
0.4193 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -0.2970321  0.7028955 sample estimates: mean in group CONN   mean in group HC 
         0.4020883          0.1991566 


$HC_HC_L_fusiform_baseline

    Welch Two Sample t-test

data:  x by merged_DF_final$Group t = 0.024945, df = 53.851, p-value =
0.9802 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -0.5768786  0.5914136 sample estimates: mean in group CONN   mean in group HC 
         0.5552184          0.5479509 


$HC_HC_L_insula_baseline

    Welch Two Sample t-test

data:  x by merged_DF_final$Group t = 0.79659, df = 52.141, p-value =
0.4293 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -0.3000513  0.6951466 sample estimates: mean in group CONN   mean in group HC 
        0.12436946        -0.07317818 


$HC_HC_L_lingual_gyrus_baseline

    Welch Two Sample t-test

data:  x by merged_DF_final$Group t = -0.11033, df = 53.756, p-value =
0.9126 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -0.5172863  0.4633268 sample estimates: mean in group CONN   mean in group HC 
         0.4395066          0.4664864

CodePudding user response:

Look at names(A[[1]]) or str(A[[1]]) to see what the components are, then use $ or [[ to extract them, e.g.

names(t.test(extra ~ group, data = sleep))
 [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
 [6] "null.value"  "stderr"      "alternative" "method"      "data.name"

You can then sapply(A, "[[", "statistic") or (being more careful) vapply(A, "[[", "statistic", FUN.VALUE = numeric(1))

If you like tidyverse you can purrr::map_dbl(A, "statistic") (for results with a single value); you'll need purrr::map(A, ~.$estimate[1]) for the mean of the first group etc.. (sapply() will automatically collapse to a matrix.)

  • Related