Skip to content

Using objects within functions in formulas

5 messages · Rui Barradas, David Winsemius, Ben Bolker +1 more

#
Dear all,

I'm looking to create a formula within a function to pass to glmer()
and I'm having a problem that the following example will illustrate:

library(lme4)
y1 = rnorm(10)
x1 = data.frame(x11=rnorm(10), x12=rnorm(10), x13=rnorm(10))
x1 = data.matrix(x1)
w1 = data.frame(w11=sample(1:3,10, replace=TRUE), w12=sample(1:3,10,
replace=TRUE), w13=sample(1:3,10, replace=TRUE))

test1 <- function(x2, y2, w2) {

	print(str(w2))
	form = as.formula(paste("y2 ~ x2 +" ,paste("(1|w2$", names(w2), ")",
collapse=" + ", sep="")))
	m1 = glmer(form)
	return(m1)
}

model1 = test1(x2=x1, y2=y1, w2=w1)

As can be seen from the print statement within the function, the
object "w2" is present and is a data frame. However, the following
error occurs:

Error in is.factor(x) : object 'w2' not found

This can be rectified by making 'w2' global - defining it outside the
function. I know there are issues with defining formulas and
environment but I'm not sure why this problem is specific to 'w2' and
not the other objects passed to the function.

Any help would be appreciated.

Aidan MacNamara
EMBL-EBI
#
Hello,

Try the following. It uses argument 'data' to pass the data.frame w2. In 
the function below, I've changed the pastes to two lines of code because 
the first one changes the way the formula is put together.

test1 <- function(x2, y2, w2) {
     #print(str(w2))
     p1 <- paste("(1|", names(w2), ")", collapse=" + ", sep="")
     p2 <- paste("y2 ~ x2 +" , p1)
     form = as.formula(p2)
     m1 = glmer(form, data = w2)
     return(m1)
}


Hope this helps,

Rui Barradas
Em 09-01-2013 16:53, Aidan MacNamara escreveu:
#
On Jan 9, 2013, at 8:53 AM, Aidan MacNamara wrote:

            
Generally regression functions in R will be expecting to get one  
'data' argument and build formulas using column names from that object.

  test1 <- function(x2, y2, w2) {
    w3 <- cbind(w2, x2, x2)
	print(str(w3))
	form = as.formula(paste("y2 ~ x2 +" ,paste("(1|", names(w2), ")",
collapse=" + ", sep="")))
	m1 = glmer(form, data=w3); print(summary(m1))
	return(m1)
     }

model1 = test1(x2=x1, y2=y1, w2=w1)
David Winsemius, MD
Alameda, CA, USA
#
David Winsemius <dwinsemius <at> comcast.net> writes:
[snip David's solution to try to make gmane happy about the amount
of quoted material]
I haven't had a chance to look at this, but I will try to get to it.
It would help if you could post it on the "Issues" page of the lme4
github site, https://github.com/lme4/lme4/ .  The bottom line is that
dealing appropriately with all the different possible ways to assign
and evaluate variables within formulas is trickier than I would like
it to be.  To the best of my knowledge I have solved most of these
problems in the development version of lme4, but another test case
will be useful.  As long as there is a reasonable workaround I'm unlikely
to put the effort into fixing the stable version of lme4 (sorry ...) 

  Follow-ups to r-sig-mixed-models at r-project.org or (preferably)
to the aforementioned "Issues" list.

  Ben Bolker
#
Thanks everyone, very helpful.
On 9 January 2013 18:33, David Winsemius <dwinsemius at comcast.net> wrote: