Some arguments not working as intended in plotting a factor-smooth interaction in mgcv

458 Views Asked by At

I made a plot of a factor-smooth interaction constructed through s(... bs = "fs") in the mgcv package. It appears, however, that the xlim and main arguments (and a few other arguments) of plot.gam() are not working properly, although the same arguments for plotting other kinds of smooths work.

Here's an example.

Update: I modified the example to better reflect the issue I have (2 Dec., 2014).

library(mgcv)

# create toy data
set.seed(1)
d <- data.frame(
 x = runif(10 * 100),
 f = rep(paste0("f", 1:10), each = 100)
)
d$y <- 2 * d$x + 10 + rnorm(10 * 100)

# build a model
model <- gam(y ~ s(x) + s(x, f, bs = "fs"), data = d)

The following code, which produces the plot corresponding to the s(x) term above, limits the x axis and places the title, as intended.

plot(model, select = 1, xlim = c(0.4, 0.6), main = "Title")

However, the following code, which generates the plot corresponding to the s(x, f, bs = "fs") term above, does not limit the x axis or produce the title.

plot(model, select = 2, xlim = c(0.4, 0.6), main = "Title")

I suspect mgcv::plot.gam() fails to pass (some of) the arguments to mgcv:::plot.fs.interaction() or mgcv:::plot.mgcv.smooth() in some cases, but have not been able to figure out why it happens and how to fix it. I would appreciate any help.

1

There are 1 best solutions below

3
On

I admit this a bit of an brute force approach that basically does not "fix mgcv", but I think fixing mgcv is above my abilities.

with(d, plot(X=           seq(0,1,length=100), 
             gamPred.x2 = predict(model, 
                     newdata=data.frame(x1=mean(x1),
                                        x2=seq(0,1,length=100),
                                        f="f5")
        )))