mcnpf748 at mncn.csic.es
2007-Feb-02 17:57 UTC
[R] Access to column names stored in a vector in lm procedure
Hello everybody I have to run many statistical tests that are identical, with the exception of the dependent variable. Is there a possibility to store the dependent variable names e.g. in a vector (in the below mentioned example called “variable”) and to use the content of this vector in a simple statistical test (e.g. a regression). I would like to write the statistical procedure only once… For example: I would like to store 100 dependent variable names in a vector called “variable” and then I would like to do a simple regression with each of theses variables and the variable “length” using e.g. the while function. I would then extract e.g. the t-value and add it to a vector (“result”) that contains all results. Something like that: “variable” should contain the names of the 100 dependent variables (Var1, Var2, … Var100) while(i<101){ result<-c(result,coef(summary(lm(variable[i] ~ length, data = data2)))[2,4]); i<-i+1 } This example does not work since the lm function does not recognize the dependent variable’s name. Does somebody know how to store the names of the dependent variables in e.g. a vector and to make them available for the lm function? Many thanks Patrick
Gabor Grothendieck
2007-Feb-03 12:44 UTC
[R] Access to column names stored in a vector in lm procedure
You can regress multiple dependent variables at once by placing them in a matrix. For example, this regresses each of the first 4 columns of the builtin iris dataset against Species, the last column: iris4 <- as.matrix(iris[,-5]) summary(lm(iris4 ~ Species, iris)) On 2/2/07, mcnpf748 at mncn.csic.es <mcnpf748 at mncn.csic.es> wrote:> Hello everybody > > I have to run many statistical tests that are identical, with the > exception of the dependent variable. Is there a possibility to store the > dependent variable names e.g. in a vector (in the below mentioned example > called "variable") and to use the content of this vector in a simple > statistical test (e.g. a regression). I would like to write the > statistical procedure only once? > > > For example: I would like to store 100 dependent variable names in a > vector called "variable" and then I would like to do a simple regression > with each of theses variables and the variable "length" using e.g. the > while function. I would then extract e.g. the t-value and add it to a > vector ("result") that contains all results. Something like that: > > > > "variable" should contain the names of the 100 dependent variables (Var1, > Var2, ? Var100) > > while(i<101){ > result<-c(result,coef(summary(lm(variable[i] ~ length, data = data2)))[2,4]); > i<-i+1 > } > > This example does not work since the lm function does not recognize the > dependent variable's name. > > Does somebody know how to store the names of the dependent variables in > e.g. a vector and to make them available for the lm function? > > Many thanks > > Patrick > > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > > >