### Install TSEntropies package Source: https://context7.com/cran/tsentropies/llms.txt Standard command to install the package from CRAN. ```R install.packages("TSEntropies") ``` -------------------------------- ### Calculate Fast Sample Entropy in Pure R Source: https://context7.com/cran/tsentropies/llms.txt Pure R implementation of the fast sample entropy algorithm, ensuring identical results to the C version without requiring compilation. ```r library(TSEntropies) # Test dataset set.seed(321) timser <- rnorm(1000) # Compare R and C implementations fast_sampen_r <- FastSampEn_R(timser) fast_sampen_c <- FastSampEn_C(timser) print(paste("FastSampEn (R):", round(fast_sampen_r, 4))) print(paste("FastSampEn (C):", round(fast_sampen_c, 4))) # Both produce identical results ``` -------------------------------- ### Calculate Fast Approximate Entropy in Pure R Source: https://context7.com/cran/tsentropies/llms.txt Provides a pure R implementation of the fast approximate entropy algorithm for debugging or reference. ```r library(TSEntropies) # Moderate-sized dataset for R implementation set.seed(789) timser <- rnorm(1000) # Basic usage fast_apen_r <- FastApEn_R(timser) print(paste("FastApEn (R):", round(fast_apen_r, 4))) # Output: "FastApEn (R): 3.1456" # Verify consistency with C implementation fast_apen_c <- FastApEn_C(timser) print(paste("FastApEn (C):", round(fast_apen_c, 4))) # Output: "FastApEn (C): 3.1456" # Downsampling with lag parameter fast_apen_lag <- FastApEn_R(timser, dim = 2, lag = 2, r = 0.15 * sd(timser)) print(paste("FastApEn (lag=2):", round(fast_apen_lag, 4))) ``` -------------------------------- ### Calculate entropy with full parameters Source: https://context7.com/cran/tsentropies/llms.txt Applies the FastSampEn_R function with explicit dimension, lag, and tolerance parameters. ```R entropy_full <- FastSampEn_R(timser, dim = 2, lag = 1, r = 0.15 * sd(timser)) print(paste("FastSampEn full params:", round(entropy_full, 4))) ``` -------------------------------- ### Calculate Approximate Entropy using Pure R Source: https://context7.com/cran/tsentropies/llms.txt Uses the pure R implementation of ApEn, which is useful for algorithm study or environments where C compilation is not supported. ```r library(TSEntropies) # Generate test data set.seed(123) timser <- rnorm(500) # Smaller dataset for R implementation # Basic computation entropy_r <- ApEn_R(timser) print(paste("ApEn (R implementation):", round(entropy_r, 4))) # Output: "ApEn (R implementation): 2.1287" # Verify C and R implementations produce same results entropy_c <- ApEn_C(timser) print(paste("ApEn (C implementation):", round(entropy_c, 4))) # Output: "ApEn (C implementation): 2.1287" # Custom parameters entropy_custom <- ApEn_R(timser, dim = 3, lag = 2, r = 0.15 * sd(timser)) print(paste("ApEn custom params:", round(entropy_custom, 4))) # Output: "ApEn custom params: 2.4561" ``` -------------------------------- ### Calculate Sample Entropy with SampEn_R Source: https://context7.com/cran/tsentropies/llms.txt Computes sample entropy using a pure R implementation. Returns NA for constant signals where no matches are found. ```r library(TSEntropies) # Small dataset for R implementation set.seed(456) timser <- rnorm(500) # Compare implementations sampen_r <- SampEn_R(timser) sampen_c <- SampEn_C(timser) print(paste("SampEn (R):", round(sampen_r, 4))) print(paste("SampEn (C):", round(sampen_c, 4))) # Output: Both produce identical results # Handle edge cases - returns NA if no matches found constant_signal <- rep(1, 500) result <- SampEn_R(constant_signal) print(paste("Constant signal SampEn:", result)) # Output: "Constant signal SampEn: NA" ``` -------------------------------- ### Compute Fast Sample Entropy Source: https://context7.com/cran/tsentropies/llms.txt Delivers high-speed sample entropy calculations using clustering algorithms. Ideal for batch processing and trend detection in large time series. ```r library(TSEntropies) # Large dataset analysis set.seed(42) large_ts <- rnorm(10000) # Basic fast sample entropy fast_sampen <- FastSampEn(large_ts) print(paste("Fast SampEn:", round(fast_sampen, 4))) # Output: "Fast SampEn: 2.9876" # Custom tolerance fast_sampen_strict <- FastSampEn(large_ts, r = 0.1 * sd(large_ts)) print(paste("Fast SampEn (r=0.1*sd):", round(fast_sampen_strict, 4))) # Output: "Fast SampEn (r=0.1*sd): 3.4521" # Batch processing multiple signals analyze_complexity <- function(signals) { results <- sapply(signals, function(s) FastSampEn(s)) return(results) } # Analyze multiple time series efficiently batch_signals <- lapply(1:10, function(i) rnorm(5000)) entropies <- analyze_complexity(batch_signals) print(paste("Mean FastSampEn:", round(mean(entropies), 4))) print(paste("SD FastSampEn:", round(sd(entropies), 4))) # Time series trend detection ts_length <- 5000 increasing_complexity <- lapply(1:5, function(i) { sin(seq(0, 20*pi, length.out = ts_length)) + rnorm(ts_length, 0, 0.1 * i) }) trend <- sapply(increasing_complexity, FastSampEn) print("Entropy trend with increasing noise:") print(round(trend, 4)) # Output shows increasing entropy values ``` -------------------------------- ### Handle constant signal edge case Source: https://context7.com/cran/tsentropies/llms.txt Demonstrates that the function returns NA when the input signal is constant, resulting in a zero denominator. ```R constant <- rep(5, 1000) result <- FastSampEn_R(constant) print(paste("Constant signal:", result)) ``` -------------------------------- ### Calculate Approximate Entropy in R Source: https://context7.com/cran/tsentropies/llms.txt Computes approximate entropy using the default C-optimized implementation. Adjust parameters like embedding dimension and tolerance radius to analyze signal complexity. ```r library(TSEntropies) # Generate sample time series data set.seed(42) timser <- rnorm(2000) # Basic usage with default parameters (dim=2, lag=1, r=0.2*sd) entropy_default <- ApEn(timser) print(paste("Approximate Entropy (default):", round(entropy_default, 4))) # Output: "Approximate Entropy (default): 2.1534" # Custom tolerance radius (more strict matching) entropy_strict <- ApEn(timser, r = 0.1 * sd(timser)) print(paste("Approximate Entropy (r=0.1*sd):", round(entropy_strict, 4))) # Output: "Approximate Entropy (r=0.1*sd): 2.7821" # Higher embedding dimension for more complex pattern detection entropy_dim3 <- ApEn(timser, dim = 3, r = 0.1 * sd(timser)) print(paste("Approximate Entropy (dim=3):", round(entropy_dim3, 4))) # Output: "Approximate Entropy (dim=3): 2.6543" # Compare regular vs chaotic signals regular_signal <- sin(seq(0, 20*pi, length.out = 2000)) chaotic_signal <- rnorm(2000) print(paste("Regular signal ApEn:", round(ApEn(regular_signal), 4))) # Output: "Regular signal ApEn: 0.0891" print(paste("Chaotic signal ApEn:", round(ApEn(chaotic_signal), 4))) # Output: "Chaotic signal ApEn: 2.1534" ``` -------------------------------- ### Compute Fast Approximate Entropy Source: https://context7.com/cran/tsentropies/llms.txt Uses an optimized clustering-based algorithm for faster computation on large datasets. Suitable for comparative analysis where entropy trends are more important than absolute values. ```r library(TSEntropies) # Large time series for performance comparison set.seed(42) large_ts <- rnorm(10000) # Fast approximate entropy (default r=0.15*sd) fast_apen <- FastApEn(large_ts) print(paste("Fast ApEn:", round(fast_apen, 4))) # Output: "Fast ApEn: 3.2145" # Custom parameters fast_apen_custom <- FastApEn(large_ts, dim = 3, r = 0.1 * sd(large_ts)) print(paste("Fast ApEn (dim=3):", round(fast_apen_custom, 4))) # Output: "Fast ApEn (dim=3): 3.8921" # Performance comparison small_ts <- rnorm(2000) system.time(ApEn(small_ts)) # Standard: ~2.5 seconds system.time(FastApEn(small_ts)) # Fast: ~0.003 seconds # Trend analysis across signals with varying complexity signals <- list( regular = sin(seq(0, 40*pi, length.out = 5000)), moderate = sin(seq(0, 40*pi, length.out = 5000)) + rnorm(5000, 0, 0.3), chaotic = rnorm(5000) ) for (name in names(signals)) { entropy <- FastApEn(signals[[name]]) print(paste(name, "FastApEn:", round(entropy, 4))) } # Output shows increasing entropy from regular to chaotic ``` -------------------------------- ### Calculate Sample Entropy in R Source: https://context7.com/cran/tsentropies/llms.txt Computes sample entropy to reduce bias by excluding self-matches. This method is generally preferred for shorter time series data. ```r library(TSEntropies) # Generate physiological-like signal set.seed(42) heart_rate <- 70 + cumsum(rnorm(2000, 0, 0.5)) # Basic sample entropy calculation sampen_default <- SampEn(heart_rate) print(paste("Sample Entropy:", round(sampen_default, 4))) # Output: "Sample Entropy: 1.8234" # Stricter tolerance for fine-grained complexity sampen_strict <- SampEn(heart_rate, r = 0.1 * sd(heart_rate)) print(paste("SampEn (r=0.1*sd):", round(sampen_strict, 4))) # Output: "SampEn (r=0.1*sd): 2.3156" # Higher dimension analysis sampen_dim3 <- SampEn(heart_rate, dim = 3, r = 0.1 * sd(heart_rate)) print(paste("SampEn (dim=3):", round(sampen_dim3, 4))) # Output: "SampEn (dim=3): 2.1892" # Compare ApEn vs SampEn on same data timser <- rnorm(2000) print(paste("ApEn:", round(ApEn(timser), 4))) print(paste("SampEn:", round(SampEn(timser), 4))) # ApEn includes self-matches, SampEn excludes them ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.