Search code examples
rglmpredict

Plotting prediction from averaged GLM model: Error in eval(predvars, data, env) : object 'x3' not found


I have produced an averaged GLM model (to find the habitat preference of a species), and I want to graph the shape of the relationship for each of the most important variables, “x1” and “x3” (understorey cover and canopy cover), against my response variable, “species” (species presence). I have been using the “predict”(predict.averaging) function, but I keep running into the same error:

Error in eval(predvars, data, env) : object 'x3' not found

More details and code:

My dataset, “data.csv”, is a table with 13 rows. The first 12 rows are scaled habitat variables (10 continuous, 2 categorical), named x1-x12. Row 13 is the response variable – species presence/absence (1 or 0). Here is my code:

library(MuMIn)

dataset <- read.csv(file = 'data.csv', stringsAsFactors = FALSE)

options(na.action = "na.fail")

m1 <- glm(species ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12, data=Dataset, family=binomial())

ms1 <- dredge(m1) #Running different model combinations

d2subset <- get.models(ms1, subset = delta < 2) #Models with delta AIC <2 are selected.

avgm <- model.avg(d2subset) #DeltaAIC<2 models are averaged.

summary(avgm) #Shows 'x1' to be the most significant variable.

The following code, attempting to predict from the averaged model, causes an error:

predict(avgm, data.frame(dataset$x1), se.fit = TRUE, type = "link", backtransform = TRUE, full = TRUE)

Errors produced:

Error in predict.averaging(avgm, dataset$x1), se.fit = TRUE, :

'predict' for models '2211', '163', '147', '179', '2227', '183', '131', '2195', '167', '2275', '227', '243', '211', '148', '1171', '435', '151', '3235', '247', '2215', '155', '2231', '659', '2291', '2219' and '171' caused errors.

In addition: There were 26 warnings (use warnings() to see them)

Warning messages: 1: In eval(predvars, data, env) : object 'x3' not found

2-13: In eval(predvars, data, env) : object 'x3' not found

14: In eval(predvars, data, env) : object 'x5' not found

15-26: In eval(predvars, data, env) : object 'x3' not found

I have looked into this a lot, but I still don't understand where the error comes from or how to work around it. I would be very grateful for any suggestions. Thank you!


Solution

  • Since the model includes covariates x1-x12 you need to include all variables for predict, not just x1.