The plots of residuals contain a gap

39 views
Skip to first unread message

Auman Chan

unread,
May 23, 2025, 5:13:54 AM5/23/25
to R-inla discussion group
Hi 

I built a model included spatial  effect, and the residuals plot looks strange:
#Original data
aggreData <- weight_arraData %>%
  stats::aggregate(cbind(log_PF,log_FR,log_SE,
                         log_TE, HMI_val, dem_val, treeCov_val, tmp_val,
                                       prep_val, centLon,centLat, median_SNR)~
                     year+month+Birdtree_name+square, mean) %>%
  mutate(weight_SNR = case_when(median_SNR<10 ~median_SNR^2,
                                median_SNR>=10 ~10^2),
         weight = weight_SNR/mean (weight_SNR),
         ID = row_number())

#Spatial effect
data_sf <- st_as_sf(aggreData,
                    coords = c("centLon","centLat"),crs = 4326,agr = "constant")
st_crs(data_sf)
gal <- dnearneigh(data_sf,0,1.5)

spa_PF <- formula(log_FR ~  HMI_val + dem_val + treeCov_val + tmp_val +
                   prep_val +
                   f(ID, model = 'besag',graph = gal, scale.model = T) +
                   f(Birdtree_name, model = 'iid') +
                   f(year, model = 'iid')
)


spa.model <- inla(formula = spa_FR   ,
                         data= aggreData  ,family = family,
                         weights = aggreData$weight,
                         control.predictor=list(compute=TRUE),
                         control.compute = list(dic=T,waic=T,cpo=T,
                                                return.marginals.predictor=T,
                                                residuals =T))

spaPhy_res_FR <- data.frame(res = spaPhy.model_FR1$residuals,
                         med_SNR = phylo_aggreData$median_SNR)

plot(y= spa_res_FR$deviance.residuals, x= spa_res_FR$med_SNR)


The plots of the residuals displays a gap:
plot_zoom_png.png

 Are there any mistakes or errors with my model? 

Thanks!

Helpdesk (Haavard Rue)

unread,
May 25, 2025, 12:25:04 PM5/25/25
to Auman Chan, R-inla discussion group
it does not say what 'family' is...

but if I do

n=10^3; 
x=rnorm(n); 
eta=1+x; 
y=rpois(n,exp(eta)); 
r=inla(y~1+x,data=data.frame(y,x),family="poisson",
control.compute=list(residuals=TRUE))

and then

plot(r$residuals$deviance.residuals)

I also get a gap, so its seems like a feature for discrete response (I guess
with a non-zero prob for y=0)
> plot_zoom_png.png
>
>  Are there any mistakes or errors with my model? 
>
> Thanks!
> --
> You received this message because you are subscribed to the Google Groups "R-
> inla discussion group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to r-inla-discussion...@googlegroups.com.
> To view this discussion, visit
> https://groups.google.com/d/msgid/r-inla-discussion-group/4c1995d2-9ce4-4cd7-855c-b2f7dbae2c40n%40googlegroups.com
> .

--
Håvard Rue
he...@r-inla.org
Screenshot from 2025-05-25 17-23-52.png

Auman Chan

unread,
May 26, 2025, 3:10:55 AM5/26/25
to R-inla discussion group
Sorry about that I did not provide the name of family. I used Gaussian family in my analysis, and my data have been log- transformed and scale before import to the inla. 

But when I only added fixed effect in my model:

```
fix_PF <- formula(log_PF ~  HMI_val + dem_val + treeCov_val + tmp_val +
                    prep_val)

check.model_PF <- inla_model(formula = fix_PF, data = aggreData,
                             family = "gaussian", weight = NULL)

plot(check.model_PF$residuals$deviance.residuals)
```

And then I got this plot without any significant gap:

plot_zoom2.png

But when I simulated some data to test a simple model:

```
n=10^3
x=rnorm(n)
y=10+5*x

model <- inla(y~ x, family = "gaussian",data = data.frame(x,y),
              control.compute=list(residuals=TRUE))

summary(model)
 plot(model$residuals$deviance.residuals)
```

The residual plot of this model is:
plot_zoom3.png

It looks like that my data and the gaussian family do not include the discrete response, so maybe this gap is not affected by yhe data distribution. Probably I need to confirm the algorithm of residuals in the inla function.

Håvard Rue

unread,
May 26, 2025, 4:37:05 AM5/26/25
to Auman Chan, R-inla discussion group

I would then guess its something in the log-transform that does this, and its a
feature not bug


On Mon, 2025-05-26 at 00:10 -0700, Auman Chan wrote:
> Sorry about that I did not provide the name of family. I used Gaussian family
> in my analysis, and my data have been log- transformed and scale before import
> to the inla. 
>
> But when I only added fixed effect in my model:
>
> ```
> fix_PF <- formula(log_PF ~  HMI_val + dem_val + treeCov_val + tmp_val +
>                     prep_val)
>
> check.model_PF <- inla_model(formula = fix_PF, data = aggreData,
>                              family = "gaussian", weight = NULL)
>
> plot(check.model_PF$residuals$deviance.residuals)
> ```
>
> And then I got this plot without any significant gap:
>
> plot_zoom2.png
>
> But when I simulated some data to test a simple model:
>
> ```
> n=10^3
> x=rnorm(n)
> y=10+5*x
>
> model <- inla(y~ x, family = "gaussian",data = data.frame(x,y),
>               control.compute=list(residuals=TRUE))
>
> summary(model)
>  plot(model$residuals$deviance.residuals)
> ```
>
> The residual plot of this model is:
hr...@r-inla.org
Reply all
Reply to author
Forward
0 new messages