我正在运行分位数回归,我想提取公式以在另一个回归中使用它。
我的例子:
y <- c(0.114,-0.360,-0.102,0.406,-0.363,-0.071,0.579,-0.265,0.290,0.312,0.063,0.181,-0.542,0.653,0.164,0.047,0.481,0.161,-0.481,0.57)
x.1 <- c(24.138,28.021,28.298,33.272,25.571,34.252,29.193,22.557,33.973,28.436,33.575,31.103,23.698,33.067,33.847,33.890,26.616,36.633,23.225,30.186)
x.2 <- c(28.909,25.487,33.834,23.615,33.429,24.235,33.436,30.065,22.448,25.014,24.642,22.586,32.064,34.043,29.622,23.308,32.434,28.874,34.108,34.768)
x.3 <- c( 0.593,0.976,0.214,0.475,-0.017,0.738,0.563,0.048,0.656,0.517,0.452,0.829,0.197,-0.073,0.642,-0.047,0.002,0.378,1.172,0.141)
dat <- data.frame(y = y, x = cbind(x.1,x.2,x.3))
library(quantreg)
library(np)
qr <- rq(y ~ ., data = dat)
(formula <- formula(qr))
> y ~ x.x.1 + x.x.2 + x.x.3
# Use the same formula for kernel quantile regression
bw <- npcdistbw(formula=formula) # Error
npqr <- npqreg(bws=bw)
但是我得到了错误:
Error in eval(attr(terms(formula), "variables"), environment(formula)) :
object 'x.x.1' not found
谢谢!
答案 0 :(得分:0)
不确定我是否理解正确,但我认为您正在尝试修改通话。检查它是否适合您:
y <- c(0.114,-0.360,-0.102,0.406,-0.363,-0.071,0.579,-0.265,0.290,0.312,0.063,0.181,-0.542,0.653,0.164,0.047,0.481,0.161,-0.481,0.57)
x.1 <- c(24.138,28.021,28.298,33.272,25.571,34.252,29.193,22.557,33.973,28.436,33.575,31.103,23.698,33.067,33.847,33.890,26.616,36.633,23.225,30.186)
x.2 <- c(28.909,25.487,33.834,23.615,33.429,24.235,33.436,30.065,22.448,25.014,24.642,22.586,32.064,34.043,29.622,23.308,32.434,28.874,34.108,34.768)
x.3 <- c( 0.593,0.976,0.214,0.475,-0.017,0.738,0.563,0.048,0.656,0.517,0.452,0.829,0.197,-0.073,0.642,-0.047,0.002,0.378,1.172,0.141)
dat <- data.frame(y = y, x = cbind(x.1,x.2,x.3))
library(quantreg)
library(np)
(my_expr <- quote(rq(y ~ ., data = dat))) # create reusable expression
qr <- eval(my_expr) # call qr
# modify call:
my_expr[[1]] <- quote(npcdistbw) # change fun
my_expr[[2]] <- formula(qr) # use formula of qr
print(my_expr)
bw <- eval(my_expr) # should work now
答案 1 :(得分:-1)
正如jogo所说,你必须指定数据:
bw <- npcdistbw(formula=formula,data=dat)