Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

scaling multiple columns in lists using map, map2, mutate_at, summarise_at

$
0
0

I am having problems with the scaled_assessment data I am trying to create.

I have some time series data I split up into analysis and assessment. I want to scale the analysis data and use these scaled means and sd's to apply to the assessment data. I add comments to the code below.

I run into problems tackling the mutate_at function. I want to apply the scale function which takes in the mean and sd from the analysis data and apply it to the assessment data. - For all columns in the assessment data.

Data / code:

    library(rsample)
set.seed(1131)
# I create some random data
ex_data <- data.frame(row = 1:20, some_cat_var = paste("cat"), some_var = rnorm(20), some_other_var = rnorm(20))
ex_data

# I create the analysis and assessment splits - the analysis data has 10 observations the assess has 1
rolled_ex_data <- rolling_origin(ex_data,
                                 initial = 10,
                                 assess = 1, 
                                 cumulative = FALSE,
                                 skip = 0)

# My scaling function to apply to the analysis data
Scale_Me <- function(x){
  (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}

# This I believe "works" I collect the mean and sd from the 3rd and 4th column of the data for each split
scale_values <- map(rolled_ex_data$splits, ~ analysis(.x) %>% 
                      as_tibble(., .name_repair = "universal") %>% 
                      summarise_at(.vars = 3:ncol(.), .funs = c(mean = "mean", sd = "sd")))

# I then apply the scale function to the analysis data (to columns 3 and 4) for each split
scaled_analysis <- map(rolled_ex_data$splits, ~ analysis(.x) %>% 
                         as_tibble(., .name_repair = "universal") %>% 
                         mutate_at(.vars = 3:ncol(.), .funs = c(Scale_Me = "scale")))

# My problem is here with the mutate_at function
scaled_assessment <- map2(rolled_ex_data$splits, scale_values, ~ assessment(.x) %>% 
                            as_tibble(., .name_repair = "universal") %>% 
                            mutate_at(.vars = 3:ncol(.), .funs = c(scaled_col = (.vars - .y$mean) / .y$sd)))

EDIT:

Okay. I have managed to get it working for the two variables using mutate.

scaled_assessment <- map2(rolled_ex_data$splits, scale_values, ~ assessment(.x) %>% 
                            #as_tibble(.x, .name_repair = "universal") %>% 
                            mutate(
                              some_var_scaled = (some_var - .y$some_var_mean) / .y$some_var_sd,
                              some_other_var_scaled = (some_other_var - .y$some_other_var_mean) / .y$some_other_var_sd
                              )
                          )

This gets me a list of 10:

scaled_assessment[[1]]
scaled_assessment[[2]]
scaled_assessment[[3]]

> scaled_assessment[[1]]
  row some_cat_var  some_var some_other_var some_var_scaled some_other_var_scaled
1  11          cat -1.350214      -0.569947       -1.603747            -0.2836588
> scaled_assessment[[1]]
  row some_cat_var  some_var some_other_var some_var_scaled some_other_var_scaled
1  11          cat -1.350214      -0.569947       -1.603747            -0.2836588
> scaled_assessment[[2]]
  row some_cat_var some_var some_other_var some_var_scaled some_other_var_scaled
1  12          cat 2.242594      -1.195205        3.038992            -0.7670828
> scaled_assessment[[3]]
  row some_cat_var some_var some_other_var some_var_scaled some_other_var_scaled
1  13          cat 1.781132      0.9764677        1.593273              1.194117

I would like to know how to do it using mutate_at because I do not know the number of time series columns I have to scale. Here I use 2 columns some_var and some_other_var but I could have 3 or 4 columns which is why I tried to use .vars = 3:ncol(.).


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>