I'm posting in response to Matt's SO post and comments below.
I have a function, toopt(th, dat). The goal is to minimize the function in terms of its first argument. The second argument is a data.table. I guess this is a pretty common use case.
library(data.table)
f = function(dat, th){
dat[, prob := dpois(x, th) ][]
}
toopt = function(th, dat){
mydat = data.table:::shallow(dat)
f(mydat, th)
mydat[, - sum(log(prob))]
}
nid = 10
nobs = 10
par = 20
DT = data.table(id = seq_len(nid))[, .(x = rpois(nobs, par)), by=id]
optimize(toopt, dat = DT, c(0,100))
mean(DT$x) # the closed-form solution
Inside optim or optimize, the function is called many times as it searches for the optimum.
Sorry if this example is too trivial or abstract. In my actual example, I have a sequence of functions like f that add columns (stored in separate functions so I can isolate them for easier testing and to reduce duplication, since I have multiple toopt functions that are similar but not identical); and my DT has some columns that I don't need for the calculations in toopt.
Besides functions passed to optim, I also use / want to use shallow in other functions, so I can benefit from the efficiency of not making a copy without worrying about whether my input will be messed with (if I didn't use copy) or about whether I need to to prune my input table's columns down to the minimum required for the function (if I did use copy).
I'm posting in response to Matt's SO post and comments below.
I have a function,
toopt(th, dat). The goal is to minimize the function in terms of its first argument. The second argument is a data.table. I guess this is a pretty common use case.Inside
optimoroptimize, the function is called many times as it searches for the optimum.Sorry if this example is too trivial or abstract. In my actual example, I have a sequence of functions like
fthat add columns (stored in separate functions so I can isolate them for easier testing and to reduce duplication, since I have multipletooptfunctions that are similar but not identical); and myDThas some columns that I don't need for the calculations intoopt.Besides functions passed to
optim, I also use / want to useshallowin other functions, so I can benefit from the efficiency of not making a copy without worrying about whether my input will be messed with (if I didn't usecopy) or about whether I need to to prune my input table's columns down to the minimum required for the function (if I did usecopy).