使用data.table步骤

时间:2015-08-10 01:29:22

标签: r data.table lm

我能够执行以下内容:

foo=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100),x3=rnorm(100))
full=lm(foo$y ~ foo$x1 + foo$x2 + foo$x3)
nil=lm(foo$y ~ 1)
fwd=step(nil,scope=list(lower=formula(nil),upper=formula(full)),direction='forward')

但我正在使用data.table,如下所示:

library(data.table)
foo=data.table(y=rnorm(100),x1=rnorm(100),x2=rnorm(100),x3=rnorm(100))
full=foo[,lm(y ~ x1 + x2 + x3)]
nil=foo[,lm(y ~ 1)]
fwd=foo[,step(nil,scope=list(lower=formula(nil),upper=formula(full)),direction='forward')]

我收到错误:

Error in eval(expr, envir, enclos) : object 'x1' not found

x1是在上面的J表达式中为data.table定义的 - 有没有办法绕过它而不必将我的表转换为data.frame?

1 个答案:

答案 0 :(得分:4)

您需要将.SD作为data参数传递给lm中的[.data.table,否则data.table会优化j foo=data.table(y=rnorm(100),x1=rnorm(100),x2=rnorm(100),x3=rnorm(100)) full=foo[,lm(y ~ x1 + x2 + x3,data=.SD)] nil=foo[,lm(y ~ 1,data=.SD)] fwd <- step(nil,scope=list(lower=formula(nil),upper=formula(full)),direction='forward') # using .SD means all columns are available ls(environment(formula(nil)) ## [1] "x1" "x2" "x3" "y" # compared with nil.noSD =foo[,lm(y ~ 1)] ls(environment(formula(nil.noSD))) ## [1] "y" }}参数只使用引用的内容。

lm

请注意,data.table和Account a = findAccountIdByUID(uid); ProvisioningService provService = oimClient.getService(oracle.iam.provisioning.api.ProvisioningService.class); a = provService.getAccountDetails(Long.valueOf(a.getAccountID())); System.out.println(a.getAccountData().getData().get("UD_ZZZZ_USR_LOGIN")); a.getAccountData().getData().put("UD_ZZZZ_USR_LOGIN", "hohoho"); <<<<< error provService.modify(a); 以及update.formula等的范围规则并不总是很好地发挥&#34;

请参阅Why is using update on a lm inside a grouped data.table losing its model data?以获取示例