rfunctiontime-seriestidyversewavelet-transform

How do you use map() to apply function to data frame, when function calls for specific column input in R?


My goal is to apply wavelet analysis and image construction to large data set of time series data to be eventually used in pipeline for time series clustering. The function to do the first step is from WaveletComp and I am using purr map () from Tidyverse package. Ideally the output is a list labeled for each column that I can then apply other functions to in the pipeline.

library(WaveletComp)

The data set has 3 columns and 6000 values

df <- data.frame(replicate(3,sample(-5:5,6000,rep=TRUE)))

wave_emg <- function(df) {
  analyze.wavelet(my.data = df, my.series = "X1", loess.span =50,
                        dt=1, dj=1/250,
                        lowerPeriod = 32,
                        upperPeriod = 512,
                        make.pval = TRUE, n.sim = 100)

Solution <- mutate(model = map(df, wave_emg))

I get the following error *Error in my.data[, ind] : incorrect number of dimensions

It appears to me that the my.series command in the analyze.wavelet function is looking for a single column to be specified. Is there a way to inform the command to take the next column successively?


Solution

  • You could write a function which takes two input, dataframe and column name/position.

    library(WaveletComp)
    library(purrr)
    
    ave_emg <- function(df, col) {
      analyze.wavelet(my.data = df, my.series = col, loess.span =50,
                      dt=1, dj=1/250,
                      lowerPeriod = 32,
                      upperPeriod = 512,
                      make.pval = TRUE, n.sim = 100)
    }
    

    analyze.wavelet function takes column names or column index as input so you could use any of these versions :

    #column names
    result <- map(names(df), ave_emg, df = df)
    #column index
    result <- map(seq_along(df), ave_emg, df = df)
    

    You can also replace map with lapply to get the same output.