From groemping at bht-berlin.de  Mon Sep 28 10:00:24 2009
From: groemping at bht-berlin.de (Ulrike Groemping)
Date: Mon, 28 Sep 2009 01:00:24 -0700 (PDT)
Subject: [Rd]  model.matrix troubles with AlgDesign
Message-ID: <25641680.post@talk.nabble.com>


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

-- 
View this message in context: http://www.nabble.com/model.matrix-troubles-with-AlgDesign-tp25641680p25641680.html
Sent from the R devel mailing list archive at Nabble.com.



[Rd] model.matrix troubles with AlgDesign
Ulrike Groemping