Search code examples
riterationgetvalue

How to repeat a code for multiple times and store the output of each iteration in the same dataframe?


I am asking two questions:

  1. How to run a code for multiple times?
  2. How to store the output of each iteration in the same dataframe?

I have 3 outputs in my code: mae,rmse and per_metrics. I only want per_metrics for my final output for every iteration, and store the results from each per_metics for my overall output out of all the iterations.

    getValue <- function() {
    mae <- mae(dp$true_norm, dp$dp_threshold_norm)
    rmse <- rmse(dp$true_norm, dp$dp_threshold_norm)
    per_metrics <- c(mae,rmse)
return(i)
    }
result <- replicate(10, getValue())

N.B. Number of iterations = 10

This is only part of my code, I have the same input for all iterations but a noise adding mechanism rlaplace() between the input and output. So I get a different result in every iteration.


Solution

  • In the absence of a reproducible example, the following uses an example from the {Metrics} package documentation to construct your dataframe dp. Augment as appropriate.

    Further you need to provide parameters to your function. In this case we supply the data frame dp (which you call in your function).

    Lastly, replicate() returns an array/matrix. We reformat this into a "long" format and then coerce it to a data frame.

    library(Metrics)
    
    # simulate the data -----------------------------------------
    actual <- c(1.1, 1.9, 3.0, 4.4, 5.0, 5.6)
    predicted <- c(0.9, 1.8, 2.5, 4.5, 5.0, 6.2)
    
    dp <- data.frame(
        true_norm         = actual
      , dp_threshold_norm = predicted
    )
    
    # make function work -----------------------------------------
    getValue <- function(dp) {          # we define a parameter dp for the function
         mae <- mae(dp$true_norm, dp$dp_threshold_norm)
         rmse <- rmse(dp$true_norm, dp$dp_threshold_norm)
         per_metrics <- c(mae,rmse)
         return(per_metrics)            # return value 
    }
    
    # apply function multiple times with replicate()
    # check this to understand the returned data format
    replicate(n = 10, expr = getValue(dp))
    
    # result ---------------------------------------------
    ## store in variable
    result <- replicate(n = 10, expr = getValue(dp))
    
    ## coerce to "long data frame"  - here we set ncol 2 for 2 variables
    result <- matrix(result, ncol = 2)
    
    ## coerce to data frame
    result <- as.data.frame.array(result)
    

    This yields:

    result
    
              V1        V2
    1  0.2500000 0.2500000
    2  0.3341656 0.3341656
    3  0.2500000 0.2500000
    4  0.3341656 0.3341656
    5  0.2500000 0.2500000
    6  0.3341656 0.3341656
    7  0.2500000 0.2500000
    8  0.3341656 0.3341656
    9  0.2500000 0.2500000
    10 0.3341656 0.3341656
    

    You can now rename the columns as required.