seananderson / glmmfields

R package for Bayesian spatial and spatiotemporal GLMMs with possible extremes
50 stars 10 forks source link

predict does not return correct predictions if newdata specified #1

Closed seananderson closed 7 years ago

seananderson commented 7 years ago

I thought I was going crazy. Model fit comparisons between correctly and incorrectly specified just weren't making sense (the incorrect model was often better). Then occasionally in some scripts they would make sense. Then they would stop working again.

I finally discovered the reason. I think the predict.rrfield function is somehow resorting the data if newdata is specified. It works correctly otherwise.

There's a simple unit test here that is broken: https://github.com/seananderson/rrfields/blob/bea28dda856bf64afe0d1c187139ac81c412e530/tests/testthat/test-predict.R

The solution isn't obvious to me yet, but I'm still digging into it.

seananderson commented 7 years ago

predict.rrfield worked with newdata = model_fit$data and the only difference between the data coming out of the model fit and the simulated data was that the model fit data was also of class tbl.

When I wrap newdata in as.tbl everything miraculously works. I have no idea why. https://github.com/seananderson/rrfields/commit/110f9c1053c52c455e91c4bf775bcd5776e963a0

I'll test this with subsets of the data as well.