pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
62 stars 18 forks source link

end of input error with extra () when using poly #52

Closed jebyrnes closed 3 years ago

jebyrnes commented 6 years ago

I was recently working on some polynomial regressions and ran into the oddest error. Consider the following:

library(tidyverse)
library(ggplot2)
library(visreg)

#some fake data
set.seed(35)
my_data <- data.frame(x1 = runif(100,-50,50), x2 = runif(100, -50, 50)) %>%
  mutate(y = rnorm(100, 0.001*x2*(x1 - x1^2), 100))

Now, let's fit a model, but in one case, we'll wrap poly with an extra set of () - this happened because initially I had been working with an uncentered polynomial (x + I(x^2))

#fit a model
mod <- lm(y ~ x2*poly(x1,2), data=my_data)
mod2 <- lm(y ~ x2*(poly(x1,2)), data=my_data)

now

visreg(mod, xvar = "x1", by = "x2", gg=TRUE)

works fine, but

visreg(mod2, xvar = "x1", by = "x2", gg=TRUE)

produces

Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:0: unexpected end of input
1: ~ x2 + (x1

This is particularly odd, as the following works

mod3 <- lm(y ~ x2*(x1 + I(x1^2)), data=my_data)
visreg(mod3, xvar = "x1", by = "x2", gg=TRUE)

Thoughts?

pbreheny commented 6 years ago

OK, good to know. The basic issue is that visreg needs to parse the model formula to figure out what the covariates are, and I apparently need some tweaks to the regular expression parsing, since the current version is getting confused by the extra parentheses.

jebyrnes commented 6 years ago

Thanks! And thanks again for an awesome package!

pbreheny commented 3 years ago

Not exactly sure when this was fixed, but this is fixed now.