[Rd] model.matrix troubles with AlgDesign

Heather Turner Heather.Turner at warwick.ac.uk
Tue Sep 29 18:40:24 CEST 2009


Hi Ulrike,

It looks like 'aus' is created by fac.design(); is there any reason why
you can't add the variable
y <- 1:nrow(aus)
to this data frame and use y as the response in your formula?

Otherwise I think aus needs to be in the environment of frml (or frml
needs to be given the environment of aus).

Heather

Ulrike Groemping wrote:
> Dear DevelopeRs,
> 
> in continuing with my suite of packages on experimental design, I am stuck
> with an issue that appears to be related to package AlgDesign - I have tried
> to get it solved by Bob Wheeler, but he seems to be stuck as well. 
> 
> Whenever AlgDesign is loaded, some of my code does not work any more. For
> example, in a fresh R session: 
> 
> require(DoE.base)
> fac.design(nlevels=c(2,6,2))
> require(AlgDesign)
> fac.design(nlevels=c(2,6,2))
>> Error in nrow(aus) : object 'aus' not found 
> 
> The reason seems to be that AlgDesign creates a function
> model.matrix.formula that only finds variables that are in the global
> environment and variables that are in the data frame given with the formula,
> but not calculation results from the intermediate calling environment. 
> 
> Results from traceback():
> 9: nrow(aus)
> 8: eval(expr, envir, enclos)
> 7: eval(predvars, data, env)
> 6: model.frame.default(object, data, xlev = xlev)
> 5: model.frame(object, data, xlev = xlev)
> 4: model.matrix.default(frml, data, ...)
> 3: model.matrix.formula(1:nrow(aus) ~ ., data = aus)
> 2: model.matrix(1:nrow(aus) ~ ., data = aus)
> 1: fac.design(nlevels = c(2, 6, 2))
> 
> If I reset model.matrix.formula to model.matrix.default, the problem
> disappears (but AlgDesign's comfort functions for squares etc. do not work
> any longer). In this particular case, I can also avoid the issue by
> modifying the formula in fac.design, removing the left-hand side. But this
> just means to wait for the next place where troubles occur. Between 3 and 4
> of the traceback(), AlgDesign's function model.matrix.formula modifies the
> formula frml using AlgDesign's function expand.formula:
> 
> model.matrix.formula <- function (frml, data = sys.frame(sys.parent()), ...) 
> {
>     if (!missing(data)) {
>         if (!inherits(data, "data.frame")) 
>             stop("data must be a data.frame")
>         if (!inherits(frml, "formula")) 
>             stop("frml must be a formuls")
>         frml <- expand.formula(frml, colnames(data))
>     }
>     model.matrix.default(frml, data, ...)
> }
> 
> 
> I have looked at expand.formula as well, and I've been wondering whether a
> simple fix can be found by adding environment information (which?) within
> that function (I believe that the relevant portion of the code is included
> below):
> 
> expand.formula <- function (frml, varNames, const = TRUE, numerics = NULL) 
> {
> ## omitted quite a bit of code 
> ##...
>     frml <- deparse(frml, width = 500)
>     while ((0 != (pos <- findFunction("quad", frml))[1]) || (0 != 
>         (pos <- findFunction("cubicS", frml))[1]) || (0 != (pos <-
> findFunction("cubic", 
>         frml))[1])) {
>         prog <- substr(frml, pos[1], pos[2])
>         strHead <- substr(frml, 1, pos[1] - 1)
>         strTail <- substr(frml, pos[2] + 1, nchar(frml))
>         prog <- eval(parse(text = prog))
>         frml <- paste(strHead, prog, strTail, sep = "")
>     }
>     if (0 != (pos <- findDot(".", frml))[1]) {
>         strHead <- substr(frml, 1, pos[1] - 1)
>         strTail <- substr(frml, pos[2] + 1, nchar(frml))
>         prog <- eval(parse(text = "doDot()"))
>         frml <- paste(strHead, prog, strTail, sep = "")
>     }
>     if (!const) 
>         frml <- paste(frml, "+0", sep = "")
>     frml <- as.formula(frml)
>     frml
> }
> 
> Any help would be greatly appreciated.
> 
> Regards, Ulrike
>



More information about the R-devel mailing list