Learn R Programming

NeuroDecodeR: Neural Decoding in R

Overview

Neural decoding is a data analysis method that uses pattern classifiers to predict experimental conditions based on neural activity. The NeuroDecodeR package makes it easy to do neural decoding analyses in R.

Installation

You can install NeuroDecodeR package from CRAN of the development version on GitHub.

# Current release on CRAN:
install.packages("NeuroDecodeR")


# Development version on GitHub:
# install.packages("devtools")
devtools::install_github("emeyers/NeuroDecodeR")

Documentation

The documentation for this package is available at: https://emeyers.github.io/NeuroDecodeR/

To get started we recommend you read the introductory tutorial

Usage

The package is based on 5 abstract object types:

  1. Datasources (DS): generate training and test sets.
  2. Feature preprocessors (FP): apply preprocessing to the training and test sets.
  3. Classifiers (CL): learn relationships on the training set and make predictions on the test data.
  4. Result Metrics (RM): summarize the prediction accuracies.
  5. Cross-validators (CV): take the DS, FP and CL objects and run a cross-validation decoding procedure.

By combing different versions of these 5 object types together, it is possible to run a range of different decoding analyses.

Below is a brief illustration of how to use the NDR to do a simple decoding analysis. To learn how to use the NDR please see the documentation website and the package vignettes.

library(NeuroDecodeR)

# file to data in "binned format"
basedir_file_name <- system.file(file.path("extdata", "ZD_150bins_50sampled.Rda"), 
                                 package="NeuroDecodeR")

# create the DS, FP, CL, RM, and CV objects
ds <- ds_basic(basedir_file_name, 'stimulus_ID', 5, num_label_repeats_per_cv_split = 3)
#> Automatically selecting sites_IDs_to_use. Since num_cv_splits = 5 and num_label_repeats_per_cv_split = 3, all sites that have 15 repetitions have been selected. This yields 132 sites that will be used for decoding (out of 132 total).

fps <- list(fp_zscore())
cl <- cl_max_correlation()
rms <- list(rm_main_results(), rm_confusion_matrix())
cv <- cv_standard(datasource = ds, 
                  classifier = cl, 
                  feature_preprocessors = fps, 
                  result_metrics = rms, 
                  num_resample_runs = 2)  # better to use more resample runs for actual analyses (default is 50)

# run a decoding analysis (this takes a few minutes) 
DECODING_RESULTS <- run_decoding(cv)
#>   |                                                                              |                                                                      |   0%  |                                                                              |===================================                                   |  50%  |                                                                              |======================================================================| 100%
# plot the results for three different result types
plot(DECODING_RESULTS$rm_main_results, results_to_show = 'all', type = 'line')
# create a temporal cross decoding plot
plot(DECODING_RESULTS$rm_main_results)

Running an analysis using pipes (|>)

One can also run a decoding analysis using the pipe (|>) operator to string together the different NDR objects as shown below.


basedir_file_name <- system.file(file.path("extdata", "ZD_500bins_500sampled.Rda"), package="NeuroDecodeR")
  
  DECODING_RESULTS <- basedir_file_name |>
    ds_basic('stimulus_ID', 6, num_label_repeats_per_cv_split = 3) |>
    cl_max_correlation() |>
    fp_zscore() |>
    rm_main_results() |>
    rm_confusion_matrix() |>
    cv_standard(num_resample_runs = 2) |>
    run_decoding()
#> Automatically selecting sites_IDs_to_use. Since num_cv_splits = 6 and num_label_repeats_per_cv_split = 3, all sites that have 18 repetitions have been selected. This yields 132 sites that will be used for decoding (out of 132 total).
#>   |                                                                              |                                                                      |   0%  |                                                                              |===================================                                   |  50%  |                                                                              |======================================================================| 100%
  
  plot(DECODING_RESULTS$rm_confusion_matrix)

Copy Link

Version

Install

install.packages('NeuroDecodeR')

Monthly Downloads

137

Version

0.2.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Ethan Meyers

Last Published

March 15th, 2024

Functions in NeuroDecodeR (0.2.0)

get_data.ds_generalization

A datasource (DS) method to generate training and test sets
fp_select_k_features

A feature preprocessor (FP) that reduces data to the k most selective features
get_parameters

Get parameters of an NeuroDecodeR object
get_parameters.cv_standard

