Let say I've a dataframe as :
image_id image_group group x y nn
I've 20 images defined by image_id ; each image belongs to a group G1 or G2 defined in the image_group column. Eeach image is a list of points from different groups (e.g. A, B and C) defined by their coordinates x and y. nn represents the number of neighbour of the point in a predefined radius e.g. r=100.
My goal is to compare two groups of points by their minimal distances. As an example A vs B : I compute for each point of A : the distance to all points of B and report the minimal distance. Thus if I've 100 A points it will report 100 distances.
Using these distances I want to compare group G1 vs G2 and see if A vs B distances are different.
My idea was to use a linear mixed model and use nn as a covariate but not sure if it should be used as fixed or random effect.
In R (for the example I generate a dist column to simulate the distance between points. Here group G1 has a lower number of neihgbour compared to G2.
require(lme4)
require(lmerTest)
set.seed(123)
dat <-
data.frame(
image_id = c(rep(1:5,1000),rep(6:10,1000)),
image_group = c(rep("G1",1000),rep("G2",1000)),
dist = rnorm(2000,mean=50,sd=10),
nn = c( round(rnorm(1000,mean = 20,sd=5)),round(rnorm(1000,mean=30,sd=5))))
# version without using nn as covariate
mixed.lmer <- lmer(dist ~ image_group + (1|image_id), data = dat)
# version with nn as random effect
mixed.lmer2 <- lmer(dist ~ image_group + (1|image_id) + (1|nn), data = dat)
# version with nn as fixed effect
mixed.lmer3 <- lmer(dist ~ image_group + nn + (1|image_id) , data = dat)
anova(mixed.lmer)
anova(mixed.lmer2)
anova(mixed.lmer3)
Hereby the results
> anova(mixed.lmer)
Type III Analysis of Variance Table with Satterthwaite's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
image_group 173.41 173.41 1 9998 1.733 0.1881
> anova(mixed.lmer2)
Type III Analysis of Variance Table with Satterthwaite's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
image_group 90.472 90.472 1 1793.8 0.9183 0.3381
> anova(mixed.lmer3)
Type III Analysis of Variance Table with Satterthwaite's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
image_group 385.83 385.83 1 9997 3.8563 0.04959 *
nn 213.12 213.12 1 9997 2.1300 0.14447
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Which one will be the most suitable ? In this simulated example, results can be really different depending on the model. As number of neighbours can obviously bias the minimal distance calculation (More points in the vicinity -> more chance to have a point of group B close )
Thank you the help and comments
EDIT
Following @Doctor Milt advice I used a gam
Here's the plot of the model
model4 <- gam(dmin ~ image_group + s(nn) + s(image_id, bs = "re"), data = dat)
plot(model4)
nn
a numeric variable? Inmixed.lmer2
you're treating it as a categorical variable, which doesn't make sense to me. $\endgroup$mixed.lmer3
), so maybe try a spline? $\endgroup$