Search code examples
predictmumin

Predict from model average results: MuMIn


Thanks in advance for all help.

I would like to produce a plot similar to the below from my model averaged results. This plot was produced from a single model using the effect() function in the effects package.

Desired plot outcome

To my knowledge this cannot be achieve from model averaged results from the model.avg() function so I have tried to achieve a similar result by first predicting my model averaged results and then creating a plot.

I have built two models from this data:

igm_20 <- glmmTMB(igm_pres ~  fRHDV2_arrive_cat + fseason + sage + save_ajust_abun + fseason*fRHDV2_arrive_cat + (1 | fsite), data = test, family = binomial)
igm_21 <- glmmTMB(igm_pres ~  fRHDV2_arrive_cat + fseason + sage + save_ajust_abun + fseason*fRHDV2_arrive_cat + sage*fRHDV2_arrive_cat + (1 | fsite), data = test, family = binomial)

I can average these models like so:

mod_ave_list_1 <- list(igm_20, igm_21)
mod_ave_1 <- model.avg(mod_ave_list_1, rank = AICc)
s1 <- summary(mod_ave_1)

I have then tried to make predictions from the model averaged results for each combination of fseason*fRHDV2_arrive_cat from the dataset provided, test:

a <- as.data.frame(c("Summer", "Autumn", "Winter", "Spring", "Summer", "Autumn", "Winter", "Spring"))
a$fRHDV2_arrive_cat <- c("Pre-arrival", "Post-arrival", "Pre-arrival", "Post-arrival", "Pre-arrival", "Post-arrival", "Pre-arrival", "Post-arrival")
colnames(a) <- c("fseason", "fRHDV2_arrive_cat")

predict(mod_ave_1, full = TRUE, backtransform = TRUE, newdata = a, se.fit = TRUE)

The predict function runs fine if I do not include newdata = a, consequently I believe the data frame I supply to newdata is not of the appropriate structure.

If someone is able to help me to structure newdata properly so that I can produce predictions for each combination of fseason*fRHDV2_arrive_cat it would be much appreciated? I beleive I can produce the desired plot once I have the predictions.

My situation is very similar to that described in another post (here), which remains unanswered. Above I have described my attempt to achieve a similar plot via another means. I also note that there are other similar posts, such as here; I have not found these useful to my situation.


Solution

  • Your newdata must include all the explanatory variables used by the model, which it currently does not.

    Variables required for prediction:

    all.vars(formula(mod_ave_1)[-2]) # extracts all names from formula minus response
    # [1] "fRHDV2_arrive_cat" "fseason"           "sage"              "save_ajust_abun"  
    

    Variables in the new data:

    colnames(a)
    # [1] "fseason"           "fRHDV2_arrive_cat"