[R] Help for numericDeriv function

William Dunlap wdunlap at tibco.com
Fri May 18 20:48:43 CEST 2012


You could use parse(text=a)[[1]]:
   > numericDeriv(parse(text=a)[[1]], c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
       [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    1    0    3    0    0    0

or construct your expression directly:
  > lnames <- array(lapply(l, as.name), dim=dim(l))
  > aa <- call("+", call("*", lnames[[1,1]], lnames[[2,1]]), call("*", lnames[[1,2]], lnames[[1,2]]))
  > numericDeriv(aa, c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
       [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    1    0    3    0    0    0

The results are identical.  The latter method is a bit more work to set up but
works no matter what the names in the expression look like.  The former
fails if the names contain spaces or other things that are not allowed in
R names.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Cengiz Zopluoglu
> Sent: Friday, May 18, 2012 11:33 AM
> To: r-help at r-project.org
> Subject: Re: [R] Help for numericDeriv function
> 
> I missed a couple line of codes in the previous e-mail. Here is whole code
> again:
> 
> load <- matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE)
> 
> l <- matrix(nrow=nrow(load),ncol=ncol(load))
> for(i in 1:nrow(load)) {
> for(j in 1:ncol(load)) { l[i,j]=paste("l",i,j,sep="")}}
> 
> for(i in 1:nrow(load)){
> for(j in 1:ncol(load)){ assign(paste("l",i,j,sep=""),load[i,j]) }}
> 
> numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE))
> 
> a <- paste(
> paste(l[1,1],l[2,1],sep="*"),
> paste(l[1,2],l[1,2],sep="*"),
> sep="+"
> )
> 
> numericDeriv(???a???, c(t(l),recursive=TRUE))
> 
> Thanks
> 
> On Fri, May 18, 2012 at 1:25 PM, Cengiz ZopluoÄŸlu <cen.zop at gmail.com> wrote:
> 
> > Hi,
> >
> > I am stuck on something for a couple days, I am almost about to give up.
> > This looks simple, but I can't figure out. I hope I can get some help here.
> >
> > I am trying to do some symbolic and numerical derivations. Let me explain
> > the problem. Let's say, I have a matrix as follows:
> >
> > > load <- matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE)
> > >
> > > load
> >       [,1] [,2]
> > [1,]    3    0
> > [2,]    1    4
> > [3,]    1    3
> >
> > I will create objects for each element of the matrix, and assign the
> > matrix elements to these objects. These objects will be also an element of
> > another matrix.
> >
> > l <- matrix(nrow=nrow(load),ncol=ncol(load))
> > for(i in 1:nrow(load)) {
> > for(j in 1:ncol(load)) { l[i,j]=paste("l",i,j,sep="")}}
> >
> > > l
> >      [,1]  [,2]
> > [1,] "l11" "l12"
> > [2,] "l21" "l22"
> > [3,] "l31" "l32"
> >
> > > l11
> > [1] 3
> > > l12
> > [1] 0
> > > l21
> > [1] 1
> > > l22
> > [1] 4
> > > l31
> > [1] 1
> > > l32
> > [1] 3
> >
> > Let's say, I need to take the derivative of  (l11*l21+l12*l22) with
> > respect to l11,l12,l21,l22,l31,l32.
> >
> > > numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE))
> > [1] 3
> > attr(,"gradient")
> >      [,1] [,2] [,3] [,4] [,5] [,6]
> > [1,]    1    4    3    0    0    0
> >
> > Everything is fine up to this point. I need what I have. But, I don't want
> > to manipulate the elements in "quote()" manually within the numericDeriv
> > function. For instance, I want to call these elements from the matrix "l"
> > as below:
> >
> > > a <- paste(
> > + paste(l[1,1],l[2,1],sep="*"),
> > + paste(l[1,2],l[1,2],sep="*"),
> > + sep="+"
> > + )
> > > a
> > [1] "l11*l21+l12*l12"
> >
> > Now, "a" is a character. Do you have any idea about how to use the
> > character value "l11*l21+l12*l12" assigned to object "a" within the
> > numericDeriv function. So, I can get the same result as above without
> > manipulating the expression in quote() manually.
> >
> > Thanks
> >
> >
> >
> >
> > --
> >
> > Cengiz Zopluoglu
> >
> >
> >
> >
> >
> 
> 
> --
> 
> Cengiz Zopluoglu
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list