Assume we have a DF with hundreds of linear model parameters, including slope m and y-intercept b, as well as upper-limits for integration up_lim.
tmp_df <- tibble(m = rnorm(1:1000, mean = 1, sd = 1),
b = rnorm(1:1000, mean = 3, sd = 0.5),
up_lim = rnorm(1:1000, mean = 11, sd = 4))
My goal is to row-wise integrate over x, from 0 to up_lim using a simple linear model:
integrand <- function(x) { m * x + b }
The result should be stored in a new column in tmp_df. I did some searching online and I am aware of the non-vector nature of the integrate
function, but could not translate any of the discussion/solutions that I found to my case. My best solution was to loop, which works on a few hundred integrations but crashes my 12 Core MacBook (even after I tried multi-core support) when I feed it my full data set (> 1 million rows):
lapply(c("foreach", "doParallel"),
library, character.only = TRUE)
n <- nrow(tmp_df)
registerDoParallel(numCores)
tmp_df$Fs_linear <-
foreach (i = 1:n, .combine = rbind) %dopar% {
integrate(
function(x) { tmp_df$m[i] * x + tmp_df$b[i] },
lower = 0,
upper = tmp_df$up_lim[i])$value
}
stopImplicitCluster()
Is there an elegant/resource-efficient way to accomplish this? I would be incredibly thankful for any pointers.