PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label t-test. Show all posts
Showing posts with label t-test. Show all posts

Friday, October 7, 2022

[FIXED] How to save t-test output with names of columns into a dataframe in R?

 October 07, 2022     dataframe, lapply, r, statistics, t-test     No comments   

Issue

I have a dataframe df with following data:

df <- structure(list(group = c("cluster2", "cluster2", 
"cluster1", "cluster2", "cluster2", "cluster2", 
"cluster2", "cluster1", "cluster1", "cluster2"
), One = c(-0.614639315096381, 0.88834977627436, 
0.0832368160901144, 0.00321829065579383, -1.04180850739839, 0.0631077979797929, 
0.590396305169489, 0.103255370028437, 0.309521983720179, -0.101131803547097
), two = c(-0.431029772056812, 1.01576532724102, 
1.8217432927475, 0.369890996511319, -1.67807784856458, 0.470436956726829, 
0.0136060165886214, -0.975047520238315, -0.72883862189043, -0.215444548513809
), three = c(0.319549563636166, -0.331820109901466, 
-0.522498726621558, -0.533583057293564, -1.07557447076409, -1.2439536016024, 
0.137327193404068, 0.824807312120951, 0.890772513135045, -0.699907273864071
), four = c(0.0242164205055869, 0.127580710965884, 
0.939940141796124, -0.95328238190511, -1.12539228910431, -0.57463577872874, 
0.0989572749173223, 0.581259982380773, 1.06395223195912, -1.30497643408927
), five = c(0.0672370094329536, 0.46800631599416, 
0.0336342629045394, -1.5260321604671, -0.108501672097744, -0.00598870144824637, 
-0.135182382987021, 0.697107825231465, 0.736189139343644, -0.822125529718815
), six = c(-0.6376725640001, 0.375979360195534, 
0.850685206672859, -1.7888882165231, -1.65086216108027, -0.224594156132765, 
0.106452755431465, 0.431408065353943, 1.02765650619415, -0.459408909488439
)), row.names = c(NA, 10L), class = "data.frame")

I applied t-test on each column comparing "cluster2" vs "cluster1"

tests <- lapply(seq(2, length(df)), function(x){t.test(df[,x]~df[,1])})

t-test results are in a list. So, I'm trying to extract all information into a dataframe.

v1 <- sapply(tests, function(x) x$p.value)
names(v1) < names(tests)

This gave logical(0).

Can anyone please tell how to save all the results into dataframe?


Solution

If you don't mind using an external package then:

library(matrixTests)
col_t_welch(df[df$group=="cluster2",-1], df[df$group=="cluster1",-1])

      obs.x obs.y obs.tot      mean.x     mean.y  mean.diff     var.x      var.y    stderr       df  statistic       pvalue   conf.low   conf.high alternative mean.null conf.level
One       7     3      10 -0.03035821 0.16533806 -0.1956963 0.4347748 0.01569194 0.2595021 6.906193 -0.7541221 0.4756968552 -0.8110149  0.41962235   two.sided         0       0.95
two       7     3      10 -0.06497898 0.03928572 -0.1042647 0.7347812 2.39802096 0.9509517 2.545136 -0.1096425 0.9207496910 -3.4608429  3.25231355   two.sided         0       0.95
three     7     3      10 -0.48970882 0.39769370 -0.8874025 0.3385390 0.63615343 0.5103076 2.964909 -1.7389561 0.1815091371 -2.5223572  0.74755220   two.sided         0       0.95
four      7     3      10 -0.52964750 0.86171745 -1.3913649 0.3785659 0.06283704 0.2739097 7.963842 -5.0796483 0.0009668828 -2.0235014 -0.75922852   two.sided         0       0.95
five      7     3      10 -0.29465530 0.48897708 -0.7836324 0.4417576 0.15588465 0.3392194 6.575237 -2.3101050 0.0565252579 -1.5963836  0.02911888   two.sided         0       0.95
six       7     3      10 -0.61128484 0.76991659 -1.3812014 0.6884335 0.09377073 0.3600063 7.996676 -3.8366033 0.0049749882 -2.2114376 -0.55096530   two.sided         0       0.95


