Hi Soren, Thanks for the reply. The main reason I want to compute the derivative is to add it to the derivative table in R so that I can use it later in my optimization. And, my complicated function doesn’t have an analytical form of the derivative as it involves distribution function of a bivariate normal distribution.
Thanks, Debangan -- Debangan Dey, PhD Student in Biostatistics, Johns Hopkins Bloomberg School of Public Health, Baltimore, MD From: Søren Højsgaard <sor...@math.aau.dk> Date: Wednesday, April 14, 2021 at 6:56 PM To: Debangan Dey <dd...@jhu.edu>, r-help@r-project.org <r-help@r-project.org> Cc: Mikkel Meyer Andersen <m...@math.aau.dk> Subject: Re: [R] NLSR package newDeriv function External Email - Use Caution One approach is to compute things exact using the caracas package; see below. Best regards Søren ---- > library(caracas) > f <- function(x,y){x+y} > def_sym(x,y,z) > f <- x+y^2+y*z^2 > f [caracas]: 2 2 x + y + y⋅z > > d1 <- der(f, c(x,y,z)) > d2 <- der2(f, c(x,y,z)) > > d1 [caracas]: ⎡ 2 ⎤ ⎣1 2⋅y + z 2⋅y⋅z⎦ > d2 [caracas]: ⎡0 0 0 ⎤ ⎢ ⎥ ⎢0 2 2⋅z⎥ ⎢ ⎥ ⎣0 2⋅z 2⋅y⎦ > > tex(d1) [1] "\\left[\\begin{matrix}1 & 2 y + z^{2} & 2 y z\\end{matrix}\\right]" > tex(d2) [1] "\\left[\\begin{matrix}0 & 0 & 0\\\\0 & 2 & 2 z\\\\0 & 2 z & 2 y\\end{matrix}\\right]" > > as_expr(d1) expression(cbind(1, 2*y + z^2, 2*y*z)) > as_expr(d2) expression(rbind(cbind(0, 0, 0), cbind(0, 2, 2*z), cbind(0, 2*z, 2*y))) > On Wed, 2021-04-14 at 19:35 +0000, Debangan Dey wrote: > Hi, > > I am trying to solve a non-linear least square which has a function > from R^3 -> R. Is it possible to define gradient using newDeriv for a > 3-variate scalar function? > > I am trying to use the genD function in numDeriv package to define > numerical gradient and treat them as a function. So far, I have > failed to do it for simple function as follows � > > f <- function(x,y){x+y} > grad.f <- function(x,y){z <- c(x,y) > f2 <- function(z){f(z[1],z[2])} > gd <- genD(f2,z)$D[,1:length(z)] > return(gd) > } > newDeriv(f(x,y), grad.f(x,y)) > > This derivative definition is not working as the following error pops > up � > > fd <- fnDeriv(~ f(x,y), c("x",�y�)) > fd(c(1,2)) > Error in .grad[, "x"] <- grad.f(x) : > number of items to replace is not a multiple of replacement length > > > I know the above example would work if I just give expressions > instead of using grad function in numDeriv but I am trying to use > numDeriv::grad here to see if my toy example can be translated to the > bigger function I would later use it on. > > Thanks, > Debanga > > -- > Debangan Dey, > PhD Student in Biostatistics, > Johns Hopkins Bloomberg School of Public Health, > Baltimore, MD > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Cddey1%40jhu.edu%7C24130e75316f4a4d701308d8ff98701f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637540377919364158%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Q26CwpHPCqViRwuZu4rCc7OyPlO0Kp4nr84JJ3Llcyk%3D&reserved=0 > PLEASE do read the posting guide > https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Cddey1%40jhu.edu%7C24130e75316f4a4d701308d8ff98701f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637540377919364158%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=R6SR3Eap%2FqHHm0jn%2BibWmM9hlLqkSzehU74ZCZJo3ok%3D&reserved=0 > and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.