Re: [R] derivatives in R

From: Lavan <>
Date: Mon, 24 Mar 2008 20:05:18 -0700 (PDT)

hi thanks,
pls see the code below. the elements of matrix Il should be the derivatives in matrix h (in Boldface) plus some additional terms, here I did calculate derivatoves in the matrix, but I want R to calculate it for me.

pid<- function (tau1,tau2,sigma=10,n=100,x_bar=20) {


a1	<- 1.5
a2	<- 4

t1	<-seq(-1,1);
t2	<-seq(3,4);

l	<-length(t1);
m	<-length(t2);

N1	<- matrix(NA,l,m);
N2	<- matrix(NA,l,m);
wp	<- matrix(NA,l,m);
rr	<- matrix(NA,l,m);
N	<- matrix(NA,l,m);
mu	<- matrix(NA,l,m);
Il	<- matrix(NA,l,m);
iIl	<- matrix(NA,l,m);

theta 	<-expression(x+y);
dx	<-D(theta, "x");
dy	<-D(theta, "y"); 
d2x	<-D(dx, "x");
dxy	<-D(dx, "y"); 
dyx	<-D(dy, "x");
d2y	<-D(dy, "y");

h	<- matrix(c(d2x,dxy,dyx,d2y),nrow=2,ncol=2); 

for (i in 1:l)
            for (j in 1:m)
	mu[i,j] <-(t1[i] + t2[j]);
	iIl <-solve(Il);
            ul[i,j] <-


        ul <-matrix(c(n*(x_bar-mu)/(sigma^2)-(theta1-a1)/(tau1^2),n*(x_bar-mu)/

	ult <-t(ul);
	wl[i,j] <-t(ul)%*%solve(Il,ul);
	rr[i,j] <-(wp[i,j]/wl[i,j]);
	N[i,j]	<-(wl[i,j]-wp[i,j])/wl[i,j]


           list("Test Statistics Wp"= wp,"Stat Ratio"=rr, "N"=N,"N1"=N1, "N2"=N2, "mean"=mu, "Il"=Il,

           "inverse of Il"=iIl)

View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 25 Mar 2008 - 04:09:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Mar 2008 - 05:30:24 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive