I have a dataset (PAEscore) of 1672 entries per column (38 participants, 44 observations per participant). I want to look at Fraction_Value vs. Subject_Fraction, and code part of a script that will give me the R coefficient for each subject. In the data I have loaded, I have a column for subject number, a column for fraction value, and a column for subject fraction. So far, I have a code that will give me a plot and R value for the combined data of all 38 participants:
fit1 <- lm(Fraction_Value ~ Subject_Fraction)
summary(fit1)
plot(Fraction_Value ~ Subject_Fraction)
abline(fit1)
However, I am having a lot of trouble running a loop that will give me a table containing the individual R values for each participant. I've tried this and it doesnt work.
PAEcoeff = matrix()
for (i in levels(PAEscore$Subject_ID)) {
# Create temporary data frame:
PAE_tmp <- PAEscore[PAEscore$Subject==i,]
# Perform regression:
reg_result <- lm(PAE_tmp$Fraction_Value ~ PAE_tmp$Subject_Fraction)
# Get coefficient:
tmp_coef <- coef(reg_result)
# Store coefficient:
PAEcoeff[as.numeric(i)] <- tmp_coef[2]
}
Do you guys have any tips on what I can do to fix it? I don't get any errors when I run this, but I just get an empty table. What am I missing? Or, is there an easier way to do this?