Mike H. Ryu
2008-Apr-29 11:27 UTC
[R] Running regression (lm, lrm) 100+ times and saving the results as matrix
An undergraduate here, so do not hesitate to let me know if you feel that
I''m heading in a wrong direction.
I have a data frame containing panel data across 10 years (hence 120
months). I want to be able to run regression separately for each month (or
year). The below shows how I ran the regression for each month, but I need
to know how I would combine the regression results together into a matrix
possibly.
Thank you!
Mike
-------------------------------
m = max(data$TIME)
# define regmatrix
for(i in 1:m){
g=runreg(data, i)
# attach g to regmatrix
}
runreg = function(data, index){
datainterim = subset(data, TIME==index)
g = lrm(X ~ A + B, datainterim)
return(g)
}
[[alternative HTML version deleted]]
Chuck Cleland
2008-Apr-29 12:05 UTC
[R] Running regression (lm, lrm) 100+ times and saving the results as matrix
On 4/29/2008 7:27 AM, Mike H. Ryu wrote:> An undergraduate here, so do not hesitate to let me know if you feel that > I'm heading in a wrong direction. > > I have a data frame containing panel data across 10 years (hence 120 > months). I want to be able to run regression separately for each month (or > year). The below shows how I ran the regression for each month, but I need > to know how I would combine the regression results together into a matrix > possibly. > > Thank you!If by "results" you mean the coefficients, how about something like this? t(sapply(split(mydata, mydata$TIME), function(x){coef(lm(X ~ A + B, data = x))}))> Mike > > ------------------------------- > > m = max(data$TIME) > > # define regmatrix > > for(i in 1:m){ > > g=runreg(data, i) > > # attach g to regmatrix > > } > > runreg = function(data, index){ > > datainterim = subset(data, TIME==index) > > g = lrm(X ~ A + B, datainterim) > > return(g) > > } > > [[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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Mike H. Ryu
2008-Apr-29 23:38 UTC
[R] Running regression (lm, lrm) 100+ times and saving the results as matrix
Much thanks to Chuck Cleland for the following solution:
t(sapply(split(mydf, mydf$TIME), function(x){coefficients(summary(glm(X ~ A
+ B, data = x, family=binomial)))}))
To see what's in the 12 columns of the matrix returned by those lines, look
at the results this way:
lapply(split(mydf, mydf$TIME), function(x){coefficients(summary(glm(X ~ A +
B, data = x, family=binomial)))})
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Mike H. Ryu
Sent: Tuesday, April 29, 2008 7:27 AM
To: r-help at r-project.org
Subject: [R] Running regression (lm, lrm) 100+ times and saving the results
as matrix
An undergraduate here, so do not hesitate to let me know if you feel that
I'm heading in a wrong direction.
I have a data frame containing panel data across 10 years (hence 120
months). I want to be able to run regression separately for each month (or
year). The below shows how I ran the regression for each month, but I need
to know how I would combine the regression results together into a matrix
possibly.
Thank you!
Mike
-------------------------------
m = max(data$TIME)
# define regmatrix
for(i in 1:m){
g=runreg(data, i)
# attach g to regmatrix
}
runreg = function(data, index){
datainterim = subset(data, TIME==index)
g = lrm(X ~ A + B, datainterim)
return(g)
}
[[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.