[R] Covariance matrix calc method question

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Thu Mar 15 22:50:13 CET 2007


I have been comparing the output of an R package to S+Finmetrics and I
notice that 
the covariance matrix outputted by the two procedures is different. The
R package
computes the covariance matrix using Method 1 and I think ( but I'm not
sure ) that S+Finmetrics computes it 
using Method 2.

I put in a correctionfactor (see below ) in to Method 2 in order to deal
with the fact that the  var function 
calculates the unnbiased estimate of variance. This gives the same
answer in both problems for the
data shown which I just made up.  But, my hope is that , for much larger
problems, leaving the correction factor 
out can maybe cause huge differences in the determinant ? That would
explain why some of the output ( AIC, BIC ) 
differs in the two packages. I'm not really sure how to check this
easily but if someone has an idea,
it would be appreciated. Basically, I kind of want to run some sort of
simulation along
the lines of below to check whether this could be the reason for the
differences.  Thanks.

x<-c(11,12,13,14,16)
y<-c(2,4,6,8,12)
z<-c(14,18,22,50,20)
LHS<-cbind(x,y)
sample<-nrow(lhs)

# METHOD1

output1<-lm(LHS ~ z)
resids<-resid(output1)
sigma.hat1<-crossprod(resids)/sample
print(sigma.hat1)

print(det(sigma.hat1))

# METHOD2

fit1<-lm(LHS[,1] ~ z)
fit2<-lm(LHS[,2] ~ z)

correctionfactor<-sample-1/sample

sigma.hat2<-correctionfactor*var(cbind(resid(fit1),resid(fit2)))

print(sigma.hat2)
print(det(sigma.hat2))



More information about the R-help mailing list