Regression: within and between-subject variable interaction I have a dataset with the following variables, which are measured between subjects (i.e. 1 measurement per individual):


*

*ID: subject identifier

*age: subject age

*activity: overall activity level of that individual


Each individual completes 2 computer tasks (one easy, one hard) and reaction time (RT) was measured for each. So I have the following variables measured within subjects (repeated measures):


*

*difficulty: task difficulty

*RT: reaction time on the task


My data looks something like this:
df <- data.frame("ID" = c(1,1,2,2,3,3,4,4),
                 "age" = c(20,20,25,25,19,19,21,21),
                 "activity" = c(55,55,72,72,83,83,67,67),
                 "difficulty" = c(0,1,0,1,0,1,0,1),
                 "RT" = c(110,250,90,100,99,132,122,134))
df$difficulty <- factor(df$difficulty)

  ID age activity difficulty  RT
1  1  20       55          0 110
2  1  20       55          1 250
3  2  25       72          0  90
4  2  25       72          1 100
5  3  19       83          0  99
6  3  19       83          1 132
7  4  21       67          0 122
8  4  21       67          1 134

I'm expecting to see an interaction such that the slope between activity and RT will be different for each level of difficulty factor (while controlling for age). I have tried the following model in R:
lm(RT ~ activity*difficulty + age, data=df)

My concern here is the repeated nature of some of my variables. As you can see from my sample data there are twice as many rows as there should be. The values for my between subject variables are doubled and each participant was measured twice, which will affect my degrees of freedom, and thus my p values.
Is this a valid concern when testing regression interactions involving within subject variables?
Is there a more appropriate way to test this and what would it look like in R?
 A: The lm() model you proposed would make sense if each individual in your study provided a single reaction time (RT) value. However, each individual provides two RT values which are likely correlated for that individual. That correlation invalidates the assumption of independence of the RT values required by the lm() model, hence the results produced by that model. 
What you need to use instead of the lm() function is the lmer() function from the lme4 package, which implements a linear mixed effects model. Here is an example of lmer syntax: 
install.packages("lme4")

require(lme4) 

m <- lmer(RT ~ activity*difficulty + age + (1 + difficulty | ID), data = df)

where ID needs to be coded as a factor. The above model includes a random intercept and a random slope for difficulty, as well as a random grouping factor (i.e., individual). For a model with just a random intercept, you can use this syntax: 
m <- lmer(RT ~ activity*difficulty + age + (1 | ID), data = df)

This tutorial will get you started with linear mixed effects models: http://www.bodowinter.com/tutorial/bw_LME_tutorial2.pdf. 
As an aside, I have a feeling that RT might need to be transformed before being included in your model (e.g., via a log transformation). 
