0

This is related to this post.

I want to succinctly write a formula with many variables to estimate a non-parametric model, and I used the method provided by the post above. However, I found it does not work with np::npplregbw.

As a start, the formula in linear model works well

df<-data.frame(y=rnorm(10),x1=rnorm(10),x2=rnorm(10),x3=rnorm(10),x4=rnorm(10),
               x5=rnorm(10))

## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:4, sep="")
fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))

## This works
m <- lm(formula = fmla, data=df)

However, if we continue from the code above, and try to get a formula that is suitable for np:npplregbw

xnam2 <- paste("y ~ ", paste(xnam, collapse= "+"))
fmla <- as.formula(paste(xnam2, '|x5'))

## This returns an error
bw <- np::npplregbw(formula = fmla, data=df)

This returns an error "Error in npplregbw.formula(fmla, data = df) : invoked with improper formula, please see npplregbw documentation for proper use"

But the formula itself should work, as is seen below

print(fmla) ## Gets y ~ x1 + x2 + x3 + x4 | x5

## This also works
bw <- np::npplregbw(y ~ x1 + x2 + x3 + x4 | x5, data=df)

Can someone explain why this happens and how to solve this?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Tututuu
  • 105
  • 2

1 Answers1

0

The error comes from improper use of matchc.call npplregbw.formula, invoked by npplregbw`. The error is thrown in the first couple lines of code

npplregbw.formula <- function (formula, data, subset, na.action, call, ...) 
{
  mf <- match.call(expand.dots = FALSE)
  m <- match(c("formula", "data", "subset", "na.action"), 
    names(mf), nomatch = 0)
  mf <- mf[c(1, m)]
  if (!missing(call) && is.call(call)) {
    for (i in 1:length(call)) {
      if (tryCatch(class(eval(call[[i]])) == "formula", 
        error = function(e) FALSE)) 
        break
    }
    mf[[2]] <- call[[i]]
  }
  mf.xf <- mf
  mf[[1]] <- as.name("model.frame")
  mf.xf[[1]] <- as.name("model.frame")
  chromoly <- explodePipe(mf[["formula"]])
  if (length(chromoly) != 3) 
    stop("invoked with improper formula, please see npplregbw documentation for proper use")
  ...
}

Note a small example:

foo <- function(formula){
  mf <- match.call(expand.dots = FALSE)
  mf[["formula"]]
}
foo(fmla)
fmla # <=== output line

This is definitely something to report as an issue (opened here). The quick-fix is the one given by Roland in the comments

eval(bquote(np::npplregbw(formula = .(fmla), data=df)))

The better fix has to be done on the package end.

Oliver
  • 8,169
  • 3
  • 15
  • 37