I am trying to model the difference between the pixel values of two regions in a set of images. Each image is different and contains the pair of regions.

My question is about how to take into account the spatial autocorrelation in each region.

I was thinking to fit a GAM like the following:

mdl <- gam(intensity ~ region_id + s(row, col, by=image_id), ...)

I am not sure this is the correct way to capture the difference between the pixel intensities in the two regions, because the images are different.

Another approach I was considering is using an mrf for the spatial component. To make the image spatially separated, I was thinking to assign a coded id to each pixel that would be unique for each image (e.g. image1_1, image1_2, ..., image2_1, ...) and then define the set of neighbors within each image only.
In this way, I would have:

mdl <- gam(intensity ~ region_id + s(pixel_id, bs='mrf', xt = list(nb = neighbors)), ...)

EDIT: My question is probably related to this one:

GAM: 2D factor smooth with uneven sampling in x*z space across factor levels (R, mgcv)

The difference is that I have spatial data (image) and the pixel coordinates are not related between the images.

EDIT 2: The dataset consists of low-resolution measures from tissue sections (they can be seen as images). I'd like to compare the intensity of the response (pixels) between two ROIs from each image. The set of images can be considered a set of biological replicates. I was thinking to apply a CAR model, but I'd like to know if I can fix the issue of non-independence of neighbor pixels with a GAM.

  • $\begingroup$ what are those images of? $\endgroup$
    – rep_ho
    Apr 16, 2021 at 11:32
  • $\begingroup$ These are tissue images, they all come from different donors. I'm testing if the two regions of interest have different pixel intensities. $\endgroup$
    – piplustwo
    Apr 16, 2021 at 13:05
  • $\begingroup$ another thing to consider is that GAM will assume that the spatial autocorrelation is smooth, which is probably not for tissues $\endgroup$
    – rep_ho
    Apr 16, 2021 at 14:37
  • $\begingroup$ I think it should be ok if I do model it as an MRF. I've seen this done in spatial data, with the only difference that either there is only one spatial distribution, or the map is the same (for instance, spatio-temporal data). $\endgroup$
    – piplustwo
    Apr 16, 2021 at 15:50
  • 1
    $\begingroup$ The variation in intensity could be spatially smooth or at least well approximated by a process that is spatially smooth) with the random component giving the noisy pixelated data we observe. Simon Wood has an example in his GAM book of smoothing fMRI data and testing whether their are hemispheric differences in the spatial pattern of pixel intensities. In principle there is no problem assuming a spatially smooth mean with noise. $\endgroup$ Apr 16, 2021 at 20:08

1 Answer 1


I am not sure if you need to take the spatial autocorrelation into account in this problem. One possible way to test that difference is to calculate the average difference in each region in each image, and then perform a paired t-test or some other paired test to check if average differences between regions are significant. You take into account that the region intensities are coming from the same image by using a paired test.

  • $\begingroup$ I see. But in that case, am I not dropping a lot of information about the variance in each region? $\endgroup$
    – piplustwo
    Apr 16, 2021 at 13:04
  • 1
    $\begingroup$ you will keep the information about the variance of the average between images, but you won't keep the information about the variance of pixel intensities within a region. I don't know if this is an issue, or if GAM would help $\endgroup$
    – rep_ho
    Apr 16, 2021 at 13:43
  • $\begingroup$ I've added the link to (I think) a similar question. $\endgroup$
    – piplustwo
    Apr 16, 2021 at 14:23
  • $\begingroup$ another possibility is to bootstrap it, i.e., bootstrap the individual images, and calculate whatever test statistics you want from them. Spatial autocorrelation will be taken into account since you are bootstrapping whole images. You can also come up with some permutation test, which we often do in neuroimaging for very similar problems, however, we usually not test two ROI against each other, so I am not aware for a specific method for that $\endgroup$
    – rep_ho
    Apr 16, 2021 at 17:03
  • $\begingroup$ Yes, that may work. I’ll try with bootstrap. Another thing I was thinking is doing a block permutation test between the ROIs within each image. $\endgroup$
    – piplustwo
    Apr 16, 2021 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.