Error in contrast when creating a prediction map

44 views Asked by At

I have a dataframe consisting of the location of several individuals, each identified by an ID, and the corresponding values of some environmental variables, all continuous except for one that is factorial. I have created a glmer model and now I want to create a prediction map using this model and the variables.

The summary of my dataframe is as follow:

> summary(jackal.Data.rsf)
       X                 Y              Used                ID          elevation        
 Min.   :4548927   Min.   :2524750   Mode :logical   Alberto : 7733   Min.   :  -0.5151  
 1st Qu.:4570832   1st Qu.:2538306   FALSE:43960     Isabella: 6809   1st Qu.:  95.2394  
 Median :4583158   Median :2555639   TRUE :4396      Pepe    : 4114   Median : 172.2590  
 Mean   :4585547   Mean   :2555266                   Sergio  : 2167   Mean   : 289.8413  
 3rd Qu.:4597305   3rd Qu.:2571271                   Torre   :15070   3rd Qu.: 354.5094  
 Max.   :4635669   Max.   :2591750                   Trilli  :10923   Max.   :1869.0605  
                                                     Yama    : 1540                      
     slope       land_cover     dist_water        dist_road     
 Min.   :  0.0   1   : 4293   Min.   :  0.000   Min.   :   0.0  
 1st Qu.:242.0   2   :20206   1st Qu.:  7.071   1st Qu.: 100.0  
 Median :249.0   3   :14331   Median : 16.155   Median : 200.0  
 Mean   :242.2   4   : 8583   Mean   : 21.367   Mean   : 358.5  
 3rd Qu.:250.0   5   :  754   3rd Qu.: 31.048   3rd Qu.: 447.2  
 Max.   :250.0   7   :  171   Max.   :123.004   Max.   :4167.7  
                 NA's:   18                                     

The model is created using the function

> m2.glmer <- glmer (Used ~ scale(elevation) + scale(slope) + land_cover 
+                    + scale(dist_water) + scale(dist_road) + (1|ID), 
+                    data=jackal.Data.rsf, family = binomial(link="logit"),
+                    glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000)))

I then stack the rasters

> rasters <- stack(elevation, slope, land_cover, dist_water, dist_road)

But when I try to create the prediction map I get the error:

> predictions = raster::predict(object = rasters, model=m2.glmer, fun=predict)
Error in `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]]) : 
  contrasts apply only to factors

I have tried to look for the solution to this error but I can only find solutions regarding this error when it appears in the creation of the model, which is not my case.

0

There are 0 answers