本文介绍了在R中的数据框中的列子集上拟合模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在协变量的子集上使用lm()和matchit().我已经生成了任意数量的带有前缀"covar"的列,即"covar.1","covar.2"等.我想做类似的事情

I'm trying to use lm() and matchit() on a subset of covariates. I have generated an arbitrary number of columns with prefix "covar", i.e. "covar.1", "covar.2", etc. I'd like to do something like

lm(group〜covars,data = df)

lm(group ~ covars, data=df)

其中covars是字符串c("covar.1","covar.2",...)的向量.

where covars is a vector of strings c("covar.1", "covar.2", ...).

我尝试过类似的事情

  cols <- colnames(df)
  covars <- cols[grep("covar", colnames(df))]
  m.out <- matchit(group ~ covars, data=df, method="nearest", distance="logit", caliper=.20)

但得到了variable lengths differ (found for 'covars').

仅使用covars和group定义新的数据框是可行的,但是使用matchit违反了我的目的,因为我也希望匹配的数据具有其他列,而不仅仅是我选择作为匹配对象的covars.

Defining a new dataframe only with covars and group can work but that defeats my purpose using matchit because I want the matched data to have other columns, too, not just covars I picked to be the matched on.

这似乎是一件容易的事,但经过一番谷歌搜索后,我不知何故.不知道那里的R公式期望作为列的子集.感谢您的帮助.

This seems to be an easy task but somehow I can't figure out after some googling. Not sure what R formula expects there as subset of columns. Any help is appreciated.

推荐答案

您可能想使用as.formula.
尝试这样做:

You might want to use as.formula.
Try doing this:

替换group ~ covars

as.formula(paste('group','~', paste(covars, collapse="+"))))

这篇关于在R中的数据框中的列子集上拟合模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:49