没有常量的简单回归中的 quantreg lm.recursive.fit
quantreg lm.recursive.fit in simple regression without constant
我尝试使用 R 的 quantreg
包中的函数 lm.fit.recursive
为没有常量的简单回归构造递归残差。
下面是一个无效方法的最小示例:
# some data
n <- 20
z <- rnorm(n)
x <- rnorm(n)
x.mat <- matrix(rnorm(2*n),ncol=2)
lm.fit.recursive(x, z, int=T) # works WITH intercept with one regressor
lm.fit.recursive(x.mat, z, int=F) # works WITHOUT intercept with two regressors
lm.fit.recursive(x, z, int=F) # what I actually want but which returns Error in 1:p : argument of length 0
我的直觉是错误与回归矩阵有关,在这种情况下不是矩阵而是向量,这导致 R 以不同方式对待这个变量。
是这样吗,还是我使用的函数不正确?
的确如此,
> lm.fit.recursive
function (X, y, int = TRUE)
{
if (int)
X <- cbind(1, X)
p <- ncol(X)
n <- nrow(X)
D <- qr(X[1:p, ])
...
}
所以ncol(x)=0
为一个向量。因此,
lm.fit.recursive(as.matrix(x,ncol=1), z, int=F)
提供解决方法。
我尝试使用 R 的 quantreg
包中的函数 lm.fit.recursive
为没有常量的简单回归构造递归残差。
下面是一个无效方法的最小示例:
# some data
n <- 20
z <- rnorm(n)
x <- rnorm(n)
x.mat <- matrix(rnorm(2*n),ncol=2)
lm.fit.recursive(x, z, int=T) # works WITH intercept with one regressor
lm.fit.recursive(x.mat, z, int=F) # works WITHOUT intercept with two regressors
lm.fit.recursive(x, z, int=F) # what I actually want but which returns Error in 1:p : argument of length 0
我的直觉是错误与回归矩阵有关,在这种情况下不是矩阵而是向量,这导致 R 以不同方式对待这个变量。
是这样吗,还是我使用的函数不正确?
的确如此,
> lm.fit.recursive
function (X, y, int = TRUE)
{
if (int)
X <- cbind(1, X)
p <- ncol(X)
n <- nrow(X)
D <- qr(X[1:p, ])
...
}
所以ncol(x)=0
为一个向量。因此,
lm.fit.recursive(as.matrix(x,ncol=1), z, int=F)
提供解决方法。