I'm fitting an mgcv::gam model over a map using a formula likeP ~ s(X,Y) term where X and Y are the geographic coordinates of the data. visreg2d(model, "X","Y") does a great job of making a plot of the fitted smoother, but the X and Y range only extend to the min-max of X and Y.
What I want to do is extend that to cover a geographic border that extends outside the range of the X and Y data, in order to then crop it to the border to make a neat map. Yes, I know extrapolation outside the data like this is probably a bad idea but with consideration of errors etc its not a problem...
Currently the ranges are set in visreg:::setupV2:
xx <- if (is.factor(x))
factor(levels(x), levels = levels(x))
else seq(min(x), max(x), length = nn)
yy <- if (is.factor(y))
factor(levels(y), levels = levels(y))
else seq(min(y), max(y), length = nn)
and as a quick hack I modified this with an extra parameter specifying an extra offset to subtract from the minimum value and add to the maximum. Not very nice but works for me.
Ideally I might want to specify the expanded xydf grid directly:
xydf <- as.data.frame(expand.grid(xx, yy))
since I might want to do a grid with precise, eg 1000x1000, step size. At some point it might just be easier for me to write the code to return the model predictions on the grid without using visreg, but being able to specify variable ranges for plotting directly in the visreg functions might be generally useful for others too.
I'm fitting an
mgcv::gammodel over a map using a formula likeP ~ s(X,Y)term where X and Y are the geographic coordinates of the data.visreg2d(model, "X","Y")does a great job of making a plot of the fitted smoother, but the X and Y range only extend to the min-max of X and Y.What I want to do is extend that to cover a geographic border that extends outside the range of the X and Y data, in order to then crop it to the border to make a neat map. Yes, I know extrapolation outside the data like this is probably a bad idea but with consideration of errors etc its not a problem...
Currently the ranges are set in
visreg:::setupV2:and as a quick hack I modified this with an extra parameter specifying an extra offset to subtract from the minimum value and add to the maximum. Not very nice but works for me.
Ideally I might want to specify the expanded
xydfgrid directly:since I might want to do a grid with precise, eg 1000x1000, step size. At some point it might just be easier for me to write the code to return the model predictions on the grid without using
visreg, but being able to specify variable ranges for plotting directly in thevisregfunctions might be generally useful for others too.