0

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?

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Becky
  • 3
  • 2
  • 1
    You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data. It's unclear exactly what's going on. – MrFlick Apr 10 '17 at 17:50
  • @李哲源ZheyuanLi how would I fix this? – Becky Apr 10 '17 at 18:11

1 Answers1

0

My answer is assuming that PAEscore$Subject_ID is readily a factor rather than a character (otherwise levels(PAEscore$Subject_ID gives NULL and your loop does nothing!)

Your loop counter i is through levels, so it will be a character string. Coercing it to numeric with as.numeric(i) gives NA.

To fix it. Outside the loop, initialize

PAEcoeff <- setNames(numeric(nlevels(PAEscore$Subject_ID)), levels(PAEscore$Subject_ID))

Then inside loop, use PAEcoeff[i].

Is there an easier way to do this?

You are fitting a linear regression by group. I suggest you read Linear Regression and group by in R. My answer here is just to fix your error.

Community
  • 1
  • 1
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • That still gives me an empty data table - what could I be doing wrong? – Becky Apr 10 '17 at 19:47
  • I tried this and it did not work. What could I be doing wrong? PAEcoeff$Subject_ID <- as.factor(PAEcoeff$Subject_ID) – Becky Apr 12 '17 at 21:14