pbreheny / visreg

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

Plotting a model that uses a smoother with knots specified by a variable #29

Closed DanielReedOcean closed 7 years ago

DanielReedOcean commented 7 years ago

First of all, kudos on an awesome package! I'm having a bit of a problem when specifying the number of knots in a smoother with a variable and then plotting the results with visreg. For example, the following seems to think kval, the variable for the number of knots used by the smoother, is a predictor and creates a plot for it:

# Load data
data("mtcars")

# Number of knots in the smoother function
kval <- 3

# Create a formula that looks like this: mpg ~ s(hp, k = kval)
formula.tmp <- reformulate(response = "mpg", termlabels = "s(hp, k = kval)")

# Run GAM
gam.res <- gam(formula.tmp, data = mtcars)

# Load library
library(visreg)

# Plot results 
visreg(gam.res)
##### This plots for two predictors: hp and kval. #####

If I hardcode the number of knots in the formula, this doesn't happen even though the models are essentially exactly the same.

# Create a formula that looks like this: mpg ~ s(hp, k = 3)
formula.tmp <- reformulate(response = "mpg", termlabels = "s(hp, k = 3)")

# Run GAM
gam.res <- gam(formula.tmp, data = mtcars)

# Plot results 
visreg(gam.res)
##### This plots for one predictor: hp. #####
pbreheny commented 7 years ago

Daniel,

Thanks for bringing this up...it didn't really rise high on my radar since you can always just request the specific plot you want with

visreg(gam.res, "hp")

But still, I agree, visreg was not doing the correct thing here -- fixed now, though.