Get started

  collapse = TRUE,
  comment = "#>"
options(rmarkdown.html_vignette.check_title = FALSE)

The goal of this package is to easily apply the same t-tests/basic data description across several sub-groups, with the output provided as a nice arranged data.frame. Multiple comparison and $p$-value significance symbols are also provided.

This kind of analysis is commonly seen in ROI (Region-of-interest) analysis of brain imaging data (hence the name of the package, roistats).


Get some basic description about the data

The package comes with a data.frame called color_index, which is similar in format to what we might get after cleaning and wrangling ROI data. This data.frame contains the neural analysis result of the degree of color memory sensitivity at each brain region of each subject. The color_index data frame has three columns:


Before we dive into the statistical test, we likely want to get descriptive statistics for color_index, including the mean, standard deviation, and standard error of the mean for each brain region. The df_sem function help us with this, and is designed to be used in combination with dplyr, specifically dplyr::group_by(). To obtain our descriptive statistics by roi_id, we just first group the data frame by roi_id, then pass the data frame to def_sem().


color_index %>%
  group_by(roi_id) %>% # The column to get summaries by
  df_sem(color_index) # The column to summarize

Yay! We have obtained the SEM (which is commonly used for error bar plotting in psych and cog neuro area) for each sub-group easily.

Note that if we do not use dplyr::group_by() we just get the overall summaries. The package reports a warning when this happens because it generally goes against the intent of the package (computing multiple comparisons).

df_sem(data = color_index, x = color_index)

One-sample t-tests for all sub-groups

Now suppose we want to test whether color_index is significantly different (i.e., significantly different than zero) for each possible sub-group (roi_id). Note that we are not computing pairwise comparisons yet, just whether the mean for each subgroup is different from zero. Here, we have eight sub-groups, which means we will get eight one-sample t-test results in total. As a first step in the analysis we probably don't care much about all the detailed output from stats::t.test. Instead, we're just looking for mean difference and significance. This is what the t_test_one_sample function was designed to accommodate. We again pass the function a grouped data frame, and we get $t$-test results back for each group.

By default, a Bonferroni $p$-value adjustment is applied, but any adjustment available through stats::p.adjust() can be supplied. Similarly, each mean is compared to zero by default, but this can be adjusted through the optional mu argument. The interface for the function works essentially equivalanetly to df_sem().

color_index %>% 
  group_by(roi_id) %>% 

Here, we see the t-values, degrees of freedom, as well as the uncorrected and bonferroni corrected $p$-values! Nice! Note that the multiple comparison corrected $p$-values are provided by the p_bonferroni column, but the name of this column will change depending on the method you want to use. Let's try again, but this time using both the Bonferroni and the Benjamini and Hochberg (1995) method. We'll put this in a nice table as well.

color_index_one_sample_t_res <- color_index %>%
  group_by(roi_id) %>% 
  t_test_one_sample(color_index, p_adjust = c("bonferroni", "BH"))

knitr::kable(color_index_one_sample_t_res, digits = 3)

As before, if we our data frame is not grouped, we'll get the stats returned, but with a warning.

color_index %>% 

Significance symbols for an even clearer table and possible visulization

Usually, we want the significance symbol to highlight the result in a table or plot. Here we have the p_range function to create the significance symbol:

color_index_one_sample_t_with_sig <- color_index_one_sample_t_res %>% 
  mutate(sig_origin_p = p_range(p))

knitr::kable(color_index_one_sample_t_with_sig, digits = 3)

You can use p_range for a single number too:


Two-sample t-tests for all sub-groups

The t_test_two_sample function is used for applying two-sample t-tests to all sub-groups. This dataset has all the same columns as color_index, but also includes a group column specifying the condition of the experiement (paired versus control).


We can obtain paired t-test for each sub-group using a similar approach as before. Here we're including the additional paired = TRUE argument to specify we want a paired $t$-test. This argument defaults to FALSE.

color_index_two_sample %>% 
  group_by(roi_id) %>% 
  t_test_two_sample(color_effect, group, paired = TRUE)

Try the roistats package in your browser

Any scripts or data that you put into this service are public.

roistats documentation built on March 11, 2021, 1:07 a.m.