Why are the data points on my conditional plot different to those on a raw plot?

I’m running a model of population counts as a function of time with an interaction of the location. My models run fine and the fit looks good. However, when I compare a conditional effects plot:

plot(conditional_effects(nbglm_awb_default, prob = 0),
                 ask = FALSE,
                 points = T,
                 offset = T)

with a basic ggplot of the raw data

ggplot(mydata, aes(x = ndate, y = AWB)) + geom_point(aes(colour = CarcassPres)) +
  theme_bw() + geom_smooth(method = "lm") + facet_wrap( ~ NP)

the points look different, for instance, there are many points above 75 on the ggplot. What could account for this?

(Just to note Selous = Nyerere)

nbglm_awb_default<-
  brm(
    AWB ~  0 + Intercept + # this allows control of prior on the intercept
      ndate * NP + Season + CarcassPres +
      (1 | NP / StandardTransect) +
      offset(log(Tlength)),
    family = negbinomial,
    data = mydata,
    chains = 4,
    #control = list(adapt_delta = 0.999, max_treedepth = 15),
    prior = newprior_awb,
    save_pars = save_pars(all = TRUE)
  )

Here’s my data

mydata <- structure(list(AWB = c(7, 66, 15, 44, 22, 60, 45, 32, 30, 33, 
14, 0, 45, 39, 39, 24, 37, 66, 37, 60, 18, 3, 25, 13, 34, 38, 
58, 0, 12, 6, 33, 2, 34, 18, 75, 20, 4, 9, 15, 4, 0, 21, 50, 
24, 21, 9, 5, 87, 13, 43, 1, 19, 13, 1, 28, 56, 18, 42, 13, 2, 
53, 16, 37, 51, 79, 5, 49, 11, 34, 91, 30, 2, 0, 15, 3, 57, 5, 
18, 31, 14, 56, 72, 35, 94, 10, 45, 8, 29, 33, 34, 8, 53, 54, 
24, 5, 21, 11, 27, 83, 23, 24, 4, 10, 13, 17, 11, 51, 6), ndate = c(0, 
0, 0, 0, 0, 14, 14, 14, 14, 14, 14, 19, 19, 19, 19, 20, 20, 25, 
25, 25, 25, 25, 25, 32, 32, 33, 33, 33, 33, 33, 38, 38, 38, 38, 
38, 38, 39, 39, 39, 39, 39, 41, 41, 42, 42, 42, 42, 43, 43, 43, 
44, 46, 46, 46, 46, 51, 51, 51, 51, 51, 51, 54, 54, 54, 55, 56, 
56, 56, 58, 58, 61, 61, 61, 61, 61, 63, 63, 66, 66, 67, 67, 67, 
68, 68, 68, 68, 68, 71, 72, 73, 78, 78, 79, 79, 89, 89, 89, 90, 
90, 91, 91, 91, 96, 96, 96, 96, 97, 97), nyear = c(2013, 2013, 
2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021), NP = structure(c(1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 
2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 
1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 1L), .Label = c("Katavi", 
"Ruaha", "Selous"), class = "factor"), Season = c("Dry", "Dry", 
"Dry", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry", 
"Wet", "Wet", "Wet", "Wet", "Wet", "Wet", "Dry", "Dry", "Dry", 
"Dry", "Dry", "Dry", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", 
"Wet", "Wet", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry", 
"Dry", "Dry", "Dry", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", 
"Wet", "Wet", "Wet", "Wet", "Dry", "Dry", "Dry", "Dry", "Dry", 
"Dry", "Dry", "Dry", "Dry", "Dry", "Wet", "Wet", "Wet", "Wet", 
"Wet", "Wet", "Wet", "Dry", "Dry", "Dry", "Wet", "Dry", "Dry", 
"Dry", "Dry", "Dry", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", 
"Wet", "Wet", "Wet", "Wet", "Dry", "Dry", "Dry", "Wet", "Wet", 
"Wet", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", "Wet", 
"Wet", "Dry", "Dry", "Dry", "Dry", "Dry", "Dry"), CarcassPres = structure(c(1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("0", 
"1"), class = "factor"), StandardTransect = structure(c(4L, 3L, 
1L, 5L, 7L, 5L, 1L, 8L, 6L, 4L, 3L, 8L, 5L, 6L, 1L, 4L, 3L, 4L, 
3L, 5L, 1L, 8L, 6L, 8L, 6L, 5L, 1L, 8L, 6L, 1L, 5L, 5L, 1L, 6L, 
4L, 3L, 8L, 6L, 5L, 1L, 8L, 4L, 3L, 5L, 1L, 8L, 6L, 5L, 1L, 6L, 
8L, 5L, 1L, 8L, 6L, 4L, 3L, 5L, 1L, 8L, 6L, 3L, 4L, 10L, 2L, 
1L, 5L, 6L, 2L, 10L, 5L, 8L, 8L, 1L, 6L, 3L, 4L, 4L, 3L, 9L, 
2L, 10L, 5L, 1L, 7L, 5L, 7L, 2L, 10L, 9L, 4L, 3L, 10L, 2L, 5L, 
1L, 7L, 4L, 3L, 2L, 10L, 9L, 5L, 7L, 1L, 2L, 9L, 4L), .Label = c("Jongomero", 
"Kidai", "LakeChada", "LakeKatavi", "Lunda", "Magangwe", "Mbagi-Mdonya", 
"Mpululu", "Msolwa", "Mtemere"), class = "factor"), Tlength = c(35.2, 
86.7, 93, 75, 27.2, 74.4, 93, 10.3, 45.8, 35.2, 78.2, 10.3, 71, 
45.8, 93, 35.2, 63.9, 35.2, 77.9, 86.6, 93, 10.3, 45.8, 10.3, 
45.8, 68.9, 93, 10.3, 45.8, 93, 86.7, 90.5, 93, 45.8, 35.2, 81.6, 
10.3, 45.8, 88.2, 93, 10.3, 35.2, 64.6, 82.3, 93, 10.3, 45.8, 
77.9, 93, 45.8, 10.3, 90.3, 93, 10.3, 45.8, 35.2, 77.4, 87.5, 
93, 10.3, 45.8, 66, 35.2, 71.2, 85.7, 93, 87.5, 45.8, 85.5, 69.6, 
97.8, 10.3, 10.3, 93, 45.8, 86.6, 35.2, 35.2, 71.9, 77.9, 88.5, 
80, 85.2, 93, 56.1, 85.5, 56.1, 97.6, 81.8, 79.7, 35.2, 71.1, 
81.9, 53.8, 86.5, 68.7, 60.7, 78.7, 56.6, 66.9, 71.8, 79.2, 82.6, 
71.8, 92.4, 85.6, 78.5, 77.3)), row.names = c(NA, -108L), class = "data.frame")

Here’s my session info:

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_Ireland.1252  LC_CTYPE=English_Ireland.1252   
[3] LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C                    
[5] LC_TIME=English_Ireland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] sjPlot_2.8.6       sjstats_0.18.1     interactions_1.1.5 tidybayes_3.0.0    readxl_1.3.1      
 [6] brms_2.16.3        Rcpp_1.0.8.3       forcats_0.5.2      stringr_1.4.0      dplyr_1.0.9       
