[R] Help with a Loop

stathelp ebballller1584 at yahoo.com
Sun Dec 2 23:33:47 CET 2007

```I am having trouble getting a loop to work for the following problem. Any
help would be much appreciated. Thanks.

I need to find the slope and intercept from the linear regression of Drug
Level on Day by Participant. There are a total of 37 Participants. I need to
store the Participant, Label, Slope, and Intercept in a new data frame.

This data is ordered by Participant number 37 total participants. A sample
of the data is given below.

Label Participant Day DrugLevel
17      0       1  15 1.84179
121     0       1   5 2.10772
147     0       1   7 3.00658
152     0       1  11 2.91729
250     0       1  10 2.75816
289     0       1  13 3.20468
321     0       1   6 2.43389
362     0       1  12 2.77770
433     0       1   9 3.03167
469     0       1   8 2.97613
475     0       1  14 2.86934
70      0       2  13 0.68022
210     0       2   8 1.41767
243     0       2  11 1.28867
246     0       2   9 1.53601
247     0       2   6 1.64863
280     0       2   5 1.19795
310     0       2  12 1.24440
343     0       2  10 1.18929
413     0       2   7 1.57207
41      0       3   7 1.87884
74      0       3   8 1.82477
100     0       3   5 2.09422
133     0       3   6 1.91853
134     0       3  12 0.90422
149     0       3  11 1.38232
172     0       3  10 1.55323
216     0       3   9 1.24088
65      0       4   8 2.49412
69      0       4   5 1.79840

This is my thought process of what the loop needs to do but I cant get the
correct loop.

X1=Day[Participant=="1"]
Y1=DrugLevel[Participant=="1"]

Coeffs=function(X1,Y1)
{
lmfirst=lm(Y1~X1)
lmfirst\$coefficients
}

Coeffs(X1,Y1)

# output slope and intercept here
# do same for the next participant

X2=Day[Participant=="2"]
Y2=DrugLevel[Participant=="2"]

Coeffs=function(X2,Y2)
{
lmfirst=lm(Y2~X2)
lmfirst\$coefficients
}

Coeffs(X2,Y2)

# output slope and intercept here
# do same for the next participant

X3=Day[Participant=="3"]
Y3=DrugLevel[Participant=="3"]

Coeffs=function(X3,Y3)
{
lmfirst=lm(Y3~X3)
lmfirst\$coefficients
}

Coeffs(X3,Y3)

# output slope and intercept here
# do same for the next participant

# etc for the rest of the participants

# any ideas?

# thanks

--
View this message in context: http://www.nabble.com/Help-with-a-Loop-tf4933354.html#a14120625
Sent from the R help mailing list archive at Nabble.com.

```