Как извлечь формулу из модели и использовать для другой модели в R? - PullRequest
0 голосов
/ 04 мая 2018

Я использую квантильную регрессию и хочу извлечь формулу, чтобы использовать ее в другой регрессии.

Мой пример:

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

Спасибо!

Ответы [ 2 ]

0 голосов
/ 04 мая 2018

Не уверен, правильно ли я вас понял, но я думаю, что вы пытаетесь изменить вызов. Проверьте, работает ли он у вас:

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
0 голосов
/ 04 мая 2018

Как сказал Джого, вы должны указать данные:

bw <- npcdistbw(formula=formula,data=dat)
...