[Rd] model.matrix troubles with AlgDesign

Heather Turner Heather.Turner at warwick.ac.uk
Tue Sep 29 22:25:49 CEST 2009


Ah ok, I get the problem now.

I think you just need to modify model.matrix.formula as follows

 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")
         env <- environment(frml)
         frml <- expand.formula(frml, colnames(data))
         environment(frml) <- env
     }
     model.matrix.default(frml, data, ...)
 }

or the equivalent could be done inside expand.formula, which might be
neater actually.

Hope that fixes it,

Heather

Ulrike Groemping wrote:
> Hi Heather, 
> 
> thanks, I would be able to add the variable y <- 1:nrow(aus), I would even
> be able to omit these. 
> What concerns me is the fact that the presence of function
> model.matrix.formula in package AlgDesign will stop model.matrix from
> working as expected in further situations that I have not spotted yet. 
> 
> For the future development of package DoE.wrapper, I would like to include
> AlgDesign in the Depends field, and that would mean that the package would
> then stop user functions with perfectly valid code from working correctly.
> So I would like to find a way to modify model.matrix.formula or
> expand.formula from AlgDesign so that AlgDesign stops messing up usages of
> model.matrix in other contexts. I think that the solution may perhaps lie in
> assigning the right environment to frml in expand.formula, but I am not
> familiar enough with assigning environments to know what the right strategy
> would be.
> 
> Any suggestions ?
> 
> Regards, 
> Ulrike
> 
> 
> 
> Heather Turner wrote:
>> 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
>>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
> 

-- 
Dr H Turner
Senior Research Fellow
Dept. of Statistics
The University of Warwick
Coventry CV4 7AL
Tel: 024 76575754
Fax: 024 76524532
Url: www.warwick.ac.uk/go/heatherturner



More information about the R-devel mailing list