TLDR: Given a dataset as shown in the last plot, we consider the following analyses:
1. For each subject extract the slope and perform a simple t-test on the slopes
2. Fit lmer(salery ~ slope + (1|subject)) and test if \(\beta_{slope} =0\)
3. Fit lmer(salery ~ slope + (slope|subject)) and test if \(\beta_{slope} =0\) Then: 1 & 3 are valid (if data balanced, they are equally powerful) approaches and 2 is only valid if there is no individual slope.
nsim <-2000library(mcreplicate) # for parallelizationlibrary(ggplot2)library(lmerTest)
Loading required package: lme4
Loading required package: Matrix
Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':
lmer
The following object is masked from 'package:stats':
step
#' `nsub`: how many subjects (default: 6)#' `nyears`: how many years(default: 10)#' `obsSD`: standard deviation of noise (observation-level) (default: 15)#' `subjSD`: standard deviation of individual effect (default: 4)#' `slope`: shared increase of income per year (default: 5)#' `subjSlopeSD`: subject specific standard deviation from slope (default: 2)get_data <-function(nsub=6, nyears=10, obsSD=15, subjSD=4, slope=5, subjSlopeSD=2){ subj_intercept <-rep(rnorm(nsub, 0, subjSD), each=nyears) subj_slope <-rep(rnorm(nsub, slope, subjSlopeSD), each=nyears)data.frame(subject =as.factor(rep(1:nsub, each = nyears)),year =rep(1:nyears, times = nsub),salary = subj_intercept +# subject effect subj_slope*(1:nyears) +# individual slopernorm(nyears*nsub, 0, obsSD) # obsSD )}
Plot Data
plot_data <-function(main, ...){ggplot(get_data(...), aes(x = year, y = salary, group = subject, color = subject)) +geom_line() +geom_point() +labs(x ="Year", y ="Salary") +theme_minimal() +ggtitle(main)}