[R] Loop for running several models and retrieving outputs

jim holtman jholtman at gmail.com
Tue Sep 16 04:48:11 CEST 2008


Put the data in a list:

myData <- list(data1, data2, ..., data10)
resultList <- list()
for (i in seq_along(myData)) resultList[[i]] <- lm(y ~ x, myData[[i]])


On Mon, Sep 15, 2008 at 10:45 PM, FD <fhduan at gmail.com> wrote:
> Hi Jim:
>
> Thanks a lot for helping me with this question.
>
> The "xxxx" are just a model formula.
>
> For example,
>
> lm(y~x, data=data1) #model 1
> lm(y~x, data=data2) #model 2
> ...
> lm(y~x, data=data10) #model 10
>
>
> All I want is extracting the coefficient estimates and p values and saving
> them into one data frame, so that I can easily compare these 10 models. I
> think your way works. But can you tell me how to write those 10 models into
> your list variable "myModels"? Thanks.
>
> FD
>
> On Mon, Sep 15, 2008 at 9:19 PM, jim holtman <jholtman at gmail.com> wrote:
>>
>> It is hard to tell since you have not told us what 'xxxxxxxxxxxxx' is,
>> but one way is to create a list of the objects you want to send to
>> 'lm' and then save them in a list as output:
>>
>> fit.mod <- list()
>> for (i in seq_along(myModels)){
>>    fit.mod[[i]] <- lm(myModels[[i]]
>> }
>>
>> On Mon, Sep 15, 2008 at 4:49 PM, FD <fhduan at gmail.com> wrote:
>> > Hi All:
>> >
>> > I need to run a bunch of models one-by-one and retrieve the outputs
>> > (like
>> > estimates, p values) accordingly. Some of models may not have outputs
>> > due to
>> > singularity or even no observations. If this happens, assign NA to the
>> > outputs.
>> >
>> > For example,
>> >
>> > #running models
>> > fit.mod <- lm(xxxxxxxxxx)  #model 1
>> >  fit.mod <- lm(xxxxxxxxxx)  #model 2
>> > .........
>> > fit.mod <- lm(xxxxxxxxxx)  #model 10
>> >
>> > #outputting estimates and p values to a data frame
>> > ....
>> >
>> >
>> > Any suggestions? Thank you very much in advance.
>> >
>> > FD
>> >
>> >        [[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.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list