问题
I need to calculate the beta of a regression between two prices with:
- No intercept
- Using Total Least Squares estimate
In R there is the function prcomp
to perform it.
After it, how can I extract the beta?
the code is
`library(quantmod)
# how to get closes
getCloses <- function(sym) {
ohlc <- getSymbols(sym, from="2009-01-01", to="2011-01-01",
auto.assign=FALSE, return.class="zoo")
Cl(ohlc)}
# how to import data (2 assets)
closes <- merge(IWM=getCloses("IWM"),
VXZ=getCloses("VXZ"), all=FALSE)
# function for hedging ratio
tlsHedgeRatio <- function(p, q) {
r <- princomp( ~ p + q+0)
r$loadings[1,1] / r$loadings[2,1]
}
# get the hedging ratio
with(closes, {
cat("TLS for VXZ vs. IWM =", tlsHedgeRatio(VXZ,IWM), "\n")
})`
In the code show how to perform TLS regression with intercept. I trying to perform the same without intercept.
While with lm
function, adding +0
allow to perform regression without intercept, how could I do the same with prcomp
function?
回答1:
If R is the matrix that contains the data
pcaresult <- prcomp(R)
eigenVect <- pcaresult$rotation
eigenVal <- (pcaresult$sdev)^2
coeff1 = as.numeric(coeff$eigenvectors[,"PC2"][1])
coeff2 = -as.numeric(coeff$eigenvectors[,"PC2"][2])
ratio = coeff2/coeff1
you can also check the function ?MethComp::Deming
(in package MethComp
) which gives similar result.
来源:https://stackoverflow.com/questions/28337205/total-least-squares-regression-without-intercept-with-r