簡體   English   中英

R稀疏矩陣對Quantreg的支持

[英]R sparse matrix support for quantreg

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具有相同的行數。

我可以用示例數據重現您的示例:

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對象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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM