s-news
[Top] [All Lists]

Re: loop over a series regression models

To: <huangp@musc.edu>, <s-news@lists.biostat.wustl.edu>
Subject: Re: loop over a series regression models
From: <Bill.Venables@csiro.au>
Date: Fri, 15 Apr 2005 14:54:10 +1000
Thread-index: AcVBaxQxFNVcKigJTWW9SKyqsZ1dnQAC5S6A
Thread-topic: [S] loop over a series regression models

Your question "How [do I] write a loop to obtain the output?" implies that you have already decided that you must need a loop.  In fact you don't.  Here is how I would obtain your output:

    mydata<- data.frame(x1=1:10, x2=(1:10)^2,
          y1=2*(1:10) + (1:10)^2 + rnorm(10),
          y2=10*(1:10) + 3*(1:10)^2 + rnorm(10))
    fit <- lm(as.matrix(mydata[, 3:4]) ~ x1 + x2, mydata)
    output <- t(coef(fm)[, -1])   

and that's it.

Bill V.

     -----Original Message-----
    From:   s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu On Behalf Of Peng Huang
    Sent:   Friday, 15 April 2005 1:27 PM
    To:     s-news@lists.biostat.wustl.edu
    Subject:        [S] loop over a series regression models

    Dear All,

    I have a data frame that contains several dependent variables and independent variables. I would like to save the regression coefficients after fitting each dependent variables (with the same independent variables). How to write a loop to do it automatically? For example, I have

    mydata<- data.frame(x1=1:10, x2=(1:10)^2,
          y1=2*c(1:10) + c(1:10)^2 + rnorm(10),
          y2=10*c(1:10) + 3*c(1:10)^2 + rnorm(10))

    # I want to perform the following
    mod1<- summary(lm(y1~x1+x2, data="">
    mod2<- summary(lm(y2~x1+x2, data="">

    # then save the coefficients:
    output <- rbind(mod1, mod2)

    Question: how to write a loop to obtain the output?
    Thank you for your help!

    Peng Huang

<Prev in Thread] Current Thread [Next in Thread>