Calculate Variance Covariance Matrix in R manually

#Mr. Rohan Byanjankar

#E-mail : rohanbjkr@gmail.com

#Manual Calculation of Variance Covariance Matrix in R

# This dataset has been Taken from "An Introduction to Econometrics by Christopher Dougherty"

#Data file saved in Siddha Sir's Blog

datafile<- read.csv("https://sites.google.com/site/siddhabhatta/data/data3.csv", header=T)


#Derivation of Variance Covariance Matrix

#Formula: [X - [1 1'X]/n]'*[X - [1 1'X]/n]

attach(datafile)

y = wage_rate 

X = cbind (yearsedu, exp, ASVABC)#Extract yearsedu, exp and ASVABC from datafile

X1 <- matrix(1:1, nrow=540, byrow=TRUE)#Matrix of 540x1 with "1" 

X1t <- t(X1)#transpose of X1

n <- nrow(X1)

varcov1 <- ((t(X-(X1%*%((X1t%*%X)/n))))%*%(X-(X1%*%((X1t%*%X)/n))))/n

varcov1

Result

                   yearsedu       exp           ASVABC

yearsedu     5.935154  -2.351877   13.532991

exp             -2.351877  19.612091  2.532059

ASVABC    13.532991  2.532059  91.385882



Post a Comment

0 Comments