cplot() for GLM with poly() term
Closed this issue · 11 comments
First off, thanks for this package!!
I'm trying to run cplot() on a GLM with one polynomial term coded with poly() (from the stats package). I get the error "Error in eval(expr, envir, enclos) : could not find function "poly". The x term in cplot here is Easting, and that's the term that I've used poly() for in the GLM (ie, glm(response ~ poly(Easting, 3))
Any ideas?
John
Can you run traceback() after the error and include the full output?
11: eval(expr, envir, enclos)
10: eval(predvars, data, env)
9: model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels)
8: model.frame(Terms, newdata, na.action = na.action, xlev = object$xlevels)
7: predict.lm(object, newdata, se.fit, scale = residual.scale, type = ifelse(type ==
"link", "response", type), terms = terms, na.action = na.action)
6: predict.glm(model, newdata = out[[i]], type = type, se.fit = TRUE,
...)
5: predict(model, newdata = out[[i]], type = type, se.fit = TRUE,
...)
4: prediction.glm(model = object, data = tmpdat, type = type, level = level)
3: prediction(model = object, data = tmpdat, type = type, level = level)
2: cplot.glm(trans_stems_2015_cubic.glm, x = "Easting", data = trans_stems_2015)
1: cplot(trans_stems_2015_cubic.glm, x = "Easting", data = trans_stems_2015)
The model is:
glm(StemHerb ~ poly(Easting, 3) * Round * Source + Easting:Round:Transect, data = trans_stems_2015, family = binomial(link = logit))
Sorry for the pestering, but do you have an idea of what's going on here? Think there's a hack / workaround? Totally understand you're doing this on your free time, so no response needed, but if you thought you knew offhand what might be going on that could maybe help me solve it.
@jwbenning Sorry, I'd forgotten about this - I think if you just try prefixing poly()
with stats::poly()
it will work. Let me know if not.
Indeed, in a reproducible example, I get:
x <- glm(Sepal.Length ~ poly(Sepal.Width, 3), data = iris)
margins(x)
## Error in poly(Sepal.Width, 3, coefs = list(alpha = c(3.05733333333333, :
## could not find function "poly"
x <- glm(Sepal.Length ~ stats::poly(Sepal.Width, 3), data = iris)
margins(x)
## Average marginal effects
## glm(formula = Sepal.Length ~ stats::poly(Sepal.Width, 3), data = iris)
##
## Sepal.Width
## -0.1116
cplot(x, "Sepal.Width")
I'm still having trouble with this, unfortunately. When I run your example, I get the error:
> cplot(x, "Sepal.Width")
Error in `[.data.frame`(data, , c(nnames, fnames2), drop = FALSE) :
undefined columns selected
Traceback gives:
6: stop("undefined columns selected")
5: `[.data.frame`(data, , c(nnames, fnames2), drop = FALSE)
4: data[, c(nnames, fnames2), drop = FALSE]
3: check_factors(object, data, xvar = xvar, dx = dx)
2: cplot.glm(x, "Sepal.Width")
1: cplot(x, "Sepal.Width")
Can you give me the output of sessionInfo() ?
Sure! I think that must be it -- though I tried updating all packages to no avail...though looking at this now I should update R itself...
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] margins_0.3.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.15 lubridate_1.7.2 lattice_0.20-34 tidyr_0.8.0 class_7.3-14 assertthat_0.2.0
[7] ipred_0.9-6 psych_1.7.8 foreach_1.4.4 R6_2.2.2 plyr_1.8.4 stats4_3.3.2
[13] ggplot2_2.2.1 rlang_0.1.6 lazyeval_0.2.1 caret_6.0-78 geosphere_1.5-7 raster_2.6-7
[19] kernlab_0.9-25 rpart_4.1-10 Matrix_1.2-7.1 RStoolbox_0.1.10 splines_3.3.2 rgdal_1.2-16
[25] CVST_0.2-1 ddalpha_1.3.1.1 gower_0.1.2 stringr_1.2.0 foreign_0.8-67 munsell_0.4.3
[31] broom_0.4.3 compiler_3.3.2 pkgconfig_2.0.1 mnormt_1.5-5 dimRed_0.1.0 rgeos_0.3-26
[37] nnet_7.3-12 tidyselect_0.2.3 tibble_1.4.2 prodlim_1.6.1 DRR_0.0.3 codetools_0.2-15
[43] RcppRoll_0.2.2 XML_3.98-1.9 dplyr_0.7.4 withr_2.1.1 MASS_7.3-44 recipes_0.1.2
[49] ModelMetrics_1.1.0 grid_3.3.2 nlme_3.1-128 gtable_0.2.0 magrittr_1.5 scales_0.5.0
[55] stringi_1.1.6 reshape2_1.4.3 doParallel_1.0.11 bindrcpp_0.2 sp_1.2-7 timeDate_3042.101
[61] robustbase_0.92-8 lava_1.6 prediction_0.2.0 iterators_1.0.9 tools_3.3.2 glue_1.2.0
[67] DEoptimR_1.0-8 purrr_0.2.4 sfsmisc_1.1-1 parallel_3.3.2 survival_2.41-3 colorspace_1.3-2
[73] bindr_0.1
You need the development version of margins from Github. Please reinstall, restart R and try again. We're at 0.3.18 at the moment.
It worked!! Thanks so much.