0

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!

Phil
  • 7,287
  • 3
  • 36
  • 66
hm777
  • 3
  • 1
  • Does you model `avgm` only include the variable `x1`? – J.C.Wahl Sep 10 '20 at 14:42
  • @J.C.Wahl No, it includes variables x1 through to x12 – hm777 Sep 10 '20 at 14:47
  • Well, there is your answer. – Roland Sep 10 '20 at 14:49
  • Hi @Roland, do you mean that I should make a new model with only the variable 'x1' and predict from that? – hm777 Sep 10 '20 at 14:51
  • I'm not telling you what to do, @J.C.Wahl basically told you what causes the error. Personally, I think you are missing a model validation step and I don't understand why you even do model development if you don't have the necessary prediction data. – Roland Sep 10 '20 at 14:53
  • Sorry, I misunderstood your first comment. Thank you for the advice. Do you see a particular error here to make you suggest that? – hm777 Sep 10 '20 at 15:52

1 Answers1

0

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

J.C.Wahl
  • 1,394
  • 8
  • 15
  • Thank you for the answer. I can produce a graph using: plot(predict(avgm, dataset, backtransform = TRUE)) , but I am not sure how this shows the relationship between x1 and the response variable in particular. Sorry if I am misunderstanding something – hm777 Sep 10 '20 at 15:03
  • Could you try adding `predict(avgm, type = "terms", terms = "x1")`? Also, I would try to read the documentation for `predict.averaging` [here](https://cran.r-project.org/web/packages/MuMIn/MuMIn.pdf) – J.C.Wahl Sep 10 '20 at 15:13
  • That code works, and graphed into a different scatter plot than before, so I think I am getting closer now. I did read the predict.averaging documentation, but didn't fully understand it (I am fairly new to R), so I will read it again now that I have a better understanding and try to figure out exactly what has been produced by the code. Thanks very much for your help :) – hm777 Sep 10 '20 at 15:36