Combining cbind and insert into linear model

I would like to know how to come up with the lm formula syntax that will allow me to use paste with cbind for multiple multidimensional regression.

Example

In my model, I have a set of variables, which corresponds to a primitive example below:

 data(mtcars) depVars <- paste("mpg", "disp") indepVars <- paste("qsec", "wt", "drat") 

Problem

I would like to create a model with my depVars and indepVars . A hand-drawn model will look like this:

 modExmple <- lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars) 

I am interested in creating the same formula without resorting to variable names and only using depVars and indepVars vectors defined above.


Attempt 1

For example, what I had in mind would correspond to:

 mod1 <- lm(formula = formula(paste(cbind(paste(depVars, collapse = ",")), " ~ ", indepVars)), data = mtcars) 

Attempt 2

I tried this too:

 mod2 <- lm(formula = formula(cbind(depVars), paste(" ~ ", paste(indepVars, collapse = " + "))), data = mtcars) 

Side notes

  • I found some good examples on how to use paste with a formula, but I would like to know how I can combine with cbind .
  • This is mainly a matter of syntax; in my real data, I have a number of variables that I would like to represent models, and using a previously generated vector is more economical and makes the code more presentable. In fact, I'm only interested in creating a formula object that will contain cbind with variable names corresponding to one vector and the remaining variables corresponding to another vector.
  • In short, I want to get the formula in modExample without entering variable names.
+5
source share
2 answers

I think it works.

 data(mtcars) depVars <- c("mpg", "disp") indepVars <- c("qsec", "wt", "drat") lm(formula(paste('cbind(', paste(depVars, collapse = ','), ') ~ ', paste(indepVars, collapse = '+'))), data = mtcars) 
+5
source

All of the solutions below use the following definitions:

 depVars <- c("mpg", "disp") indepVars <- c("qsec", "wt", "drat") 

1) character string formula Create a character string representing the formula, and then run lm using do.call . Please note that the formula shown in the output is displayed correctly and is written out.

 fo <- sprintf("cbind(%s) ~ %s", toString(depVars), paste(indepVars, collapse = "+")) do.call("lm", list(fo, quote(mtcars))) 

giving:

 Call: lm(formula = "cbind(mpg, disp) ~ qsec+wt+drat", data = mtcars) Coefficients: mpg disp (Intercept) 11.3945 452.3407 qsec 0.9462 -20.3504 wt -4.3978 89.9782 drat 1.6561 -41.1148 

1a) This will also work:

 fo <- sprintf("cbind(%s) ~.", toString(depVars)) do.call("lm", list(fo, quote(mtcars[c(depVars, indepVars)]))) 

giving:

 Call: lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars[c(depVars, indepVars)]) Coefficients: mpg disp (Intercept) 11.3945 452.3407 qsec 0.9462 -20.3504 wt -4.3978 89.9782 drat 1.6561 -41.1148 

2) reformulate @akrun and @Konrad, suggest using reformulate in the comments below the question. This approach creates a "formula" object, while those above create a string of characters as a formula. (If it was necessary for the previous solutions above, this would be possible using fo <- formula(fo) .) Note that it is important that the reformulate response reformulate be a call object and not a character string, or reformulate interprets the character string as name of one variable.

 fo <- reformulate(indepVars, parse(text = sprintf("cbind(%s)", toString(depVars)))[[1]]) do.call("lm", list(fo, quote(mtcars))) 

giving:

 Call: lm(formula = cbind(mpg, disp) ~ qsec + wt + drat, data = mtcars) Coefficients: mpg disp (Intercept) 11.3945 452.3407 qsec 0.9462 -20.3504 wt -4.3978 89.9782 drat 1.6561 -41.1148 

3) lm.fit Another way that does not use the formula at all:

 m <- as.matrix(mtcars) fit <- lm.fit(cbind(1, m[, indepVars]), m[, depVars]) 

The output is a list with these components:

 > names(fit) [1] "coefficients" "residuals" "effects" "rank" [5] "fitted.values" "assign" "qr" "df.residual" 
+3
source

Source: https://habr.com/ru/post/1241986/


All Articles