Creating a formula (syntax) in R for a package

I have a likelihood function that comes as an argument to the maxLik function of the maxLik package. For instance:

 library(maxLik) likFun <- function(param, x, y, h) { dep <- as.matrix(x[,y]) indep <- as.matrix(x[,h]) indep <- as.matrix(cbind(cons=1, indep)) k <- dim(indep)[2] beta <- as.matrix(param[1:k]) xbeta <- crossprod(t(indep), beta) sig <- param[k+1] res <- (dep-xbeta)/sig sum( (-(1/2)*log(2*pi)) - ((1/2)*log((sig)^2)) - ((res^2)/2) ) } model <- maxLik(likFun, start=c(0,0,0,1), grad=NULL, hess=NULL, x=mtcars, y="mpg", h=c("cyl", "hp")) summary(model) 

In principle, the aforementioned likelihood function uses the maximum probability for a multiple regression model. My question is: how to create a formula in R (for a package), for example, in lm , so that the user can enter a dependent variable and independent variables and data. I checked model.matrix , but I'm not sure if this is the one I should look for. Any suggestion in this regard would be greatly appreciated.

+4
source share
2 answers

To be able to call your function using mpg ~ cyl + hp instead of y="mpg", h=c("cyl", "hp") in the arguments, you can use the formula object. By default, they are not evaluated, so you do not need substitute and match.call .

If you need something more complicated than just deleting the characters in each part of the formula, you will have to write your own parser. Otherwise, use the all.vars function:

 f <- function(data, formula){ print("Right hand side:") print(head(data[,all.vars(formula[[3]]), drop=FALSE])) print("Left hand side:") print(head(data[,all.vars(formula[[2]]), drop=FALSE])) } 

Result:

 > f(mtcars, mpg ~ cyl + hp) [1] "Right hand side:" cyl hp Mazda RX4 6 110 Mazda RX4 Wag 6 110 Datsun 710 4 93 Hornet 4 Drive 6 110 Hornet Sportabout 8 175 Valiant 6 105 [1] "Left hand side:" mpg Mazda RX4 21.0 Mazda RX4 Wag 21.0 Datsun 710 22.8 Hornet 4 Drive 21.4 Hornet Sportabout 18.7 Valiant 18.1 
+3
source

Roughly, you use eval inside the function. However, first you need to get the arguments of the function in an unreasonable context (so that you can substitute the variables in the formula). For this you will need match.call . Here is a very simple example:

 f <- function (.FUN, ...) { args <- match.call(expand.dots = FALSE) eval(args$.FUN, args$...) } 

This will write the formula to .FUN and the actual arguments to ... , which can be accessed (like a list) via args$...

Now you can call this function similar to lm :

 > f(x + y, x = 1, y = 2) 3 

As an alternative to match.call you can also use substitute to get a function argument in an invaluable context:

 f <- function (.FUN, ...) eval(substitute(.FUN), list(...)) 
+2
source

Source: https://habr.com/ru/post/1502268/


All Articles