[11] purrr_0.3.4        readr_2.1.2        tidyr_1.2.0        tibble_3.1.6       ggplot2_3.3.5     
[16] tidyverse_1.3.2   

loaded via a namespace (and not attached):
  [1] backports_1.4.1      jtools_2.1.0         plyr_1.8.6           igraph_1.2.6        
  [5] splines_4.0.3        svUnit_1.0.6         crosstalk_1.1.0.1    rstantools_2.1.1    
  [9] inline_0.3.16        digest_0.6.27        htmltools_0.5.1.1    rsconnect_0.8.16    
 [13] fansi_0.4.1          magrittr_2.0.3       checkmate_2.0.0      googlesheets4_1.0.1 
 [17] tzdb_0.1.2           modelr_0.1.8         RcppParallel_5.0.2   matrixStats_0.57.0  
 [21] xts_0.12.1           prettyunits_1.1.1    colorspace_1.4-1     rvest_1.0.3         
 [25] ggdist_3.0.0         xfun_0.31            haven_2.5.1          callr_3.7.0         
 [29] crayon_1.5.1         jsonlite_1.7.2       lme4_1.1-25          zoo_1.8-8           
 [33] glue_1.6.2           gtable_0.3.0         gargle_1.2.0         emmeans_1.5.2-1     
 [37] sjmisc_2.8.5         V8_3.3.1             distributional_0.2.2 pkgbuild_1.3.1      
 [41] rstan_2.21.2         abind_1.4-5          scales_1.1.1         mvtnorm_1.1-3       
 [45] DBI_1.1.0            ggeffects_0.16.0     miniUI_0.1.1.1       performance_0.7.1   
 [49] xtable_1.8-4         diffobj_0.3.5        stats4_4.0.3         StanHeaders_2.21.0-6
 [53] DT_0.16              htmlwidgets_1.5.3    httr_1.4.2           threejs_0.3.3       
 [57] arrayhelpers_1.1-0   posterior_1.1.0      ellipsis_0.3.2       pkgconfig_2.0.3     
 [61] loo_2.4.1            farver_2.0.3         dbplyr_2.2.1         utf8_1.1.4          
 [65] labeling_0.4.2       effectsize_0.4.0     tidyselect_1.1.2     rlang_1.0.4         
 [69] reshape2_1.4.4       later_1.1.0.1        munsell_0.5.0        cellranger_1.1.0    
 [73] tools_4.0.3          cli_3.3.0            generics_0.1.0       sjlabelled_1.1.7    
 [77] broom_1.0.0          ggridges_0.5.2       fastmap_1.0.1        knitr_1.39          
 [81] processx_3.5.0       fs_1.5.0             pander_0.6.3         nlme_3.1-149        
 [85] mime_0.11            projpred_2.0.2       xml2_1.3.3           compiler_4.0.3      
 [89] bayesplot_1.7.2      shinythemes_1.1.2    rstudioapi_0.13      curl_4.3.2          
 [93] gamm4_0.2-6          reprex_2.0.2         statmod_1.4.35       stringi_1.5.3       
 [97] parameters_0.13.0    ps_1.4.0             Brobdingnag_1.2-6    lattice_0.20-41     
