Search code examples
rsimulationlapply

Multiplying simulated variables by simulated variables across multiple observations


This is an offshoot of my previous query on substituting simulated variables using rnorm().

Now, I'm faced with another tricky thing to navigate since I'm a very beginner in R. Say:

# Values representing the number of simulations for each observation
simulations <- c(728, 680, 631, 583, 534, 486, 437, 388, 340, 291)

# Use lappy to generate the series of simulations for each observation
unit.cost <- lapply(simulations, rnorm, mean = 8403.86, sd = 1000)
bldg.size <- lapply(simulations, rnorm, mean = 35, sd = 5)

Considering that the mean and sd are constant for unit.cost and bldg.size, how do I multiply the simulated variables against each other for EACH observation, returning a list of the product (multiplied variables). For example, on the first observation, there will be 728 simulations of unit.cost and 728 simulations of bldg.size. I want these simulations be multiplied against each other, in order, to come up with a new variable called "bldg.cost" representing the product.

I'm also expecting to generate the sum of "bldg.cost" for each observation. In the end, I should have a data frame reflecting the sum of "bldg.cost" for each observation.


Solution

  • You can use Map

    Map(function(x,y) x * y, unit.cost, bldg.size)
    

    library(tidyverse)
    
    bldg <- tibble(
        simulations,
        unit.cost = lapply(simulations, rnorm, mean = 8403.86, sd = 1000),
        bldg.size = lapply(simulations, rnorm, mean = 35, sd = 5),
        bldg.cost = Map(function(x,y) x * y, unit.cost, bldg.size),
      ) |>
      unnest(cols=c(unit.cost, bldg.size, bldg.cost))
    
    summarise(bldg, sum=sum(bldg.cost), .by=simulations)