Total Least squares regression without intercept, with R

别来无恙 提交于 2019-12-11 07:00:56

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!