Spearman correlation coefficient with repeated measures

I have a data set where 2 continuous variables(bio markers) are being measured a variable amount of times for each subject. Some subjects have 1 measurement while others have towards 100. The data is from an observational study of subjects under no intervention and began out as a single measurement however it has changed setup and is now a longitudinal study with drop outs. It is expected that there is a nonlinear correlation between the markers which at the same time are highly influenced by the lifestyle of the subject which is why it is necessary to use a repeated measurement setup.

I would like to calculate something resembling a repeated measures Spearman correlation coefficient. Is this possible in a reasonable way? Something which approaches the ordinary Spearman coefficient when number of measures per subject approaches 1.

(If a solution can be posted in R I will be happy but I can implement a general outline if necessary). Right now my only idea is to collapse each subject by taking the mean or calculate a Spearman correlation coefficient for each subject and then collapse with a mean.

Here is a synthetic data example where I have added some correlation coefficients. spearman_mean is calculating a Spearman for each subject an calculating the mean. mean_spearman is calculating a subject mean and then Spearman. rmcorr is a linear mixed model based approach which resembles a Pearson coefficient (as far as I understand). If a "real" repeated measurement Spearman is not available then if you can give me some pointers towards a best practice it would be really helpful.

library(dplyr)
library(ggplot2)
N = 100
set.seed(1234)
df <- data.frame(id_num = sample(1:3, N, replace = T, prob = c(0.1,0.5,0.4)),
var1 = rnorm(N, mean = 5, sd = 0.5)) %>%
mutate(id = factor(LETTERS[id_num]),
var2 = exp(1/sqrt(id_num)*var1)) %>%
arrange(id)
df$$var2 <- df$$var2 + rnorm(N, sd = 3)

df %>%
ggplot(aes(x = var1, y = var2, color = id)) + geom_point(size = 2)

df_cor <- data.frame(pearson = cor(df$$var1, df$$var2, method = "pearson"),
spearman = cor(df$$var1, df$$var2, method = "spearman"),
rmcorr = rmcorr::rmcorr(participant = id, measure1 = var1, measure2 = var2, dataset = df)\$r,
df %>% group_by(id) %>% summarise(spear = cor(var1, var2, method = "spearman")) %>% summarise(spearman_mean = mean(spear)),
df %>% group_by(id) %>%
summarise(var1 = mean(var1),
var2 = mean(var2)) %>%
summarise(mean_spearman = cor(var1, var2, method = "spearman")))

df_cor %>% knitr::kable()

pearson spearman rmcorr spearman_mean mean_spearman
0.3331734 0.5253885 0.622991 0.9114043 0.5
• Can you add some context? What does the repeated measures represent? Why the variability in measures per id? People here prefer to respond to questions with context ... If you asked for the Pearson r, some standard mixed model should work ... but here something more is maybe needed. Feb 1, 2021 at 20:05
• Alright added a bit of text and a somewhat more relevant example. Feb 2, 2021 at 8:58
• @kjetilbhalvorsen sounded like you might have had an idea. Care to elaborate a bit? Stray thoughts are also welcome :) Feb 3, 2021 at 8:52
• Have you come to any new conclusions in the meantime? Feb 25 at 11:56
• Nope. I left it and went for an analysis of the first and last measurements respectively. Mar 7 at 9:51