[R] numericDeriv() giving a vector when multiple variables input
Spencer Graves
spencer.graves at pdf.com
Sun Jan 22 08:15:42 CET 2006
I've never used "numericDeriv" before, but from reading the
documentation and working throught the example, a little thought led me
to the following example:
f <- function(g., A){
A %*% g.
}
g <- function(x1, x2, x3, x4, G){
G %*%c(x1, x2, x3, x4)
}
env234 <- new.env()
A <- array(1:6, dim=c(2,3))
G <- array(1:12, dim=c(3,4))
assign("A", A, env=env234)
assign("G", G, env=env234)
assign("x1", 1., env=env234)
assign("x2", 2., env=env234)
assign("x3", 3., env=env234)
assign("x4", 4., env=env234)
(AG <- numericDeriv(quote(f(g(x1, x2, x3, x4, G), A)),
c("x1", "x2", "x3", "x4"), env234))
(AG. <- A%*%G)
(AG.%*%1:4)
This seemed to work for me and give me the correct answer. If this
is not enough, RSiteSearch("numericDeriv") gave me 115 hits; many of
these are not relevant to your question, but I would expect that some
might be.
hope this helps.
spencer graves
Seth Pruitt wrote:
> R Help List --
>
> I have defined two time-series-vector-valued-functions, let them be f and g,
> and want to find the numeric derivative of f with respect to the variable x
> where f depends on x through g:
> (d/dx)(f (g(x) )
>
> Moreover, x is a vector
>
> I tried this out the long way (naming every element of the x vector and then
> making the 'theta' argument in numericDeriv() the character vector of all
> these names) and the result is just one time series vector; I was hoping for
> a matrix. Also weirdly, if I instead make theta equal to just one of the
> named elements of x, I get the same time series vector; the same happens for
> any subset of the named elements
>
> My call to numericDeriv looks like this (vphi acts as x,
> swz.kalman.vectoracts as g,
> decision.ts.vector acts as f):
>
> ***
> numericDeriv(
> expr = decision.ts.vector(
> a = swz.kalman.vector(
> zeta1=zeta[1], u = Phi$u, phi = Phi$Phi,
> vphi =
> c(varphi1,varphi2,varphi3,varphi4,varphi5,varphi6,varphi7,varphi8,varphi9,varphi10,
>
> varphi11,varphi12,varphi13,varphi14,varphi15,varphi16,varphi17,varphi18,varphi19,
>
> varphi20,varphi21,varphi22,varphi23,varphi24,varphi25,varphi26,varphi27,varphi28,
>
> varphi29,varphi30,varphi31,varphi32,varphi33,varphi34,varphi35,varphi36,varphi37,
> varphi38,varphi39,varphi40,varphi41,varphi42),
> alpha.prior = specs$alpha.prior
> ),
> phi = Phi$Phi, lambda = specs$lambda, delta = specs$delta, pi.star =
> specs$pi.star, u.2star = specs$u.2star
> ),
> theta =
> c("varphi1","varphi2","varphi3","varphi4","varphi5","varphi6","varphi7","varphi8","varphi9","varphi10",
>
> "varphi11","varphi12","varphi13","varphi14","varphi15","varphi16","varphi17","varphi18","varphi19",
>
> "varphi20","varphi21","varphi22","varphi23","varphi24","varphi25","varphi26","varphi27","varphi28",
>
> "varphi29","varphi30","varphi31","varphi32","varphi33","varphi34","varphi35","varphi36","varphi37",
> "varphi38","varphi39","varphi40","varphi41","varphi42")
> );
> ***
>
> As you can see, it includes some calls to other objects that are lying
> around. Maybe from this email someone can tell me my mistake with how I've
> called things; otherwise, I'm happy to send along the .R files to whomever
> is so kind as offer guidance.
>
> I appreciate your time,
> Seth
>
> --
> Seth Pruitt
> Department of Economics
> University of California, San Diego
> sjpruitt at ucsd.edu
> http://dss.ucsd.edu/~sjpruitt
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list