AnswerBun.com

What type in c# is most like an expression in R and why?

Stack Overflow Asked by Kirsten Greed on January 12, 2021

As a C# developer I curious about the formula parameter in R’s lm function
As in the following code

wine<-read.csv("wine_train.csv")
fit<-lm(formula=quality ~ ., data=wine)

In the help shown by

?lm 

It says

formula
an object of class "formula" (or one that can be coerced to
that class): a symbolic description of the model to be fitted.

In the help shown by

? formula

It says

Model Formulae
The generic function formula and its
specific methods provide a way of extracting formulae which have been
included in other objects.

So I am confused as to whether formula is a class or a function or an "expression" whatever that is.

In C# we have delegates, lambdas and actions that all seem useful to treat code snippets as memory variables.

R is strongly but dynamically typed where as c# is mostly statically typed

Is there a useful parallel in C# for the formula in R?

[Update]

I get that the left and right side of the tilde act as parameters to be used in lm

One Answer

Technically, a formula object is an unevaluated function call, along with an associated environment:

f <- as.formula(y ~ x1 + x2)
> str(f)
Class 'formula'  language y ~ x1 + x2
  ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
> is.call(f)
[1] TRUE

Thinking of the y ~ x1 + x2 part as an unevaluated function call is a bit misleading, because we wouldn't typically really use it like we would other functions. But you can see it really is stored that way:

> f[[1]]
`~`
> f[[2]]
y
> f[[3]]
x1 + x2

i.e. it's a quoted string and two arguments, matching the description of a call from ?call. Of course, we wouldn't really think of ~ as a function in R, but it is one if you check help("~") you'll see it's a primitive. Using an unevaluated call in this way is one of R's more unique features, in that it is leveraging this ability to compute on the language itself to do something nominally unrelated: create a new "symbolic" object to store the conceptual definition of a model.

You can see how the formula object is actually used in this sense in model.frame.default, although that code is....fairly dense itself. The basic idea is that R is piggybacking on this ability to create functions with associated environments to allow the formula to be "evaluated" in the context of that environment, like a function would. But instead of "calling a function" it's more a vehicle for transporting model information that is then used to build the matrices you need for the model fitting process.

Correct answer by joran on January 12, 2021

Add your own answers!

Related Questions

Avast blocks pip and Pyinstaller

1  Asked on December 9, 2020 by dripis

         

Trying to grasp callbacks javascript

2  Asked on December 9, 2020 by justaordinarymonkey

 

Typescript get distinct values from an object

3  Asked on December 9, 2020 by nikhila-reddy-c

     

Strip n from list created from txt file

1  Asked on December 9, 2020 by nataku62

       

Find match pattern and delete the first occurrence

4  Asked on December 8, 2020 by neha-choudhary

   

Weird scoping behavior in python

1  Asked on December 8, 2020 by snickerdoodles777

     

Sqlite foreign key mismatch?

1  Asked on December 8, 2020 by daisy

     

pandas remove for loop

1  Asked on December 8, 2020 by arne

     

Change File with extension txt to json in C#

0  Asked on December 8, 2020 by azeter09

         

Appending an item from one list to another

3  Asked on December 8, 2020 by retroactive

       

Combine two Xcode targets into one distributable app

1  Asked on December 8, 2020 by thecoolwinter

         

Sorting multi-index pd.Series using pd.Categorical?

2  Asked on December 8, 2020 by np8

   

Trying to map an Array to an Object – Javascript

3  Asked on December 7, 2020 by mike-varela

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir