#| label: setup
#| include: false

library(knitr)
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    cache = TRUE,
    message = FALSE,
    warning = FALSE,
    echo = TRUE
)

Introduction

miaTime implements tools for time series manipulation based on the r BiocStyle::Biocpkg("TreeSummarizedExperiment") (TreeSE) data container. Much of the functionality is also applicable to the r BiocStyle::Biocpkg("SummarizedExperiment") data objects. This tutorial shows how to use miaTime methods as well as the broader R/Bioconductor ecosystem to manipulate time series data.

Check also the related package TimeSeriesExperiment.

Installation

miaTime is hosted on Bioconductor, and can be installed using via BiocManager.

#| label: install
#| eval: false

BiocManager::install("miaTime")

Once installed, miaTime is made available in the usual way.

#| label: load_package

library(miaTime)

Sorting samples

To sort data based on subject and time point in base R, you can use the order() function.

#| label: sort_samples

data(hitchip1006)
tse <- hitchip1006

index <- order(tse[["subject"]], tse[["time"]])
tse <- tse[ , index]

Storing time information with period class

miaTime utilizes the functions available in the package lubridate to convert time series field to period class object. This gives access to a number of readily available time series manipulation tools.

Load example data:

#| label: lubridate

# Load packages
library(lubridate)

# Time is given in days in the demo data.
# Convert days to seconds
time_in_seconds <- 60*60*24*tse[["time"]]
# Convert the time data to period class
seconds <- as.period(time_in_seconds, unit = "sec")
# Check the output
seconds |> tail()

Conversion between time units

The time field in days is now shown in seconds. It can then be converted to many different units using the lubridate package.

#| label: hours

hours <- as.period(seconds, unit = "hour")
hours |> tail()

The updated time information can then be added to the SummarizedExperiment data object as a new colData (sample data) field.

#| label: add_seconds

colData(tse)$time_sec <- seconds
colData(tse)

Calculating time differences

The lubridate::as.duration() function helps to specify time points as duration.

#| label: duration

duration <- as.duration(seconds)
duration |> tail()

The difference between subsequent time points can then be calculated.

#| label: time_diff

time_diff <- diff(duration)
time_diff <- c(NA, time_diff)
time_diff |> tail()

The time difference from a selected point to the other time points can be calculated as follows.

#| label: time_diff2

# Difference from second time point
time_diff <- hours - sort(unique(hours))[[2]]
time_diff |> tail()

Time point rank

Rank of the time points can be calculated by rank function provided in base R.

#| label: rank

tse[["time"]] <- rank(tse[["time"]])
colData(tse)

Operations per unit

Sometimes we need to operate on time series per unit (subject, reaction chamber, sampling location, ...).

Add time point rank per subject.

#| label: rank_for_subject

library(dplyr)

colData(tse) <- colData(tse) |>
   as.data.frame() |>
   group_by(subject) |>
   mutate(rank = rank(time, ties.method = "average")) |>
   DataFrame()

Subset to baseline samples

TreeSE consists of rows for features and columns for samples. If we are specifically interested in baseline samples, we can easily subset the data as follows.

#| label: subset

tse <- tse[, tse$time==0]

Session info

#| label: session_info

sessionInfo()


microbiome/miaTime documentation built on April 17, 2025, 7:35 p.m.