Dear All, I have a large data frame with 10 rows and 82 columns. I want to apply the same function to all of the columns with a single command. e.g. zl <- lm (snp$a_109909 ~ snp$lat) will fit a linear model to the values in lat and a_109909. What I want to do is fit linear models for the values in each column against lat. I tried doing zl <- (snp[,2:82] ~ snp$lat[,1]) but got the following error message "Error in model.frame.default(formula = snp[, 2:82] ~ snp[, 1], drop.unused.levels = TRUE) : invalid type (list) for variable 'snp[, 2:82]'". Does this mean I cannot use a data frame to do this? Should I have my data in a matrix instead? Thanks -- View this message in context: http://r.789695.n4.nabble.com/automating-regression-or-correlations-for-many-variables-tp3426091p3426091.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-Apr-04 19:53 UTC
[R] automating regression or correlations for many variables
Hi: Here's a small example:> df <- data.frame(y1 = rnorm(10), y2 = rnorm(10), y3 = rnorm(10), lat rnorm(10)) > m <- lm(cbind(y1, y2, y3) ~ lat, data = df) > summary(m)<snip...provides summaries for each response> The LHS of the model formula needs to be a matrix. In your case, something like m <- lm(as.matrix(snp[, -1]) ~ lat, data = snp) ought to work. HTH, Dennis On Mon, Apr 4, 2011 at 10:13 AM, geral <geraldes@mail.ubc.ca> wrote:> Dear All, > > I have a large data frame with 10 rows and 82 columns. I want to apply the > same function to all of the columns with a single command. e.g. zl <- lm > (snp$a_109909 ~ snp$lat) will fit a linear model to the values in lat and > a_109909. What I want to do is fit linear models for the values in each > column against lat. I tried doing zl <- (snp[,2:82] ~ snp$lat[,1]) but got > the following error message "Error in model.frame.default(formula = snp[, > 2:82] ~ snp[, 1], drop.unused.levels = TRUE) : > invalid type (list) for variable 'snp[, 2:82]'". Does this mean I cannot > use a data frame to do this? Should I have my data in a matrix instead? > > Thanks > > -- > View this message in context: > http://r.789695.n4.nabble.com/automating-regression-or-correlations-for-many-variables-tp3426091p3426091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]