Hello, Indrajit,

a "quick and dirty" solution could be to loop over the names of the relevant variables of the data frame (in your case over the first three variables) and use "[[ ]]" for indexing:

for (i in names( df)[ 1:3]) {
 z <- glm( df[[ i]] ~ df$Independent1)
 print( z)
 }



An output that is a little bit more informative w. r. t. the actually used model formula is obtained by

for( i in names( df)[ 1:3]) {
 modelform <- formula( paste( i, "~ Independent1"))
 z <- eval( substitute( lm( f, data = airquality),
                        list( f = modelform) ))
 print( z)
 }



Hth,

Gerrit


On Tue, 15 Mar 2011, Indrajit Chatterjee wrote:

Hi,

I have created a dataframe (lets call is df) that contains the following
variables

"Dependent1" "Dependent2" Dependent3" "Independent1"

I want to do the following regressions:

z<- glm( df$Dependent1 ~ df$Independent1)
z<- glm( df$Dependent2 ~ df$Independent1)
z<- glm( df$Dependent3 ~ df$Independent1)

and so on

I wanted to put this in a for loop e.g.

for (i in 1:3) {

z<- glm( df$ColumnName_i ~ df$Independent1)
print(z)

}


How do I specify the column name in the regression above for the dependent
variable

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
gerrit.eich...@math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner

______________________________________________
R-help@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.

Reply via email to