I am familiar with linear regression models, but I am in the process of learning about linear mixed effects models.
My data consists of measurements for each month for a set of subjects over a long period of time (~15 years). The subjects and time frames are partially crossed - subjects do not appear for each time point. I also have a number of covariates measured at the per date per subject level, and a single boolean variable indicating a whether a
count is before or after a particular time point. The point of this particular model is to measure whether or not a particular event (occurring at the "mid date") had an effect on the
count variable. Due to the partially crossed, longitudinal nature of my data and the general discontinuity of my data over time, I don't believe that simple paired t-tests can properly answer this question. My data frame is as follows:
subject_id date_monthly count subject_join_date covar1_per_subject_per_date subject_group after_mid_date_bool covar2_per_subject_per_date covar3_per_subject_per_date 1: 0 2013-05-01 3 2011-07-01 1 afteronly TRUE 22.33333 195.7986 2: 0 2013-04-01 1 2011-07-01 1 afteronly TRUE 21.33333 194.7986 3: 0 2013-02-01 19 2011-07-01 1 afteronly TRUE 19.36806 192.8333 4: 0 2013-12-01 3 2011-07-01 1 afteronly TRUE 29.46806 202.9333 5: 0 2013-10-01 4 2011-07-01 1 afteronly TRUE 27.43333 200.8986
subject_id date_monthly count subject_join_date covar1_per_subject_per_date subject_group after_mid_date_bool covar2_per_subject_per_date covar3_per_subject_per_date 22407: 6911 2013-08-01 3 2011-08-01 1 afteronly TRUE 24.36667 198.8653 22408: 6911 2013-07-01 1 2011-08-01 1 afteronly TRUE 23.33333 197.8319 22409: 6911 2013-06-01 1 2011-08-01 1 afteronly TRUE 22.33333 196.8319 22410: 6931 2009-05-01 7 2009-05-01 1 beforeonly FALSE 0.00000 147.0986 22411: 238 2013-09-01 1 2012-10-01 1 afteronly TRUE 11.16667 199.8986
count is the response I am looking to model.
I've read through all of Bates' lme4 paper, but I am still confused as to how to specify the random effects part of my model.
My attempt at a model specification is:
lmer(log(count) ~ covar1_per_subject_per_date + covar2_per_subject_per_date + covar3_per_subject_per_date + after_mid_date_bool + subject_group + subject_join_date + (1|subject_id) + (1|date_monthly), data=df, REML=F)
Which "works" (no errors from
lmer). However, my primary question is:
Is this the correct specification for a mixed effects model with random, uncorrelated intercepts for
date_monthly? Correct here means that we model independent fixed effects for each of the fixed effects specified in the model, accounting for multiple trials of the same subject over time with subjects not appearing at every time point.
A secondary but related question is:
Have I organized my data frame in the proper way? My worry is that the
after_mid_date column may be specified improperly.
I apologize if this is long-winded or too-specific of a question. My intention of providing my exact data is to be as clear as possible with my question.