R的稀疏矩阵支持quantreg

时间:2015-06-23 19:37:15

标签: r matrix sparse-matrix

require("quantreg")
require("SparseM")
Adata <- read.csv("A.CSV", header=FALSE)
Amat = as.matrix(Adata) 
A <- as.matrix.csr(Amat)
bdata <- read.csv("b.CSV", header=FALSE)
bmat = as.matrix(bdata) 
b <- as.matrix.csr(bmat)
dim(A)
# [1] 156  39
dim(b)
# [1] 156   1
rq.fit.sfn(A, b, tau = 0.5)
# Error in rq.fit.sfn(A, b, tau = 0.5) : 
#   Dimensions of design matrix and the response vector not compatible

有人可以解释为什么我收到上述错误消息? A和b的行数相同。

1 个答案:

答案 0 :(得分:0)

我可以使用示例数据重现您的示例:

set.seed(144)
Amat <- matrix(rnorm(156*39), nrow=156)
A <- as.matrix.csr(Amat)
bmat <- matrix(rnorm(156), nrow=156)
b <- as.matrix.csr(bmat)
rq.fit.sfn(A, b, tau=0.5)
# Error in rq.fit.sfn(A, b, tau = 0.5) : 
#   Dimensions of design matrix and the response vector not compatible

解决方案(遵循?rq.fit.sfn中的示例)很简单 - 将b作为向量而不是matrix.csr对象传递:

rq.fit.sfn(A, as.vector(bmat), tau=0.5)
# $coef
#  [1] -0.061177074  0.074329205 -0.093461863  0.053660748  0.091429085 -0.064737848  0.016196847  0.115440861
#  [9] -0.067132296  0.083466922  0.061023772 -0.061221618  0.005084401 -0.143618212 -0.069085262 -0.018858796
# [17] -0.064192848  0.082730295 -0.097342244 -0.008241243  0.048140576 -0.074112669 -0.005409625  0.079146025
# [25] -0.041053199 -0.078594431 -0.021070294 -0.050820932  0.036453319  0.009325523 -0.099606843 -0.074613099
# [33] -0.082790489 -0.040980835  0.145416960  0.144655315  0.024062113  0.008003662  0.135367952
# 
# $ierr
# [1] 0
# 
# $it
# [1] 11
# 
# $time
# [1] 4.446591e-323