[101] Matrix_1.2-18        nloptr_1.2.2.2       markdown_1.1         shinyjs_2.0.0       
[105] tensorA_0.36.2       vctrs_0.4.1          pillar_1.6.4         lifecycle_1.0.1     
[109] bridgesampling_1.0-0 estimability_1.3     insight_0.13.2       httpuv_1.5.4        
[113] R6_2.5.0             promises_1.1.1       gridExtra_2.3        codetools_0.2-16    
[117] boot_1.3-25          colourpicker_1.1.0   MASS_7.3-53          gtools_3.8.2        
[121] assertthat_0.2.1     withr_2.4.3          shinystan_2.5.0      bayestestR_0.9.0    
[125] mgcv_1.8-33          parallel_4.0.3       hms_1.1.2            grid_4.0.3          
[129] coda_0.19-4          minqa_1.2.4          googledrive_2.0.0    shiny_1.5.0         
[133] lubridate_1.8.0      base64enc_0.1-3      dygraphs_1.1.1.6

To generate conditional effects for a given predictor, the conditional_effects function sets other predictors to their mean (if continuous) or reference level (if categorical). conditional_effects returns a list of the data frames containing these values. If you run, for example,

cond.eff = conditional_effects(nbglm_awb_default, prob = 0)  
str(cond.eff)

you can see the structure of the list of data frames generated by conditional_effects.

Regarding the points that are plotted when points=TRUE, the help for conditional_effects says:

In the created plots, only those points will be shown that correspond to the factor levels actually used in the conditioning, in order not to create the false impression of bad model fit, where it is just due to conditioning on certain factor levels.

If you want to condition on other values (besides means and reference levels), you can use the conditions argument to change the values that conditional_effects conditions on.

The help for conditional_effects, in particular, the details and value sections and the description of the conditions argument, provides additional information that might be useful.

1 Like