I collected a dependent variable (muscle strength) before (PRE) and after (POST) 15 training sessions. Each session was classified in 2 different types of training (Type A and Type B). I would like to investigate if there are differences in my dependent variable in PRE-POST and type of training (Type A and Type B) controlling for two covariates (continue variables) and using subjects as random effect. My problem is the following: while the Covariate A (subjects wellbeing) influences the dependent variable both in PRE and POST, the Covariate B (session training load) only influences the dependent variable at POST. Hence in my dataset the Covariate B at PRE is empty.
Here the description of my variables: Dependent variable: DV (subjects muscle strength) Independent variable: Type of practice (2 levels: Type A and Type B) Independent variable: Time (2 levels: PRE and POST) Covariate A: continuous variable (subjects wellbeing) Covariate B: continuous variable (session training load) ID: Subjects
I am attempting to fit a mixed effect model using R and lme4, but I am new to the use of mixed models. My model is the following:
Model = lmer (DV~ Time * Type + (Covariate A)+( Covariate B)+(1|subject1) , data=dataset)
dataset looks like this:
ID Session Time Type Covariate A Covariate B DV
Sub1 n1 PRE A 10 120
sub1 n1 POST A 10 5 115
sub1 n2 PRE B 14 122
sub1 n2 POST B 14 6 124
sub1 n3 PRE A 12 123
sub1 n3 POST A 12 7 119
sub1 n4 PRE B 9 118
sub1 n4 POST B 9 8 120
… … … … … … …
However, when I run the model with this database, I receive the following error:
Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more level
s.`
Differently, If I run the analysis with the following data set, the model works. However, I know that the Covariate B does not influence the DV in the PRE. Thus it is illogical to apply a value of covariate B to an observation at PRE.
ID Session Time Type Covariate A Covariate B DV
Sub1 n1 PRE A 10 5 120
sub1 n1 POST A 10 5 115
sub1 n2 PRE B 14 6 122
sub1 n2 POST B 14 6 124
sub1 n3 PRE A 12 7 123
sub1 n3 POST A 12 7 119
sub1 n4 PRE B 9 8 118
sub1 n4 POST B 9 8 120
… … … … … … …
How Can organize the database considering this aspect?
Following your suggestions, here you can find an example that you can run: drive.google.com/open?id=1h1F_ITZmmCN5sNKtMsETM2yakx3Kjmn0
Model 1: the right model where the CovariateB (session training load) only influences the dependent variable at POST.
Model 2: the wrong model where the CovariateB_2 (session training load) is reported both in PRE and POST.
R script:
library(lme4)
library(lmerTest)
library(emmeans)
library(readxl)
dataset = read_excel("$$:/$$$/$$/$$.xlsx", sheet = NULL, range = NULL, col_names = TRUE)
dataset$Time <- as.factor(dataset$Time)
dataset$Type <- as.factor(dataset$Type)
dataset$Subject <- as.factor(dataset$Subject)
Model_1 = lmer(((DV)) ~ Time*Type+(CovariateA)+(CovariateB)+(1|Subject), data=dataset, REML=FALSE);
Model_2 = lmer(((DV)) ~ Time*Type+(CovariateA)+(CovariateB_2)+(1|Subject), data=dataset, REML=FALSE);