gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

smooth_estimates fail for bi-variate conditioned factor-smooth #187

Closed Silviculturalist closed 2 years ago

Silviculturalist commented 2 years ago

Hi!

I'm attempting the following:

# There are 3 factors to mixture (eg 3 crops): A, B, C.
# There are 3 climate scenario factors.

bam2 <- mgcv::gam(Vha~s(Age,by=Mixture)+ # Expected Age decline of crop.
                   s(climate,Year,by=Mixture,bs="fs")+ # Factor smooth of climate and year captures accumulate effects during simulations of plots.
                   s(Plot,PlotReplicate,bs="re")+ #Random effects distinguishing plots.
                   Mixture, #Mixture intercepts.
                 data=combined_data,
                 family="gaussian",
                 method="REML")

bam2smooths <- gratia::smooth_estimates(bam2)

When I'm hit by a :

Error in seq.default(from = min(x, na.rm = TRUE), to = max(x, na.rm = TRUE), : 'length.out' must be a non-negative number

Grabbing by name or index for 1:3 works fine.

> gratia::smooths(bam2)
[1] "s(Age):MixtureA"               "s(Age):MixtureB"          "s(Age):MixtureC"             
[4] "s(climate,Year):MixtureA"      "s(climate,Year):MixtureB" "s(climate,Year):MixtureC"    
[7] "s(Plot,PlotReplicate)" 

I have gotten so far with the debug that I can point to something going wrong in the sequence:

smooth_estimates.gam LOC 26. When i hits 4L and tries to evaluate eval_smooth. it fails.

eval_smooth.fs.interaction LOC 9. model is a Large gam 59 elements, 27.8 MB. smooth is a Large fs.interaction (38 elements, 4.6 MB) by_var = "Mixture" data =NULL id= 4L

process_user_data_for_eval. LOC 4. data is NULL.

smooth_data LOC 51. mf = 54432 obs of 6 variables smooth_vars = chr[1:2] "climate" "Year" sm_dim = 1L n= 100 n_3d= 100 n_4d= 100

seq_per_dim LOC 17. Error is caused here by ..

out <- lapply(seq_along(vars), FUN = seq_min_max_wrapper, data = data, 
vars = vars, n = n_per_dim)
gavinsimpson commented 2 years ago

What is climate here? It sounds like (from what you wrote) this is a factor which would be wrong for the fs smooth which is s(x, f), i.e. continuous covariate first.

Silviculturalist commented 2 years ago

Worked a charm, many thanks!