Closed raff-k closed 4 years ago
I am not a great expert of the regular expressions. But it seems that also the "_" in the variable name makes it complicate to adapt the grep-pattern... The following function works, but maybe it's a bit messy:
# the following command is now working
plot_gam(model = fit_gam_test, pred = "^x_0$")
# with...
gam_to_df <- function (model = NULL, pred = NULL)
{
plot_df <- no_plot(model)
# grep all names first
plot_df_names <- sapply(plot_df, function(x) x$xlab) %>%
gsub(pattern = "_", replacement = "", x = .)
# gsub possible bad characters in names (could be extended when further error occurs)
pred <- gsub(pattern = "_", replacement = "", x = pred)
set_pred <- which(grepl(pred, plot_df_names)) # switch plot_df with plot_df_names
df <- data.frame(x = plot_df[[set_pred]]$x, se_upr = plot_df[[set_pred]]$fit +
plot_df[[set_pred]]$se, se_lwr = plot_df[[set_pred]]$fit -
plot_df[[set_pred]]$se, y = plot_df[[set_pred]]$fit)
return(df)
}
Hi @raff-k, thanks for reporting.
If its just a grepl
problem returning two lines, it should be sufficient to make the grepl
call more robust. I don't think we need such a "big" function here.
Do you have time to do this?
@raff-k
Note that library(oddsratio, mgcv)
is not valid code, i.e. {mgcv} is not being loaded in this case.
Hello, I would like to use your functions to display GAMs smoothing function. Some of my variables have partial matching variable names such as "slp" and "slp_catch". It seems, that this causes an error inside your function. Here a reproducible example.