I am trying to store functions and their arguments in a list, for example, in the following:
parameters <- list(a1 = list(fun = dnorm,
args = c(mean = 150, sd = 100)),
a2 = list(fun = dpois,
args = c(lambda = 40)))
Now I would like to use stored functions and their arguments without using eval(parse()).
Now suppose that I want to calculate functions using the following vector:
x <- 1:100
According to what I'm looking for, if I call the function stored in "a1", the results should be identical:
dnorm(x, mean = 150, sd = 100)
[1] 0.001314684 0.001334353 0.001354181 0.001374165 0.001394306 0.001414600 0.001435046 0.001455641 0.001476385
[10] 0.001497275 0.001518308 0.001539483 0.001560797 0.001582248 0.001603833 0.001625551 0.001647397 0.001669370
[19] 0.001691468 0.001713686 0.001736022 0.001758474 0.001781038 0.001803712 0.001826491 0.001849373 0.001872354
[28] 0.001895432 0.001918602 0.001941861 0.001965205 0.001988631 0.002012135 0.002035714 0.002059363 0.002083078
[37] 0.002106856 0.002130691 0.002154582 0.002178522 0.002202508 0.002226535 0.002250599 0.002274696 0.002298821
[46] 0.002322970 0.002347138 0.002371320 0.002395511 0.002419707 0.002443904 0.002468095 0.002492277 0.002516443
[55] 0.002540591 0.002564713 0.002588805 0.002612863 0.002636880 0.002660852 0.002684774 0.002708640 0.002732444
[64] 0.002756182 0.002779849 0.002803438 0.002826945 0.002850364 0.002873689 0.002896916 0.002920038 0.002943050
[73] 0.002965948 0.002988724 0.003011374 0.003033893 0.003056274 0.003078513 0.003100603 0.003122539 0.003144317
[82] 0.003165929 0.003187371 0.003208638 0.003229724 0.003250623 0.003271330 0.003291840 0.003312147 0.003332246
[91] 0.003352132 0.003371799 0.003391243 0.003410458 0.003429439 0.003448180 0.003466677 0.003484925 0.003502919
[100] 0.003520653
I tried the following example:
> parameters[[1]]$fun(x, parameters[[1]]$args)
[1] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[9] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[17] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[25] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[33] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[41] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[49] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[57] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 1.097221e-314 0.000000e+00 1.506905e-282
[65] 0.000000e+00 3.790526e-252 0.000000e+00 1.746366e-223 0.000000e+00 1.473646e-196 0.000000e+00 2.277577e-171
[73] 0.000000e+00 6.447260e-148 0.000000e+00 3.342714e-126 0.000000e+00 3.174282e-106 0.000000e+00 5.520948e-88
[81] 0.000000e+00 1.758750e-71 0.000000e+00 1.026163e-56 0.000000e+00 1.096607e-43 0.000000e+00 2.146384e-32
[89] 0.000000e+00 7.694599e-23 0.000000e+00 5.052271e-15 0.000000e+00 6.075883e-09 0.000000e+00 1.338302e-04
[97] 0.000000e+00 5.399097e-02 0.000000e+00 3.989423e-01
But this is not consistent with the results. dnorm(1:100, mean = 150, sd = 100)
The only workaround I have found so far is to use eval(parse()), but I would like to find an easier way.
Is it possible?
Thank you for your help.
Boris
source
share