# How to write a linear model formula with 100 variables in R

Is there an easy way in R to create a linear regression over a model with 100 parameters in R? Let's say we have a vector Y with 10 values and a dataframe X with 10 columns and 100 rows In mathematical notation I would write `Y = X[[1]] + X[[2]] + ... + X[[100]]`. How do I write something similar in R syntax?

-

1
are there 100 or 1000? Also, you would normally have the columns be the variables and the rows be observations (it appears that is reversed here) – Macro May 30 '12 at 13:09
100 the extra 0 was a typo – Christian May 30 '12 at 13:11

1
Really? Are you sure you want to do this? I'd be concerned about overfitting and correlation between linear combinations of the predictors. Not only that, with 100 predictors but only 10 observations, you have $p>n$ and linear regression isn't going to work at all. – Aaron May 31 '12 at 1:47

## 4 Answers

Try this

````df<-data.frame(y=rnorm(10),x1=rnorm(10),x2=rnorm(10))
lm(y~.,df)
````
-

Great answers!

I would add that by default, calling `formula` on a `data.frame` creates an additive formula to regress the first column onto the others.

So in the case of the answer of @danas.zuokas you can even do

````lm(df)
````

which is interpreted correctly.

-

1
+1 I never knew this. Interesting – Macro May 30 '12 at 13:34
Still, this answer does not work if you want to mix in interaction terms. Yours does (+1). – gui11aume May 30 '12 at 13:36

3
I'm continually amazed at how overloaded most of `R`'s operators are :) – Macro May 30 '12 at 13:38

If each row is an observation and each column is a predictor so that $Y$ is an $n$-length vector and $X$ is an $n \times p$ matrix ($p=100$ in this case), then you can do this with

````Z = as.data.frame(cbind(Y,X))
lm(Y ~ .,data=Z)
````

If there are other columns you did not want to include as predictors, you would have to remove them from `X` before using this trick, or using `-` in the model formula to exclude them. For example, if you wanted to exclude the 67th predictor (that has the corresponding name `x67`), then you could write

````lm(Y ~ .-x67,data=Z)
````

Also, if you want to include interactions, etc.. you will need to add them manually as (for example)

````lm(Y ~ .+X[,1]*X[,2],data=Z)
````

or make sure they are entered as columns of `X`.

-

You can also use a combination of the `formula` and `paste` functions.

Setup data: Let's imagine we have a data.frame that contains the predictor variables `x1` to `x100` and our dependent variable `y`, but that there is also a nuisance variable `asdfasdf`. Also the predictor variables are arranged in an order such that they are not all contiguous in the data.frame.

````Data <- data.frame(matrix(rnorm(102 * 200), ncol=102))
names(Data) <- c(paste("x", 1:50, sep=""), 
    "asdfasdf", "y", paste("x", 51:100, sep=""))
````

Imagine also that you have a string containing the names of the predictor variables. In this case, this can easily be created using the `paste` function, but in other situations, `grep` or some other approach might be used to get this string.

````PredictorVariables <- paste("x", 1:100, sep="")
````

Apply approach: We can then construct a formula as follows:

````Formula <- formula(paste("y ~ ", 
     paste(PredictorVariables, collapse=" + ")))
lm(Formula, Data)
````
• the `collapse` argument inserts `+` between the predictor variables
• `formula` converts the string into an object of class formula suitable for the `lm` function.
-

1
+1. I use this technique all the time. Occasionally, however, having the formula stored in a variable causes issues. See stackoverflow.com/a/7668846/210673 for use of `do.call` evaluate the formula before calling `lm`. – Aaron May 31 '12 at 1:44
default
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 6, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8963591456413269, "perplexity_flag": "middle"}