Get parameters of an NeuroDecodeR object
get_parameters.cl_max_correlation

Get parameters of an NeuroDecodeR object
get_parameters.ds_basic

Get parameters of an NeuroDecodeR object
get_num_label_repetitions

Get the number of sites have at least k trials of each label level
get_num_label_repetitions_each_site

Get the number of trial repetitions for a given label for each site
get_predictions.cl_svm

A classifier (CL) method to train the CL and return predictions
get_siteIDs_with_k_label_repetitions

Get the sitesIDs that have at least k trials for all label level
log_check_results_already_exist

A function that checks if a decoding analysis has already been run
fp_zscore

A feature preprocessor (FP) that z-score normalizes the data
get_parameters.rm_main_results

Get parameters of an NeuroDecodeR object
get_predictions

A classifier (CL) method to train the CL and return predictions
get_data

A datasource (DS) method to generate training and test sets
create_binned_data

Convert data from raster format to binned format
get_parameters.cl_poisson_naive_bayes

Get parameters of an NeuroDecodeR object
aggregate_resample_run_results.rm_confusion_matrix

A result metric (RM) method to aggregate results over resample runs
get_parameters.ds_generalization

Get parameters of an NeuroDecodeR object
preprocess_data.fp_select_k_features

A feature-preprocessor (FP) method to pre-process training and test data
get_parameters.fp_select_k_features

Get parameters of an NeuroDecodeR object
preprocess_data.fp_zscore

A feature-preprocessor (FP) method to pre-process training and test data
plot.label_repetition

A plot function for label_repetition object
get_parameters.cl_svm

Get parameters of an NeuroDecodeR object
run_decoding

A cross-validator (CV) method to run a decoding analysis
test_valid_binned_format

Tests if a data frame is in valid binned format
plot.raster_data

A plot function for data in raster format
cv_standard

The standard cross-validator (CV)
log_load_results_from_result_name

A function that loads DECODING_RESULTS based on the result_name
get_predictions.cl_max_correlation

A classifier (CL) method to train the CL and return predictions
log_save_results

Saves the DECODING_RESULTS and logs the parameters used in the analysis
ds_basic

A basic datasource (DS)
rm_confusion_matrix

A result metric (RM) that calculates confusion matrices
get_parameters.fp_zscore

Get parameters of an NeuroDecodeR object
rm_main_results

A result metric (RM) that calculates main decoding accuracy measures
test_valid_ndr_object

Tests if an object is a valid NDR object
get_parameters.rm_confusion_matrix

Get parameters of an NeuroDecodeR object
run_decoding.cv_standard

A cross-validator (CV) method to run a decoding analysis
log_load_results_from_params

A function that loads DECODING_RESULTS based on decoding_parameters
preprocess_data

A feature-preprocessor (FP) method to pre-process training and test data
read_matlab_raster_data

Reads MATLAB raster data
get_predictions.cl_poisson_naive_bayes

A classifier (CL) method to train the CL and return predictions
plot_main_results

A plot function to plot multiple rm_main_results
read_raster_data

Read a csv, rda, rds or mat file in raster format
plot.rm_confusion_matrix

A plot function for the rm_confusion_matrix object
plot.rm_main_results

A plot function for the rm_main_results object
test_valid_raster_format

Tests if a data frame is in valid raster format
aggregate_resample_run_results.rm_main_results

A result metric (RM) method to aggregate results over resample runs
cl_poisson_naive_bayes

A Poisson Naive Bayes classifier (CL)
cl_svm

A support vector machine classifier (CL)
cl_max_correlation

A maximum correlation coefficient classifier (CL)
convert_matlab_raster_data

Convert raster data in MATLAB to R
aggregate_CV_split_results.rm_main_results

A result metric (RM) method to aggregate results over cross-validation splits
aggregate_CV_split_results

A result metric (RM) method to aggregate results over cross-validation splits
aggregate_resample_run_results

A result metric (RM) method to aggregate results over resample runs
NeuroDecodeR

NeuroDecodeR: A package for neural decoding analyses
aggregate_CV_split_results.rm_confusion_matrix

A result metric (RM) method to aggregate results over cross-validation splits
get_data.ds_basic

A datasource (DS) method to generate training and test sets
ds_generalization

A datasource (DS) that allows training and testing on different but related labels