Skip to content

Problem with correct usage of formula environment

2 messages · Ulrike Grömping

#
Dear all, 

I am working on a function formula.design that should automatically generate
reasonable lm formulae for a number of different designs. All works well as
long as all variables used are columns of the design data frame. For one
function, I would like to incorporate a dummy variable for center points
that is not a column of the design. Without this function, it would work
like this (at least version 0.97 of package FrF2):

require(FrF2)
plan <- FrF2(8,4,ncenter=6)
y <- rnorm(14)
r.plan<-add.response(plan,y)
lm(y~A+B+C+D+!iscube(r.plan), r.plan)

Building the formula from within function formula.design works OK at first
sight:
y ~ A + B + C + D + (!iscube(r.plan))

However, using the formula in a linear model call does not work in this
case:
Fehler in inherits(x, "factor") : Objekt 'object' nicht gefunden
12: inherits(x, "factor")
11: is.factor(table)
10: match(x, table, nomatch = 0L)
9: "design" %in% class(design)
8: iscube(object)
7: eval(expr, envir, enclos)
6: eval(predvars, data, env)
5: model.frame.default(formula = r.plan, drop.unused.levels = TRUE)
4: model.frame(formula = r.plan, drop.unused.levels = TRUE)
3: eval(expr, envir, enclos)
2: eval(mf, parent.frame())
1: lm(r.plan)

9: of the traceback is the first line of function iscube, and it seems that
the object r.plan cannot be found here. I have no idea how to fix this. In
case it helps: The expression generating the formula in function
formula.design is

as.formula(paste(response, paste(c(names(factor.names),
paste("!iscube(",xnam,")",sep="")), collapse="+"),sep="~"))

I am grateful for any pointers that direct me to a fix for this problem
and/or to a better understanding of its root cause.

Thanks and regards,
Ulrike
1 day later
#
I have found a fix now - I define the variable "center" (that takes the role
of the !iscube below) within the function formula.design, and then using
"formula" instead of "as.formula" solves the issue, because it preserves the
calling environment - first time I get an idea what this is good for ...

I would still be grateful for a pointer to a readable source on the
connection between model frames, data=arguments and environments in the
context of models and formulae. 

With best regards, 
Ulrike Gr?mping
Ulrike Groemping wrote: