1

I need to calculate the beta of a regression between two prices with:

  1. No intercept
  2. 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 prcompfunction?

gcats
  • 41
  • 6
  • 2
    This question could be improved by providing a [reproducibe example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with some sample (fake) input data so we see how your data is shaped. I don't see why you would use `prcomp` rather than the standard `lm` function for fitting a regression. – MrFlick Feb 05 '15 at 06:11
  • 1
    If you are interested in applying TLS with prcomp, read [**this**](http://quanttrader.info/public/betterHedgeRatios.pdf). – Stu Feb 05 '15 at 06:13
  • @MrFlick TLS is sometimes preferred because it does not assume all the errors are coming from what you specify as the independent variable. This has an affect on the slope and intercept of the linear model: https://statpad.files.wordpress.com/2010/12/ols_eiv_plots.jpeg – Stu Feb 05 '15 at 06:16
  • Thanks @Stu somehow i missed the "total" part. – MrFlick Feb 05 '15 at 06:17
  • @user3641445 have you tried before asking? Which particular point are you missing? Please share your code. – RockScience Feb 05 '15 at 08:58
  • Code added in the question. It is an example given by Paul Teetor. – gcats Feb 05 '15 at 10:08

1 Answers1

0

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.

RockScience
  • 17,932
  • 26
  • 89
  • 125
  • 1
    now I understood how to extract coefficients, but I didn't understand yet how to do it without intercept (y=beta*x) – gcats Feb 05 '15 at 10:10