I am measuring 2 responses in patients from different age cohorts. Each response is negatively correlated with age. This gives me a positive correlation of ResponseX and ResponseY in a total population. For each given age group ResponseX and ResponseY are negatively correlated. Moreover, the slope of this correlation is Age-dependent. Below you will find the code to generate fake data very similar to what I have:
library('ggplot2')
library(tidyverse)
library(lme4)
# average X and Y depends from Age
k_mu <- c(-5,-5)
i_mu <- c(1,2)
# inside each group X and Y are linearly dependent
k_grp <- -0.5
i_grp <- 0
# Generate Ages
N = 10^2
Age <-sample(0:2,N,replace = TRUE)
# add correlations inside groups
k <-k_grp*Age+i_grp
x <- rnorm(N,mean=0,sd=1)
y <- k*x+rnorm(N,mean=0,sd=0.5)
x<-x+k_mu[1]*Age+i_mu[1]
y<-y+k_mu[2]*Age+i_mu[2]
age<-factor(Age,ordered = TRUE)
data = data.frame(x,y,age)
If I will plot the glm regressions it looks pretty nice:
ggplot(data, aes(x = x, y = y, color = age) ) +
geom_point() +
geom_smooth(method = "lm", se = T)+
geom_smooth(group=1,method = "glm", se = T,'color'='black')+
labs(x='ResponseX', y='ResponseY', color='Age')
Now I want to find equations for all 4 lines from this plot and equation of dependence of ingroup slopes from age. To do this I tried to use lmer
, but I could not find a solution. The following code gives me an error:
lmer(y ~ 1+age + (1+x|age), data = data) %>% summary()
Could you help me to model my data?