[R] Partial Derivatives in logit

Paul Heinrich Dietrich paul.heinrich.dietrich at gmail.com
Thu Apr 16 20:54:09 CEST 2009


Hi,
I'm trying to figure out how to have R estimate partial derivatives for
logit models.  As an example, I'm providing a (fake) scored observation in a
MNL with 3 categories of Y and 2 predictors (x01 and x02), and show the
right way to calculate it, but am looking for how to use an R function, such
as deriv() or anything else.  Here is my attempt:

### Variables for an observation
x01 <- 0.2
x02 <- 0.1
### Parameters for an observation
b00.1 <- 0.5
b00.2 <- 0.3
b00.3 <- 0
b01.1 <- 0.4
b01.2 <- 0.5
b01.3 <- 0
b02.1 <- 0.3
b02.2 <- -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
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

### Test method...so far, using the deriv() function
test <- 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"))
eval(test)

Obviously I'm not on the right path.  How do I set up something like this? 
I've read (probably not well enough) the ?deriv() information and found a
few threads on here about partial derivatives, but I can't figure it out. 
Thanks for any suggestions.
-- 
View this message in context: http://www.nabble.com/Partial-Derivatives-in-logit-tp23084203p23084203.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list