在线性模型中结合装订和粘贴 [英] Combining cbind and paste in linear model

查看:132
本文介绍了在线性模型中结合装订和粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何提出lm公式语法,该语法使我能够将pastecbind一起用于多元回归.

I would like to know how can I come up with a lm formula syntax that would enable me to use paste together with cbind for multiple multivariate regression.

在我的模型中,我有一组变量,它们对应于以下原始示例:

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

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

问题

我想用我的depVarsindepVars创建一个模型.手动键入的模型如下所示:

Problem

I would like to create a model with my depVars and indepVars. The model, typed by hand, would look like that:

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


我有兴趣生成相同的公式,而不必引用变量名,而仅使用上面定义的depVarsindepVars向量.


I'm interested in generating the same formula without referring to variable names and only using depVars and indepVars vectors defined above.

例如,我的想法对应于:

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

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

尝试2

我也尝试过这个:

Attempt 2

I tried this as well:

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


旁注

  • 我找到了许多有关如何使用很好的例子 >与公式结合使用,但我想知道如何与cbind结合使用.
  • 这主要是一个语法问题;在我的真实数据中,我想将许多变量引入模型中,并且利用先前生成的向量更加简洁,并使代码更易于呈现.实际上,我只想创建一个包含cbind的公式对象,其变量名对应于一个向量,其余变量对应于另一个向量.
  • 总而言之,我想得出modExample 中的公式,而不必键入变量名.

  • Side notes

    • I found a number of good examples on how to use paste with formula but I would like to know how I can combine with cbind.
    • This is mostly a syntax a question; in my real data I've a number of variables I would like to introduce to the model and making use of the previously generated vector is more parsimonious and makes the code more presentable. In effect, I'm only interested in creating a formula object that would contain cbind with variable names corresponding to one vector and the remaining variables corresponding to another vector.
    • In a word, I want to arrive at the formula in modExample without having to type variable names.
    • 推荐答案

      认为可行.

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

      这篇关于在线性模型中结合装订和粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