masurp / specr

Conducting and Visualizing Specification Curve Analyses
https://masurp.github.io/specr
GNU General Public License v3.0
67 stars 5 forks source link

np Package errors #38

Open cretecht opened 10 months ago

cretecht commented 10 months ago

Hello,

I'm running specr with the np package and encountering an issue. Here's the code

kde.reg <- function(formula, data){
  bw <- npregbw(formula=formula,
                data = data,
                nmulti = 5,
                ckertype = "epanechnikov", ukertype = "liracine",
                regtype="lc", bwmethod="cv.aic")
  npreg(bw)
}

test_specs <- setup(data = test_df,
                    y = c("y1", "y2"),
                    x = c("x1", "x2", "x3"),
                    model = "kde.reg"
                    )

summary(test_specs, rows = 50)
test_results <- specr(test_specs)

I've attached a snippet of the code error below for context. I'm familiar with the error message and have solved it before by selecting the data for the explanatory variables. However, in specr, selecting inside of the model function causes errors of its own. Would this mean that specr isn't compatible with np or is there a way around this issue?

npspecr_error

UPDATE: I've narrowed down the issue. In spec.R, at line 182, when it calls kde.reg the formula it passed in (formula = structure("y1 ~ x2 + 1", class = c("glue", "character"))) seems to be causing an error. I recreated the error by calling the npregbw with this formula format. When I replaced it with y1 ~ x2 + 1 as a standalone formula, the error was resolved. Even when including the structure(list...)) argument that spec.R creates. Although, I'm still unclear on how to resolve this issue.

UPDATE AGAIN: I was able to resolve the error by defining formula like formula=as.formula(formula), since specr defines it as a character when passed into the function. However, I'm running into a tidy error now. Working with tidy on that, but won't delete this for now.

cretecht commented 10 months ago

From tidy I've been redirected here, here's the code anew.

kde.reg <- function(formula, data){
  bw <- npregbw(formula=as.formula(formula),
                data = data,
                nmulti = 5,
                ckertype = "epanechnikov", ukertype = "liracine",
                regtype="lc", bwmethod="cv.aic")
  npreg(bw)
}

test_specs <- setup(data = test_df,
                    y = c("y1", "y2"),
                    x = c("x1", "x2", "x3"),
                    model = "kde.reg"
                    )

summary(test_specs, rows = 50)
test_results <- specr(test_specs)

I encounter the tidy error because specr calls tidy at some point. The traceback call has tidy.default(x, conf.int = TRUE), then the error follows it. Is there some way to resolve this issue?

masurp commented 8 months ago

Without having checked comprehensively, it seems that the np package is not supported by broom::tidy. Specr uses tidy to extract relevant parameters from the models. As it does this across almost the entire range of available models, it was obvious for us to implemented it in specr. That said, it means it breaks with any non-standard package.

Yet, that doesn't mean that you cannot work with specr. In fact, this is why I created the option to define your own extraction function (argument fun1 and fun2 in the setup() function). Here, you can define your own function and selectively extract whatever parameter, coefficient you are interested in. See e.g., this vignette for setting up such a custom function: https://masurp.github.io/specr/articles/invest-spec.html

If you provide a reproducible example, I can also take a stap at writing this function, but as I am not familiar with the np package, I cannot simple come up with one myself.

Best!