1010# ' @param xlim x limits for the contour plot, will be set to data limits +- 5\% if not specified
1111# ' @param ylim y limits for the contour plot, will be set to data limits +- 5\% if not specified
1212# ' @param xylim x and y limits for the contour plot
13- # ' @param fit Method to fit a model with. Current options are laGP (default)
14- # ' and mlegp . laGP is faster but might cause trouble.
13+ # ' @param fit Method to fit a model with. Current options are laGP (default),
14+ # ' mlegp, gam (uses mgcv), and locfit . laGP is faster but might cause trouble.
1515# ' @param gg If TRUE, will use ggplot2 by calling gcf_func
1616# ' @param show_points Whether the input data points should be shown on the plot.
1717# ' If missing, is TRUE when there are more than 300 points.
2020# ' @param ... passed to cf_func
2121# ' @importFrom utils capture.output
2222# ' @importFrom stats predict
23+ # ' @importFrom rmarkdown html_vignette
24+ # ' @importFrom stats lm
2325# ' @examples
2426# ' x <- runif(20)
2527# ' y <- runif(20)
@@ -58,19 +60,19 @@ cf_data <- function(x, y=NULL, z=NULL,
5860 }
5961 # Check fit name given
6062 if (fit == " " ) {
61- if (length(x ) > 200 ) {
63+ if (length(x ) > 200 && requireNamespace( " locfit " , quietly = TRUE ) ) {
6264 fit <- " locfit"
6365 message(" Fitting with locfit since n > 200" )
6466 } else {
6567 fit <- " lagp"
66- message(" Fitting with laGP since n <= 200" )
68+ message(" Fitting with laGP since n <= 200 (or locfit not available) " )
6769 }
6870 }
6971 # Fits a Gaussian process model that interpolates perfectly, i.e., no smoothing
70- if (fit == " mlegp" ) {
72+ if (fit == " mlegp" && requireNamespace( " mlegp " , quietly = TRUE ) ) {
7173 co <- capture.output(mod <- mlegp :: mlegp(X = data.frame (x ,y ),Z = z ,verbose = 0 ))
7274 pred.func <- function (xx ) {mlegp :: predict.gp(mod ,xx )}
73- } else if (fit %in% c(" lagp" )) {
75+ } else if (fit %in% c(" lagp" ) && requireNamespace( " laGP " , quietly = TRUE ) ) {
7476 X <- data.frame (x , y )
7577 da <- laGP :: darg(list (mle = TRUE ), X = X )
7678 ga <- laGP :: garg(list (mle = TRUE ), y = z )
@@ -80,15 +82,15 @@ cf_data <- function(x, y=NULL, z=NULL,
8082 dab = da $ ab , gab = ga $ ab , verb = 0 , maxit = 1000 )
8183
8284 pred.func <- function (xx ) {laGP :: predGPsep(mod1 , xx , lite = TRUE )$ mean }
83- } else if (fit == " locfit" ) {
85+ } else if (fit == " locfit" && requireNamespace( " locfit " , quietly = TRUE ) ) {
8486 # browser()
8587 X <- data.frame (x , y , z )
8688 lfmod <- locfit :: locfit(z ~ x + y , data = X , family = family )
8789 pred.func <- function (xx ) {
8890 # browser()
8991 predict(lfmod , data.frame (x = xx [,1 ], y = xx [,2 ]))
9092 }
91- } else if (fit == " gam" ) {
93+ } else if (fit == " gam" && requireNamespace( " mgcv " , quietly = TRUE ) ) {
9294 # browser()
9395 X <- data.frame (x = x , y = y , z = z )
9496 gammod <- mgcv :: gam(z ~ te(x , y ), data = X , family = family )
@@ -98,7 +100,15 @@ cf_data <- function(x, y=NULL, z=NULL,
98100 predict(gammod , data.frame (x = xx [,1 ], y = xx [,2 ]), type = ' response' )
99101 }
100102 } else {
101- stop(paste0(" fit is unknown" ))
103+ warning(paste0(" Defaulting to use LM for fit. This is bad. Choose a better" ,
104+ " option for fit and ensure the package is installed." ))
105+ X <- data.frame (x = x , y = y , z = z )
106+ lmmod <- lm(z ~ x + y + x * y , data = X )
107+ pred.func <- function (xx ) {
108+ # browser()
109+ predict(lmmod , data.frame (x = xx [,1 ], y = xx [,2 ]), type = ' response' )
110+ }
111+ # stop(paste0("fit is unknown, or the requested package is not installed"))
102112 }
103113
104114 minx <- min(x );maxx <- max(x );miny <- min(y );maxy <- max(y )
0 commit comments