【问题标题】:Total Least squares regression without intercept, with R无截距的总最小二乘回归,R
【发布时间】:2015-02-05 06:02:59
【问题描述】:

我需要计算两个价格之间的回归贝塔:

  1. 没有拦截
  2. 使用总最小二乘估计

在 R 中有函数 prcomp 来执行它。 之后,如何提取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 允许执行没有截距的回归,但我如何使用prcompfunction 做同样的事情?

【问题讨论】:

  • 可以通过向reproducibe example 提供一些样本(假)输入数据来改进这个问题,以便我们了解您的数据是如何形成的。我不明白你为什么要使用 prcomp 而不是标准的 lm 函数来拟合回归。
  • 如果您有兴趣在 prcomp 中应用 TLS,请阅读this
  • @MrFlick TLS 有时是首选,因为它并不假定所有错误都来自您指定的自变量。这对线性模型的斜率和截距有影响:statpad.files.wordpress.com/2010/12/ols_eiv_plots.jpeg
  • 谢谢@Stu 不知何故我错过了“总”部分。
  • @user3641445 问之前你试过了吗?您缺少哪一点?请分享您的代码。

标签: r regression principal-components


【解决方案1】:

如果R是包含数据的矩阵

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中),它给出了类似的结果。

【讨论】:

  • 现在我明白了如何提取系数,但我还不明白如何在没有截距的情况下做到这一点 (y=beta*x)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 2011-10-15
  • 2015-05-15
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多