Applying the optim function in R in C++ with Rcpp
问题 I am trying to call R function optim() in Rcpp . I saw an example in Calling R's optim function from within C++ using Rcpp, but I am unable to modify it correctly for my use case. Basically, the objective function depends on the x and y but I want to optimize it with respect to b . Here is the R code that does what I want: example_r = function(b, x, y) { phi = rnorm(length(x)) tar_val = (x ^ 2 + y ^ 2) * b * phi objftn_r = function(beta, x, y) { obj_val = (x ^ 2 + y ^ 2) * beta return(obj_val