Dear Contributors I would like to ask some help concerning the automatization process of an analysis, that sounds hard to my knowledge. I have a list of regression models. I call them models=c(ra,rb,rc,rd,re,rf,rg,rh) I can access the output of each of them using for example, for the first ra$coefficients and i obtain (Intercept) coeff1 coeff2 age gender 0.62003033 0.00350807 -0.03817848 -0.01513533 -0.18668972 and I know that ra$coefficients[1] would give me the intercept of this model. What i need to do is to collect the coefficients of each regression in models, and calculate and place in a table, the following simple summation: ra rb rc ... intercept intercept intercept intercept+coeff1 intercept+coeff1 intercept+coeff1 intercept+coeff2 intercept+coeff2 intercept+coeff2 intercept+coeff1+coeff2 intercept+coeff1+coeff2 intercept+coeff1+coeff2 The calculations are trivial(I know how to do it in steps) but what is difficult for me is to invent a procedure that organizes the data in an efficient way. I tried some step , starting with collecting the coefficients but i think I am going the wrong way calcolati <- list() for (i in c(ra,rb,rc,rd,re,rf,rg,rh)) { calcolati[[i]] <- i$coefficients[1] } Thanks for any help you can provide. f. ---------------------------------- Francesca Pancotto Web: https://sites.google.com/site/francescapancotto/ <https://sites.google.com/site/francescapancotto/> Energie: http://www.energie.unimore.it/ <http://www.energie.unimore.it/> ---------------------------------- [[alternative HTML version deleted]]
Dear Francesca i usually do this by collecting the models into a list not a vector model <- list(ra = ra, rb = rb, and so on and then I use lapply or sapply to process the model lapply(mode, function(x) coef(x)[1]) or something like that, not tested On 10/12/2016 07:32, francesca Pancotto wrote:> Dear Contributors > > I would like to ask some help concerning the automatization process of an analysis, that sounds hard to my knowledge. > I have a list of regression models. > I call them models=c(ra,rb,rc,rd,re,rf,rg,rh) > > I can access the output of each of them using for example, for the first > > ra$coefficients > > and i obtain > > (Intercept) coeff1 coeff2 age gender > 0.62003033 0.00350807 -0.03817848 -0.01513533 -0.18668972 > and I know that ra$coefficients[1] would give me the intercept of this model. > > What i need to do is to collect the coefficients of each regression in models, and calculate and place in a table, the following simple summation: > > > ra rb rc ... > > intercept intercept intercept > intercept+coeff1 intercept+coeff1 intercept+coeff1 > intercept+coeff2 intercept+coeff2 intercept+coeff2 > intercept+coeff1+coeff2 intercept+coeff1+coeff2 intercept+coeff1+coeff2 > > > The calculations are trivial(I know how to do it in steps) but what is difficult for me is to invent a procedure that organizes the data in an efficient way. > > I tried some step , starting with collecting the coefficients but i think I am going the wrong way > > calcolati <- list() > for (i in c(ra,rb,rc,rd,re,rf,rg,rh)) > { > calcolati[[i]] <- i$coefficients[1] > } > > Thanks for any help you can provide. > > f. > ---------------------------------- > Francesca Pancotto > Web: https://sites.google.com/site/francescapancotto/ <https://sites.google.com/site/francescapancotto/> > Energie: > http://www.energie.unimore.it/ <http://www.energie.unimore.it/> > ---------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Francesca: It is hard to know what you might mean by "organize the results in an efficient way." Some would say that a list of models already does this for you. The task you described is fairly simple, so I believe you would benefit by going through an R tutorial or two to improve your R programming skills. There are many good ones on the Web. That would probably be a better long term strategy for you than posting basic questions here. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Dec 9, 2016 at 11:32 PM, francesca Pancotto <francesca.pancotto at gmail.com> wrote:> Dear Contributors > > I would like to ask some help concerning the automatization process of an analysis, that sounds hard to my knowledge. > I have a list of regression models. > I call them models=c(ra,rb,rc,rd,re,rf,rg,rh) > > I can access the output of each of them using for example, for the first > > ra$coefficients > > and i obtain > > (Intercept) coeff1 coeff2 age gender > 0.62003033 0.00350807 -0.03817848 -0.01513533 -0.18668972 > and I know that ra$coefficients[1] would give me the intercept of this model. > > What i need to do is to collect the coefficients of each regression in models, and calculate and place in a table, the following simple summation: > > > ra rb rc ... > > intercept intercept intercept > intercept+coeff1 intercept+coeff1 intercept+coeff1 > intercept+coeff2 intercept+coeff2 intercept+coeff2 > intercept+coeff1+coeff2 intercept+coeff1+coeff2 intercept+coeff1+coeff2 > > > The calculations are trivial(I know how to do it in steps) but what is difficult for me is to invent a procedure that organizes the data in an efficient way. > > I tried some step , starting with collecting the coefficients but i think I am going the wrong way > > calcolati <- list() > for (i in c(ra,rb,rc,rd,re,rf,rg,rh)) > { > calcolati[[i]] <- i$coefficients[1] > } > > Thanks for any help you can provide. > > f. > ---------------------------------- > Francesca Pancotto > Web: https://sites.google.com/site/francescapancotto/ <https://sites.google.com/site/francescapancotto/> > Energie: > http://www.energie.unimore.it/ <http://www.energie.unimore.it/> > ---------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi Francesca, I'm not sure what you are doing here, but try this: regnames<-paste("r",letters[1:8],sep="") for(i in 1:8) { response<-rnorm(20) coef1<-rnorm(20) coef2<-rnorm(20) age<-sample(20:50,20) gender<-sample(c("M","F"),20,TRUE) assign(regnames[i],lm(response~coef1+coef2+age+gender)) } reglist<-list(ra,rb,rc,rd,re,rf,rg,rh) cumsumcoef<-function(x) return(cumsum(x$coef)) coefsumlist<-as.data.frame(lapply(reglist,cumsumcoef)) names(coefsumlist)<-regnames coefsumlist Jim On Sat, Dec 10, 2016 at 6:32 PM, francesca Pancotto <francesca.pancotto at gmail.com> wrote:> Dear Contributors > > I would like to ask some help concerning the automatization process of an analysis, that sounds hard to my knowledge. > I have a list of regression models. > I call them models=c(ra,rb,rc,rd,re,rf,rg,rh) > > I can access the output of each of them using for example, for the first > > ra$coefficients > > and i obtain > > (Intercept) coeff1 coeff2 age gender > 0.62003033 0.00350807 -0.03817848 -0.01513533 -0.18668972 > and I know that ra$coefficients[1] would give me the intercept of this model. > > What i need to do is to collect the coefficients of each regression in models, and calculate and place in a table, the following simple summation: > > > ra rb rc ... > > intercept intercept intercept > intercept+coeff1 intercept+coeff1 intercept+coeff1 > intercept+coeff2 intercept+coeff2 intercept+coeff2 > intercept+coeff1+coeff2 intercept+coeff1+coeff2 intercept+coeff1+coeff2 > > > The calculations are trivial(I know how to do it in steps) but what is difficult for me is to invent a procedure that organizes the data in an efficient way. > > I tried some step , starting with collecting the coefficients but i think I am going the wrong way > > calcolati <- list() > for (i in c(ra,rb,rc,rd,re,rf,rg,rh)) > { > calcolati[[i]] <- i$coefficients[1] > } > > Thanks for any help you can provide. > > f. > ---------------------------------- > Francesca Pancotto > Web: https://sites.google.com/site/francescapancotto/ <https://sites.google.com/site/francescapancotto/> > Energie: > http://www.energie.unimore.it/ <http://www.energie.unimore.it/> > ---------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.