Quick question: Which function do you use to calculate partial derivatives from a model equation? I've looked at deriv(), but think it gives derivatives, not partial derivatives. Of course my equation isn't this simple, but as an example, I'm looking for something that let's you control whether it's a partial or not, such as: somefunction(y~a+bx, with respect to x, partial=TRUE) Is there anything like this in R? -- View this message in context: http://www.nabble.com/Partial-Derivatives-in-R-tp23470413p23470413.html Sent from the R help mailing list archive at Nabble.com.
If you want `numerical' partial derivatives, check out: require(numDeriv) ?grad Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Paul Heinrich Dietrich <paul.heinrich.dietrich at gmail.com> Date: Sunday, May 10, 2009 4:45 pm Subject: [R] Partial Derivatives in R To: r-help at r-project.org> Quick question: > > Which function do you use to calculate partial derivatives from a model > equation? > > I've looked at deriv(), but think it gives derivatives, not partial > derivatives. Of course my equation isn't this simple, but as an example, > I'm looking for something that let's you control whether it's a > partial or > not, such as: > > somefunction(y~a+bx, with respect to x, partial=TRUE) > > Is there anything like this in R? > -- > View this message in context: > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
On May 10, 2009, at 10:05 AM, Paul Heinrich Dietrich wrote:> > Quick question: > > Which function do you use to calculate partial derivatives from a > model > equation? > > I've looked at deriv(), but think it gives derivatives, not partial > derivatives.Your reading of the help page and the examples differs from mine. It says: " It returns a call for computing the expr and its (partial) derivatives, simultaneously." > dx2x <- deriv(~ x^2, "x") ; dx2x # the example on the help page expression({ .value <- x^2 .grad <- array(0, c(length(.value), 1L), list(NULL, c("x"))) .grad[, "x"] <- 2 * x attr(.value, "gradient") <- .grad .value }) > dyx2.x <- deriv(~ y*x^2, "x") ; dyx2.x expression({ .value <- y * x^2 .grad <- array(0, c(length(.value), 1L), list(NULL, c("x"))) .grad[, "x"] <- y * (2 * x) attr(.value, "gradient") <- .grad .value }) > dy2x2.x <- deriv(~ y^2*x^2, "x") ; dy2x2.x expression({ .expr1 <- y^2 .value <- .expr1 * x^2 .grad <- array(0, c(length(.value), 1L), list(NULL, c("x"))) .grad[, "x"] <- .expr1 * (2 * x) attr(.value, "gradient") <- .grad .value }) > dy2x2.xy <- deriv(~ y^2*x^2, c("x","y")) ; dy2x2.xy expression({ .expr1 <- y^2 .expr2 <- x^2 .value <- .expr1 * .expr2 .grad <- array(0, c(length(.value), 2L), list(NULL, c("x", "y"))) .grad[, "x"] <- .expr1 * (2 * x) .grad[, "y"] <- 2 * y * .expr2 attr(.value, "gradient") <- .grad .value })> Of course my equation isn't this simple, but as an example, > I'm looking for something that let's you control whether it's a > partial or > not, such as: > > somefunction(y~a+bx, with respect to x, partial=TRUE) >That appears to be precisely what your are offered with deriv, .... just not needing the partial=TRUE deriv( David Winsemius, MD Heritage Laboratories West Hartford, CT
Have you considered genD{numDeriv}? If this does not answer your question, I suggest you try the "RSiteSearch" package. The following will open a list of options in a web browser, sorted by package most often found with your search term: library(RSiteSearch) pd <- RSiteSearch.function('partial derivative') pds <- RSiteSearch.function('partial derivatives') attr(pd, 'hits') # 58 attr(pds, 'hits')# 52 summary(pd) HTML(pd) HTML(pds) The development version available via 'install.packages("RSiteSearch", repos="http://R-Forge.R-project.org")' also supports the following: pd. <- unionRSiteSearch(pd, pds) attr(pd., 'hits')# 94 HTML(pd.) Hope this helps. Spencer Graves Paul Heinrich Dietrich wrote:> Quick question: > > Which function do you use to calculate partial derivatives from a model > equation? > > I've looked at deriv(), but think it gives derivatives, not partial > derivatives. Of course my equation isn't this simple, but as an example, > I'm looking for something that let's you control whether it's a partial or > not, such as: > > somefunction(y~a+bx, with respect to x, partial=TRUE) > > Is there anything like this in R? >
Thank you for suggesting other functions, I will look into them. When I read the deriv() function, it did mention partial, but I (being a newbie) wasn't able to get partials for a simple MNL equation. I'm sure I did something wrong then, but here's what I tried the following and got different answers, concluding prematurely maybe that deriv actually just gives regular derivatives. Thanks for looking: ### Variables for an observation x01 <- rnorm(1,0,1) x02 <- rnorm(1,0,1) ### Parameters for an observation b00.1 <- rnorm(1,0,1) b00.2 <- rnorm(1,0,1) b00.3 <- 0 b01.1 <- rnorm(1,0,1) b01.2 <- rnorm(1,0,1) b01.3 <- 0 b02.1 <- rnorm(1,0,1) b02.2 <- rnorm(1,0,1) b02.3 <- 0 ### Predicted Probabilities for an observation phat1 <- 0.6 phat2 <- 0.3 phat3 <- 0.1 ### Correct way to calculate a partial derivative for MNL partial.b01.1 <- phat1 * (b01.1 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b01.2 <- phat2 * (b01.2 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b01.3 <- phat3 * (b01.3 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b01.1; partial.b01.2; partial.b01.3 partial.b02.1 <- phat1 * (b02.1 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b02.2 <- phat2 * (b02.2 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b02.3 <- phat3 * (b02.3 - (b01.1*phat1+b01.2*phat2+b01.3*phat3)) partial.b02.1; partial.b02.2; partial.b02.3 ### Derivatives for MNL according to (my interpretation of) the deriv() function dp1.dx <- deriv(phat1 ~ exp(b00.1+b01.1*x01+b02.1*x02) / (exp(b00.1+b01.1*x01+b02.1*x02)+exp(b00.2+b01.2*x01+b02.2*x02)+ exp(b00.3+b01.3*x01+b02.3*x02)), c("x01","x02")) dp2.dx <- deriv(phat2 ~ exp(b00.2+b01.2*x01+b02.2*x02) / (exp(b00.1+b01.1*x01+b02.1*x02)+exp(b00.2+b01.2*x01+b02.2*x02)+ exp(b00.3+b01.3*x01+b02.3*x02)), c("x01","x02")) dp3.dx <- deriv(phat3 ~ exp(b00.3+b01.3*x01+b02.3*x02) / (exp(b00.1+b01.1*x01+b02.1*x02)+exp(b00.2+b01.2*x01+b02.2*x02)+ exp(b00.3+b01.3*x01+b02.3*x02)), c("x01","x02")) attr(eval(dp1.dx), "gradient") attr(eval(dp2.dx), "gradient") attr(eval(dp3.dx), "gradient") -- View this message in context: http://www.nabble.com/Partial-Derivatives-in-R-tp23470413p23475411.html Sent from the R help mailing list archive at Nabble.com.