在R中的data.table环境中创建公式 [英] create a formula in a data.table environment in R

查看:76
本文介绍了在R中的data.table环境中创建公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 data.table 中运行回归。 公式需要动态构造。我尝试了以下方法:

  x = data.table(a = 1:20,b = 20:1,id = 1:5)
> x [,as.list(coef(lm(as.formula(a〜b)))),by = id]
eval(expr,envir,

如何将环境指定为实际数据表的环境? / p>

编辑:我意识到我可以做lm(a〜b)。我需要公式是动态的,所以它被建立为一个字符串。通过动态我的意思是公式可以 paste0(var_1,〜,var_2)其中 var_1 = a code> var_2 = b



这里有一个解决方案,我认为我们可以做得更好:

  txt = parse(text =as.list(coef(lm(a〜b))))
& x [,eval(txt),by = id]
id(拦截)b
1:1 21 -1
2:2 21 -1
3: 1
4:4 21 -1
5:5 21 -1


解决方案

lm 可以接受字符串作为公式,结合 .SD 像这样:

 > x [,as.list(coef(lm(a〜b,.SD))),by = id] 
id(拦截)b
1:1 21 -1
2:2 21 -1
3:3 21 -1
4:4 21 -1
5:5 21 -1


I would like to run a regression within a data.table. The formula needs to be constructed dynamically. I have tried the following method:

x = data.table(a=1:20, b=20:1, id=1:5)
> x[,as.list(coef(lm(as.formula("a ~ b")))),by=id]
  Error in eval(expr, envir, enclos) : object 'a' not found

How does one specify the environment to be that of the actual data.table where the evaluation occurs?

EDIT: I realize I can do lm(a ~ b). I need the formula to be dynamic so it's built up as a character string. By dynamically I mean the formula can be paste0(var_1, "~", var_2) where var_1 = a and var_2 = b

Here is one solution thought I think we can do better:

txt = parse(text="as.list(coef(lm(a ~ b)))")
> x[,eval(txt),by=id]
  id (Intercept)  b
  1:  1          21 -1
  2:  2          21 -1
  3:  3          21 -1
  4:  4          21 -1
  5:  5          21 -1

解决方案

lm can accept a character string as the formula so combine that with .SD like this:

> x[, as.list(coef(lm("a ~ b", .SD))), by = id]
   id (Intercept)  b
1:  1          21 -1
2:  2          21 -1
3:  3          21 -1
4:  4          21 -1
5:  5          21 -1

这篇关于在R中的data.table环境中创建公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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