Hi,
Try:
Please use ?dput() to show the example dataset:
dat <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Y = c(555L,
345L, 456L, 552L, 342L, 452L, 555L, 345L, 456L, 552L, 342L, 452L,
562L, 352L, 463L, 582L, 342L, 483L, 522L, 362L, 493L, 532L, 342L,
423L), X1 = c(111L, 234L, 353L, 115L, 232L, 358L, 111L, 234L,
353L, 115L, 232L, 358L, 118L, 251L, 320L, 138L, 211L, 320L, 128L,
221L, 320L, 119L, 242L, 330L), X2 = c(222L, 445L, 453L, 222L,
449L, 458L, 222L, 445L, 453L, 222L, 449L, 458L, 239L, 422L, 470L,
219L, 422L, 410L, 239L, 492L, 470L, 219L, 432L, 460L), X3 = c(333L,
543L, 456L, 343L, 524L, 458L, 333L, 543L, 456L, 343L, 524L, 458L,
340L, 540L, 473L, 310L, 520L, 453L, 310L, 530L, 483L, 320L, 530L,
413L), X4 = c(456L, 33L, 333L, 486L, 23L, 348L, 456L, 33L, 333L,
486L, 23L, 348L, 463L, 40L, 330L, 423L, 43L, 310L, 433L, 45L,
310L, 433L, 41L, 340L)), .Names = c("ID", "Y",
"X1", "X2", "X3",
"X4"), class = "data.frame", row.names = c(NA, -24L))
?res <- t(sapply(split(dat[,-1],dat$ID),function(x)
coef(lm(Y~.,data=x))[-1]))
?colnames(res) <- letters[1:4]
#or
library(plyr)
?res1 <- ddply(dat,.(ID),function(x) coef(lm(Y~.,data=x[,-1])))[,-2]
?colnames(res1)[-1] <- colnames(res)
#or
res3 <- do.call(rbind,by(dat[,-1],dat[,1],function(x)
coef(lm(Y~.,data=x))[-1]))
?colnames(res3) <- colnames(res)
A.K.
On Monday, October 14, 2013 1:00 PM, Valeria Muzzolini <valeria.muzzolini at
uniud.it> wrote:
Hello,
I'd like to repeat a linear regression model for groups of rows that are
identified by an ID number.
My table is like this:
ID ??? Y ??? X1 ??? X2 ??? X3 ??? X4
1 ??? 555 ??? 111 ??? 222 ??? 333 ??? 456
1 ??? 345 ??? 234 ??? 445 ??? 543 ??? 33
1 ??? 456 ??? 353 ??? 453 ??? 456 ??? 333
2 ??? 562 ??? 118 ??? 229 ??? 340 ??? 463
2 ??? 352 ??? 241 ??? 452 ??? 550 ??? 40
2 ??? 463 ??? 360 ??? 460 ??? 463 ??? 340
3 ??? ....
??? ...
??? ...
??? ...
??? ....
3 ???
???
???
???
???
3 ???
???
???
???
???
I want to obtain an estimate of the linear model for each ID.
ex.? y = a1X1 + b1X2 + c1X3 + d1X4? ? where ID= 1
? ? ? ? y = a2X1 + b2X2 + c2X3 + d2X4? ? where ID= 2? ? ? ? and so on...
I'd like to obtain a table such this:
ID ??? a ??? b ??? c ??? d
1 ??? ... ??? ... ??? ... ??? ..
2 ??? .. ??? .. ??? .. ??? ..
3 ??? .. ??? .. ??? .. ??? ..
Can you sugget me something?
Can I use a "for" script?
Thank you very much!
Valeria
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.