Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a formula in a data.table environment in R

Tags:

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
like image 843
Alex Avatar asked Feb 09 '13 01:02

Alex


People also ask

What does table () do in R?

table() function in R Language is used to create a categorical representation of data with variable name and the frequency in the form of a table.

What is := in data table?

table way. Unlike data. frame, the := operator adds a column to both the object living in the global environment and used in the function.

What does setDT do in R?

setDT converts lists (both named and unnamed) and data. frames to data. tables by reference. This feature was requested on Stackoverflow.

What is the difference between data frame and data table in R?

frame in R is similar to the data table which is used to create tabular data but data table provides a lot more features than the data frame so, generally, all prefer the data. table instead of the data.


1 Answers

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
like image 67
G. Grothendieck Avatar answered Sep 30 '22 17:09

G. Grothendieck