[R] NLSR package newDeriv function

Søren Højsgaard @orenh @end|ng |rom m@th@@@u@dk
Thu Apr 15 00:55:38 CEST 2021


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 using 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.


More information about the R-help mailing list