Answered By - Karolis Koncevičius
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to save t-test output with names of columns into a dataframe in R?

 October 07, 2022     dataframe, lapply, r, statistics, t-test     No comments   

Issue

I have a dataframe df with following data:

df <- structure(list(group = c("cluster2", "cluster2", 
"cluster1", "cluster2", "cluster2", "cluster2", 
"cluster2", "cluster1", "cluster1", "cluster2"
), One = c(-0.614639315096381, 0.88834977627436, 
0.0832368160901144, 0.00321829065579383, -1.04180850739839, 0.0631077979797929, 
0.590396305169489, 0.103255370028437, 0.309521983720179, -0.101131803547097
), two = c(-0.431029772056812, 1.01576532724102, 
1.8217432927475, 0.369890996511319, -1.67807784856458, 0.470436956726829, 
0.0136060165886214, -0.975047520238315, -0.72883862189043, -0.215444548513809
), three = c(0.319549563636166, -0.331820109901466, 
-0.522498726621558, -0.533583057293564, -1.07557447076409, -1.2439536016024, 
0.137327193404068, 0.824807312120951, 0.890772513135045, -0.699907273864071
), four = c(0.0242164205055869, 0.127580710965884, 
0.939940141796124, -0.95328238190511, -1.12539228910431, -0.57463577872874, 
0.0989572749173223, 0.581259982380773, 1.06395223195912, -1.30497643408927
), five = c(0.0672370094329536, 0.46800631599416, 
0.0336342629045394, -1.5260321604671, -0.108501672097744, -0.00598870144824637, 
-0.135182382987021, 0.697107825231465, 0.736189139343644, -0.822125529718815
), six = c(-0.6376725640001, 0.375979360195534, 
0.850685206672859, -1.7888882165231, -1.65086216108027, -0.224594156132765, 
0.106452755431465, 0.431408065353943, 1.02765650619415, -0.459408909488439
)), row.names = c(NA, 10L), class = "data.frame")

I applied t-test on each column comparing "cluster2" vs "cluster1"

tests <- lapply(seq(2, length(df)), function(x){t.test(df[,x]~df[,1])})

t-test results are in a list. So, I'm trying to extract all information into a dataframe.

v1 <- sapply(tests, function(x) x$p.value)
names(v1) < names(tests)

This gave logical(0).

Can anyone please tell how to save all the results into dataframe?


Solution

If you don't mind using an external package then:

library(matrixTests)
col_t_welch(df[df$group=="cluster2",-1], df[df$group=="cluster1",-1])

      obs.x obs.y obs.tot      mean.x     mean.y  mean.diff     var.x      var.y    stderr       df  statistic       pvalue   conf.low   conf.high alternative mean.null conf.level
One       7     3      10 -0.03035821 0.16533806 -0.1956963 0.4347748 0.01569194 0.2595021 6.906193 -0.7541221 0.4756968552 -0.8110149  0.41962235   two.sided         0       0.95
two       7     3      10 -0.06497898 0.03928572 -0.1042647 0.7347812 2.39802096 0.9509517 2.545136 -0.1096425 0.9207496910 -3.4608429  3.25231355   two.sided         0       0.95
three     7     3      10 -0.48970882 0.39769370 -0.8874025 0.3385390 0.63615343 0.5103076 2.964909 -1.7389561 0.1815091371 -2.5223572  0.74755220   two.sided         0       0.95
four      7     3      10 -0.52964750 0.86171745 -1.3913649 0.3785659 0.06283704 0.2739097 7.963842 -5.0796483 0.0009668828 -2.0235014 -0.75922852   two.sided         0       0.95
five      7     3      10 -0.29465530 0.48897708 -0.7836324 0.4417576 0.15588465 0.3392194 6.575237 -2.3101050 0.0565252579 -1.5963836  0.02911888   two.sided         0       0.95
six       7     3      10 -0.61128484 0.76991659 -1.3812014 0.6884335 0.09377073 0.3600063 7.996676 -3.8366033 0.0049749882 -2.2114376 -0.55096530   two.sided         0       0.95


