Skip to contents

Calculate confidence intervals of mean or difference between means in a data.frame

Usage

# S3 method for data.frame
meanCI(x, ...)

meanCI_sub(data = data, x, y, group, paired = FALSE, idx = NULL, ...)

Arguments

x

Name of a categorical or numeric column. If !missing(y), name of continuous variable

...

Further arguments to be passed to meanCI

data

A data.frame

y

Name of a numeric column

group

Name of categorical column

paired

logical

idx

A vector containing factors or strings in the x columns. These must be quoted (ie. surrounded by quotation marks). The first element will be the control group, so all differences will be computed for every other group and this first group.

Value

An object of class "meanCI" which is a list containing at least the following components:

data

A tibble containing raw data or a list of numeric vector

result

A data.frame consists of summary statistics

call

the matched call

attr(*,"measure")

character. One of c("mean","unpaired","paired")

Methods (by generic)

  • meanCI: S3 method for data.frame

Examples

meanCI(acs,age)
#> 
#> call: meanCI.data.frame(x = acs, age) 
#> method: One sample t-test 
#> alternative hypothesis:
#>    true mean  is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 7
#>   m        se      DF    lower    upper    t      p        
#>   <chr>    <chr>   <chr> <chr>    <chr>    <chr>  <chr>    
#> 1 63.31155 0.39954 856   62.52736 64.09574 158.46 < 2.2e-16
meanCI(acs,sex,age)
#> 
#> call: meanCI.data.frame(x = acs, sex, age) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 6
#>   control test  DF     CI                     t      p        
#>   <chr>   <chr> <chr>  <chr>                  <chr>  <chr>    
#> 1 Female  Male  596.99 8.07 [95CI 6.52; 9.62] 10.222 < 2.2e-16
meanCI(acs,Dx,age)
#> 
#> call: meanCI.data.frame(x = acs, Dx, age) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 2 × 6
#>   control test            DF     CI                      t       p      
#>   <chr>   <chr>           <chr>  <chr>                   <chr>   <chr>  
#> 1 NSTEMI  STEMI           300.48 2.22 [95CI -0.17; 4.62] 1.82808 0.06853
#> 2 NSTEMI  Unstable Angina 250.10 0.48 [95CI -1.76; 2.73] 0.42479 0.67135
acs %>% select(age) %>% meanCI()
#> 
#> call: meanCI.data.frame(x = .) 
#> method: One sample t-test 
#> alternative hypothesis:
#>    true mean  is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 7
#>   m        se      DF    lower    upper    t      p        
#>   <chr>    <chr>   <chr> <chr>    <chr>    <chr>  <chr>    
#> 1 63.31155 0.39954 856   62.52736 64.09574 158.46 < 2.2e-16
acs %>% select(sex,age) %>% meanCI()
#> 
#> call: meanCI.data.frame(x = .) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 6
#>   control test  DF     CI                     t      p        
#>   <chr>   <chr> <chr>  <chr>                  <chr>  <chr>    
#> 1 Female  Male  596.99 8.07 [95CI 6.52; 9.62] 10.222 < 2.2e-16
meanCI(iris,Species,Sepal.Length)
#> 
#> call: meanCI.data.frame(x = iris, Species, Sepal.Length) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 2 × 6
#>   control test       DF     CI                        t       p        
#>   <chr>   <chr>      <chr>  <chr>                     <chr>   <chr>    
#> 1 setosa  versicolor 86.538 -0.93 [95CI -1.11; -0.75] -10.521 < 2.2e-16
#> 2 setosa  virginica  76.516 -1.58 [95CI -1.79; -1.38] -15.386 < 2.2e-16
meanCI(iris,Sepal.Width,Sepal.Length,paired=TRUE)
#> 
#> call: meanCI.data.frame(x = iris, Sepal.Width, Sepal.Length, paired = TRUE) 
#> method: Paired t-test 
#> alternative hypothesis:
#>    true paired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 6
#>   control     test         DF    CI                        t       p        
#>   <chr>       <chr>        <chr> <chr>                     <chr>   <chr>    
#> 1 Sepal.Width Sepal.Length 149   -2.79 [95CI -2.94; -2.63] -34.815 < 2.2e-16
meanCI(iris,Sepal.Length,Sepal.Width)
#> 
#> call: meanCI.data.frame(x = iris, Sepal.Length, Sepal.Width) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 6
#>   control      test        DF     CI                     t      p        
#>   <chr>        <chr>       <chr>  <chr>                  <chr>  <chr>    
#> 1 Sepal.Length Sepal.Width 225.68 2.79 [95CI 2.64; 2.94] 36.463 < 2.2e-16
iris %>% select(starts_with("Petal")) %>% meanCI(paired=TRUE)
#> 
#> call: meanCI.data.frame(x = ., paired = TRUE) 
#> method: Paired t-test 
#> alternative hypothesis:
#>    true paired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 1 × 6
#>   control      test        DF    CI                     t      p        
#>   <chr>        <chr>       <chr> <chr>                  <chr>  <chr>    
#> 1 Petal.Length Petal.Width 149   2.56 [95CI 2.39; 2.73] 29.797 < 2.2e-16
iris %>% meanCI(paired=TRUE)
#> 
#> call: meanCI.data.frame(x = ., paired = TRUE) 
#> method: Paired t-test 
#> alternative hypothesis:
#>    true paired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 3 × 6
#>   control      test         DF    CI                     t         p        
#>   <chr>        <chr>        <chr> <chr>                  <chr>     <chr>    
#> 1 Sepal.Length Sepal.Width  149   2.79 [95CI 2.63; 2.94] " 34.815" < 2.2e-16
#> 2 Sepal.Length Petal.Length 149   2.09 [95CI 1.90; 2.27] " 22.813" < 2.2e-16
#> 3 Sepal.Length Petal.Width  149   4.64 [95CI 4.57; 4.72] "117.539" < 2.2e-16
meanCI(acs,sex,age,Dx,mu=10)
#> 
#> call: meanCI.data.frame(x = acs, sex, age, Dx, mu = 10) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  10 
#> 
#> Results
#> # A tibble: 3 × 6
#>   control                test                 DF        CI           t     p    
#>   <chr>                  <chr>                <chr>     <chr>        <chr> <chr>
#> 1 NSTEMI.Female          NSTEMI.Male          " 98.798" 9.73 [95CI … -0.1… 0.89…
#> 2 STEMI.Female           STEMI.Male           "168.691" 9.68 [95CI … -0.2… 0.81…
#> 3 Unstable Angina.Female Unstable Angina.Male "319.818" 6.28 [95CI … -3.4… 0.00…
acs %>% select(sex,TC,TG,HDLC) %>% meanCI(group=sex)
#> 
#> call: meanCI.data.frame(x = ., group = sex) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 4 × 6
#>   control   test        DF     CI                           t      p        
#>   <chr>     <chr>       <chr>  <chr>                        <chr>  <chr>    
#> 1 Female.TC Female.TG   488.52 69.01 [95CI 58.22; 79.80]    12.564 < 2.2e-16
#> 2 Female.TC Female.HDLC 303.67 149.92 [95CI 143.72; 156.11] 47.617 < 2.2e-16
#> 3 Male.TC   Male.TG     800.95 55.46 [95CI 46.54; 64.37]    12.209 < 2.2e-16
#> 4 Male.TC   Male.HDLC   618.20 145.50 [95CI 141.57; 149.43] 72.724 < 2.2e-16
acs %>% select(sex,TC,TG,HDLC) %>% meanCI(sex)
#> 
#> call: meanCI.data.frame(x = ., sex) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 3 × 6
#>   control     test      DF     CI                        t         p     
#>   <chr>       <chr>     <chr>  <chr>                     <chr>     <chr> 
#> 1 TC.Female   TC.Male   501.99 5.61 [95CI -1.54; 12.75]  " 1.5417" 0.1238
#> 2 TG.Female   TG.Male   690.02 -7.95 [95CI -19.98; 4.09] "-1.2961" 0.1954
#> 3 HDLC.Female HDLC.Male 526.44 1.19 [95CI -0.44; 2.82]   " 1.4341" 0.1521
iris %>% select(Species,starts_with("Sepal")) %>% meanCI(Species)
#> 
#> call: meanCI.data.frame(x = ., Species) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 4 × 6
#>   control             test                    DF     CI              t     p    
#>   <chr>               <chr>                   <chr>  <chr>           <chr> <chr>
#> 1 Sepal.Length.setosa Sepal.Length.versicolor 86.538 -0.93 [95CI -1… "-10… < 2.…
#> 2 Sepal.Length.setosa Sepal.Length.virginica  76.516 -1.58 [95CI -1… "-15… < 2.…
#> 3 Sepal.Width.setosa  Sepal.Width.versicolor  94.698 0.66 [95CI 0.5… "  9… 2.48…
#> 4 Sepal.Width.setosa  Sepal.Width.virginica   95.547 0.45 [95CI 0.3… "  6… 4.57…
iris %>% select(Species,starts_with("Sepal")) %>% meanCI(group=Species)
#> 
#> call: meanCI.data.frame(x = ., group = Species) 
#> method: Welch Two Sample t-test 
#> alternative hypothesis:
#>    true unpaired differences in means is not equal to  0 
#> 
#> Results
#> # A tibble: 3 × 6
#>   control                 test                   DF     CI           t     p    
#>   <chr>                   <chr>                  <chr>  <chr>        <chr> <chr>
#> 1 setosa.Sepal.Length     setosa.Sepal.Width     97.487 1.58 [95CI … 21.5… < 2.…
#> 2 versicolor.Sepal.Length versicolor.Sepal.Width 80.867 3.17 [95CI … 37.0… < 2.…
#> 3 virginica.Sepal.Length  virginica.Sepal.Width  72.643 3.61 [95CI … 35.8… < 2.…