[Rd] model.matrix troubles with AlgDesign

Ulrike Groemping groemping at bht-berlin.de
Tue Sep 29 18:58:12 CEST 2009


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
> 
> 

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



More information about the R-devel mailing list