Answered By - Karolis Koncevičius
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to extract lower and upper bounds from confidence level in R from t test function?

 October 07, 2022     confidence-interval, r, statistics, t-test     No comments   

Issue

I used the following code to retrieve a confidence level for my data:

out <- t.test(my_data$my_col, conf.level = 0.95)
out

This returns something like:

data:  my_data$my_column
t = 30, df = 20, p-value < 2.1e-14
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 62.23191 80.11201
sample estimates:
mean of x 
 75.10457 

I've tried doing:

out[4][1]

But this returns:

$conf.int
[1]  62.23191 80.11201
attr(,"conf.level")
[1] 0.95

How do I specifically get the lower bound and upper bound from this respectively? (i.e. how do I extract 62.23191 and 80.11201 as variables?)


Solution

The output from t.test() is a list. The confidence interval is stored as a vector within the $conf.int list element.

To access the individual confidence intervals use out$conf.int[1] & out$conf.int[2]

Example:

out <- t.test(1:10, y=c(7:20)) 
out$conf.int
#[1] -11.052802  -4.947198
#attr(,"conf.level")
#[1] 0.95
out$conf.int[1]
#[1] -11.0528
out$conf.int[2]
#[1] -4.947198


Answered By - Dave2e
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, October 6, 2022

[FIXED] How to run a t-test on a subset of data

 October 06, 2022     r, statistics, subset, t-test     No comments   

Issue

First, is this dataset in a tidy form for a t-test?

https://i.stack.imgur.com/tMK6R.png

Second, I'm trying to do a two sample t-test to compare the means at time 3 of treatment a and b for 'outcome 1'. How would I go about doing this?

Sample data:

df <- structure(list(code = c(100, 100, 100, 101, 101, 101, 102, 102, 
      102, 103, 103, 103), treatment = c("a", "a", "a", "b", "b", "b", 
      "a", "a", "a", "b", "b", "b"), sex = c("f", "f", "f", "m", "m", 
      "m", "f", "f", "f", "f", "f", "f"), time = c(1, 2, 3, 1, 2, 3, 
      1, 2, 3, 1, 2, 3), `outcome 1` = c(21, 23, 33, 44, 45, 47, 22, 
      34, 22, 55, 45, 56), `outcome 2` = c(21, 32, 33, 33, 44, 45, 
      22, 57, 98, 65, 42, 42), `outcome 3` = c(62, 84, 63, 51, 45, 
      74, 85, 34, 96, 86, 45, 47)), .Names = c("code", "treatment", 
      "sex", "time", "outcome 1", "outcome 2", "outcome 3"), 
      class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))

Solution

First you'll have to define the subsets you want tested, then you can run the t-test. You don't have to necessarily store the subsets in variables as I've done, but it makes the t-test output clearer.

Normally with t-test questions, I'd recommend the help provided by ?t.test, but since this involves more complex subsetting, I've included how to do that here:

var_a <- df$`outcome 1`[df$treatment=="a" & df$time==3]
var_b <- df$`outcome 1`[df$treatment=="b" & df$time==3]

t.test(var_a,var_b)

Output:

    Welch Two Sample t-test

data:  var_a and var_b
t = -3.3773, df = 1.9245, p-value = 0.08182
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -55.754265   7.754265
sample estimates:
mean of x mean of y 
     27.5      51.5 


Answered By - www
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to exract specific values from the lists created by a statistical model (t-test)?

 October 06, 2022     list, r, statistics, t-test     No comments   

Issue

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

Solution

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.)



Answered By - Ben Bolker
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing