From S.Ellison at LGCGroup.com  Wed Oct 15 17:50:10 2014
From: S.Ellison at LGCGroup.com (S Ellison)
Date: Wed, 15 Oct 2014 16:50:10 +0100
Subject: [Rd] Advice on package design for handling of dots in a formula
In-Reply-To: <0b3f01cfe881$e3613ea0$aa23bbe0$@gmail.com>
References: <0b3f01cfe881$e3613ea0$aa23bbe0$@gmail.com>
Message-ID: <A4E5A0B016B8CB41A485FC629B633CED6412B87D30@GOLD.corp.lgc-group.com>

> This seems to be a common approach in other packages. However, one of my
> testers noted that if he put formula=y~. then w, ID, and site showed up in the
> model where they weren't supposed to be. 

This is the documented behaviour for '.' in a formula - it means 'everything else in the data object'

Without changing your current code, though, your user could have said something like
y~.-w-ID-site

if they wanted to specify 'everything _except_ the subtracted terms', so it's not as bad as having no shortcuts at all.

If you want to do the work for them, one (probably crude) way of doing it could use drop.terms() in combination with some work with the term labels:

#A function that drops the terms in two later arguments from the terms in the first and returns the resulting trimmed terms object.
f <- function(form, dropthis, dropthattoo, data) {
	everything <- attr(terms(form, data=data), "term.labels") #needs data to expand '.'
	drops <- c(attr(terms(dropthis, data=data), "term.labels"), 
			attr(terms(dropthattoo, data=data), "term.labels")) #could probably do without 'data'
	excludes <-which(everything %in% drops)
	terms(form, data=data)[-excludes]
}

d <- data.frame(a=1:10, b=10:1, g=gl(5,2), g2=gl(2,5), y=rnorm(10))

f(y~., ~g, ~b, data=d)
	#This returns a terms object, but there's a formula in that if you want it....

formula(f(y~., ~g, ~b, data=d))

 You'll need to be careful about evaluating that though; don't forget to give any relevant model or model matrix functions the environment (data frame) to go with it or you'll get nonsense.


S


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



[Rd] Advice on package design for handling of dots in a formula
S Ellison