evaluate_lagged_cor: Evaluate Lagged Correlation and Fit Peaks

View source: R/9-evaluate_lagged_cor.R

evaluate_lagged_corR Documentation

Evaluate Lagged Correlation and Fit Peaks

Description

This function takes a lagged correlation object, fits a loess curve to the correlation data, normalizes the data to ensure all correlations are on the same side of zero, fits peaks to the normalized correlation data, computes a score based on the Spearman correlation between the fitted peaks and the actual data, and optionally generates a plot.

Usage

evaluate_lagged_cor(object, plot = TRUE)

Arguments

object

An object of class lagged_cor_result which contains the fields all_cor for correlations and shift_time for the corresponding time shifts.

plot

Logical; if TRUE, generates a plot of the actual versus fitted correlation data. Defaults to TRUE.

Details

The function starts by checking if the input object is NULL. If it is, it returns a score of 0 and NULL for the plot. If the object is not NULL, the function proceeds to clean and prepare the shift times using stringr and purrr. Then, it fits a loess model to the correlation data and uses this to normalize the data and identify peaks using a custom fitpeaks function. It calculates the score of the fit using Spearman's method. The peak with the highest absolute correlation determines the score, and the function distinguishes between positive and negative correlations. The function uses ggplot2 for plotting if required.

Value

A list containing the following components:

  • score: A numeric value representing the Spearman correlation score of the fit, where higher values indicate a better fit.

  • plot: A ggplot object showing the actual versus fitted correlation data if plot is TRUE, otherwise NULL.

Note

This function depends on several external packages (ggplot2, dplyr, stringr, purrr) as well as a custom fitpeaks function that must be defined elsewhere in the user's environment.

Examples

data("object", package = "laggedcor")
result = 
  evaluate_lagged_cor(object = object, plot = TRUE)

result$score
result$plot

jaspershen/laggedcor documentation built on Nov. 9, 2023, 3:44 p.m.