2015-06-23 114 views
0
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具有相同的行数。R稀疏矩阵支持quantreg

+0

从这里看一个例子:http://astrostatistics.psu.edu/datasets/2006tutorial/html/quantreg/html/rq.fit.sfn.html该函数似乎想要在您的设计矩阵中的一列1'w大概是拦截?,试试'A < - as.matrix.csr(cbind(1,Amat) )' – Vlo

回答

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