无截距的总最小二乘回归,R [英] Total Least squares regression without intercept, with R
问题描述
我需要使用以下公式计算两个价格之间的回归系数beta:
I need to calculate the beta of a regression between two prices with:
- 没有拦截
- 使用总最小二乘估计
R中有执行功能prcomp
.
之后,如何提取Beta?
In R there is the function prcomp
to perform it.
After it, how can I extract the beta?
代码是
`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")
})`
在代码中显示了如何使用拦截执行TLS回归.我试图执行相同而没有拦截.
在使用lm
函数的同时,添加+0
允许执行回归而不截距,如何使用prcomp
函数执行同样的操作?
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?
推荐答案
如果R是包含数据的矩阵
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
您还可以检查功能?MethComp::Deming
(位于软件包MethComp
中),其结果类似.
you can also check the function ?MethComp::Deming
(in package MethComp
) which gives similar result.
这篇关于无截距的总最小二乘回归,R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!