我有一個函數Q(x | delta):R^n - > R我想擬合非線性分位數迴歸。函數Q(。)使用了一些矩陣運算,不使用它會非常複雜。問題是,當在公式參數中使用的函數中存在矩陣運算時,似乎nlrq(非線性分位數迴歸)和nls(非線性迴歸)不起作用。爲了說明,考慮在我不使用矩陣運算但不使用矩陣運算的情況下我可以在nlrq和nls函數的公式變量中使用的更簡單的函數F(x1,x2 | a,b,c)在用矩陣運算寫入時在公式參數中工作。「在...%*%...中出錯:非適形參數」在迴歸中使用自己的函數
library('quantreg')
## Generating the data
x1<- rnorm(200)
x2<- rnorm(200)
y<- 1+3*sin(x1)+2*cos(x2) +rnorm(200)
Dat<- data.frame(y,x1,x2)
## The function F1 without matrix operation
F1<- function(x_1, x_2, a, b,c){a+b*sin(x_1)+c*cos(x_2)}
## The function F2 with matrix operation
F2<- function(x_1, x_2, a, b,c){t(c(1,sin(x_1),cos(x_2)))%*%c(a,b,c)}
## Both functions work perfectly
F1(x_1=3, x_2=2, a=1, b=3,c=2)
F2(x_1=3, x_2=2, a=1, b=3,c=2)
## But only F1 can be estimated by nls and nlrq
nls_1<-nls(y ~ F1(x_1 = x1, x_2 = x2, a = 1, b, c),
data = Dat, start = list(b = 3, c = 2))
nlrq_1<-nlrq(y ~ F1(x_1 = x1, x_2 = x2, a = 1, b, c),
data = Dat, start = list(b = 3, c = 2), tau = 0.9)
## When F2 is used in the formula argument an error happens
nls_2<-nls(y ~ F2(x_1 = x1, x_2 = x2, a = 1, b, c),
data = Dat, start = list(b = 3, c = 2))
nlrq_2<-nlrq(y ~ F2(x_1 = x1, x_2 = x2, a = 1, b, c),
data = Dat, start = list(b = 3, c = 2), tau = 0.9)
的錯誤是Error in t(c(1, sin(x_1), cos(x_2))) %*% c(a, b, c) : non-conformable arguments
。我相信如果有人設法通過使用nls和nlrq的矩陣運算來估計F2,我可以在我的其他函數中使用相同的解決方案。
Dat的大小是200x3。
非常感謝。
對不起,我忘了提及要使用nlrq,需要使用quantreg包,然後在代碼中添加'install.packages('quantreg')'和'library('quantreg')'。 – ThiagoSC