[R] NLSR package newDeriv function

Debangan Dey ddey1 @end|ng |rom jhu@edu
Wed Apr 14 21:35:30 CEST 2021


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]]



More information about the R-help mailing list