简体   繁体   English

无法从函数内的np包调用函数,可能是环境问题

[英]difficulty calling a function from np package within a function, possible environment issue

I am trying to use the npreg() function from the np package within another function. 我正在尝试在另一个函数中使用np包中的npreg()函数。 I am encountering an error related to environments. 我遇到与环境有关的错误。

npreg() is a function for non-parametric regression. npreg()是用于非参数回归的函数。 I am doing the estimates in two steps, first I estimate the bandwidth using npregbw() and then I can call npreg() on the estimated bandwith to obtain the regression estimates. 我分两步进行估算,首先使用npregbw()估算带宽,然后可以在估算的带宽上调用npreg()以获得回归估算。 Outside of a function call, I encounter no issues. 在函数调用之外,我没有遇到任何问题。 However, inside a function call the npreg() function does not seem to be able to work with the estimated bandwith. 但是,在函数内部调用npreg()函数似乎无法使用估计的带宽。 Reprex below: 重新说明如下:

x <- rnorm(20)
y <- 2*x + rnorm(20)
df <- data.frame(y, x)

pidtest <- function(outformula, data) {

  # estimate conditional density of outcome 
  np_lower_bw <- np::npregbw(outformula, data = data)
  np_lower <- np::npreg(np_lower_bw)
  np_lower
}

pidtest(y~x, df)

#> Error in eval(predvars, data, env): invalid 'envir' argument of type 'closure'

If I call the function just to estimate the bandwidth there is no issue 如果我仅调用函数来估计带宽,就没有问题

pidtest <- function(outformula, data) {

  # estimate conditional density of outcome 
  np_lower_bw <- np::npregbw(outformula, data = data)
  # np_lower <- np::npreg(np_lower_bw)
  # np_lower
  np_lower_bw
}

pidtest(y~x, df)

#> 
#> Regression Data (20 observations, 1 variable(s)):
#> 
#>                       x
#> Bandwidth(s): 0.3770171
#> 
#> Regression Type: Local-Constant
#> Bandwidth Selection Method: Least Squares Cross-Validation
#> Formula: y ~ x
#> Bandwidth Type: Fixed
#> Objective Function Value: 1.469502 (achieved on multistart 1)
#> 
#> Continuous Kernel Type: Second-Order Gaussian
#> No. Continuous Explanatory Vars.: 1

Likewise outside of a function call there is no issue: 同样,在函数调用之外也没有问题:

bws <- np::npregbw(y~x, df)
np::npreg(bws)

Regression Data: 20 training points, in 1 variable(s)
                     x
Bandwidth(s): 0.307494

Kernel Regression Estimator: Local-Constant
Bandwidth Type: Fixed

Continuous Kernel Type: Second-Order Gaussian
No. Continuous Explanatory Vars.: 1

I cannot figure out why this error occurs inside my function call nor how to circumvent it. 我无法弄清楚为什么此错误会在函数调用内发生,也无法解决。 I would like to embed this estimate within a function that is doing other things so am eager to figure out a way to make it work. 我想将此估算值嵌入正在执行其他操作的函数中,因此急于想出一种使之起作用的方法。

I cannot explain exactly why, but If you try this piece of code, it works: 我无法确切解释原因,但是如果您尝试这段代码,它会起作用:

x <- rnorm(20)
y <- 2*x + rnorm(20)
df <- data.frame(y, x)

pidtest <- function(outformula, data) {

  # estimate conditional density of outcome 

  np_lower_bw <- np::npregbw(as.formula(outformula), data = data)
  np_lower <- np::npreg(np_lower_bw)
  np_lower
}

pidtest("y~x", df)

See here for more about the topic: https://stat.ethz.ch/pipermail/r-help/2005-March/067109.html 请参阅此处以获取有关该主题的更多信息: https : //stat.ethz.ch/pipermail/r-help/2005-March/067109.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM