# load corpcor library
library("corpcor")
# example data
data(longley)
x <- as.matrix(longley[ 1:13, -2])
y <- as.matrix(longley[ 1:13, 2, drop=FALSE])
xtest <- as.matrix(longley[14:16, -2])
ytest <- as.matrix(longley[14:16, 2, drop=FALSE])
# least squares regression is recovered if
# shrinkage intensities are all set to zero
lm(y ~ x)
coefs.ols <- mvr.shrink(x, y, lambda=0, lambda.var=0)
coefs.ols
# shrinkage regression coefficients
# note that these are quite different!
coefs.shrink <- mvr.shrink(x, y)
coefs.shrink
# prediction
mvr.predict(coefs.ols, xtest)
mvr.predict(coefs.shrink, xtest)
# if no response matrix is given, then each predictor
# is regressed in turn against all others
coefs <- mvr.shrink(longley)
coefs
# connection between partial correlations and regression coefficients
# (note that the mvr.shrink() function is specifically constructed so that
# this relationship holds for the shrinkage estimates)
beta <- coefs[,-1] # shrinkage regression coefficients w/o intercept
pc <- pcor.shrink(longley) # shrunken partial correlations
k <- 3
l <- 4
# partial correlations as computed by pcor.shrink()
pc[k,l]
# partial correlations obtained from mvr.shrink() regression coefficients
sqrt(beta[k,l] * beta[l,k]) * sign(beta[l,k])
Run the code above in your browser using DataLab