I have a df in long format (resulting from cognitive psych experiments), with several subjects. The df has vectors representing reaction time (RT) and trial number, and a vector with subject number. I want to model the effect of trial number on reaction time (basically, how people get faster with practice). I wrote a simple loop that is supposed to run a regression:
for (i in unique(red_incongruent$subject_nr))
{
print(i)
print(lm(red_incongruent$response_time_response ~ red_incongruent$trial_id))
}
It returns a list with the regression results for each subject. However, I get the same results for each subject so something must be wrong but I don't understand what.
Asked
Active
Viewed 198 times
-1

Davide Piffer
- 113
- 2
- 12
1 Answers
1
You are not selecting the subject in the loop. Perhaps something like this:
red_incongruent=data.frame(
subject_nr=rep(1:100,each=20),
trial_id=rep(1:20,100),
response_time_response=rnorm(2000,100))
for (i in unique(red_incongruent$subject_nr))
{
print(i)
dat = red_incongruent[red_incongruent$subject_nr==unique(red_incongruent$subject_nr)[i],]
print(lm(dat$response_time_response ~ dat$trial_id))
}

timfaber
- 2,060
- 1
- 15
- 17
-
I get the following error: [1] 1 Call: lm(formula = dat$response_time_response ~ dat$trial_id) Coefficients: (Intercept) dat$trial_id 408.417 3.613 [1] 11 Show Traceback Rerun with Debug Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases – Davide Piffer Mar 14 '17 at 12:48
-
Hmm, it works for me, I added a data example (not sure if this fits format) – timfaber Mar 14 '17 at 13:01