From pd@|gd @end|ng |rom gm@||@com  Fri Apr 19 02:48:56 2019
From: pd@|gd @end|ng |rom gm@||@com (peter dalgaard)
Date: Fri, 19 Apr 2019 02:48:56 +0200
Subject: [Rd] Bug in the "reformulate" function in stats package
In-Reply-To: <ae265c8e-951a-8973-84c0-081a3ca46d97@ist.ac.at>
References: <ac491662-1aad-3775-1936-9e2694da50e6@ist.ac.at>
 <9409b485-7a3b-0f2c-201e-7ef2bde1de86@ist.ac.at>
 <23709.55119.213555.244342@stat.math.ethz.ch>
 <765cd87b-98d6-b23f-9a58-ad817fa5c6e5@ist.ac.at>
 <bad24743-b6e1-f4cd-97a5-8047c27c6707@gmail.com>
 <0c567c84-5c27-128c-584c-022d8d1f6892@gmail.com>
 <23717.41596.382159.209811@stat.math.ethz.ch>
 <ebdebd3e-a522-6fba-4f10-3fea3ea38aae@gmail.com>
 <23719.1502.248949.496054@stat.math.ethz.ch>
 <db32594c-ffed-f41f-b6a8-c13b753d983e@ist.ac.at>
 <d5cf30f0-adcd-a8c3-9bfa-746b88a5582a@gmail.com>
 <ae265c8e-951a-8973-84c0-081a3ca46d97@ist.ac.at>
Message-ID: <41EDD673-A9F7-4545-B440-A79914C6442E@gmail.com>

I think that, also in R core, it is well recognized that it is unfortunate design that some formula manipulation tools rely on going via textual representation of the entire formula.
I'd be strongly tempted to replace the current reformulate() with something like this

> x <- c("a variable","another variable", "anormalone")
> lhs <- Reduce(function(x,y) bquote(.(x)+.(y)), lapply(x, as.name))
> as.formula(bquote(~.(lhs)))
~`a variable` + `another variable` + anormalone

However, there is a fair amount of conservatism because of the existing code base. 
In particular, one needs to watch out for nasty corner cases: E.g., reformulate(c("x","y","x:y")) contains an interaction term, not a regression variable `x:y`. It is not too clear that this is desirable, but it is quite likely that someone's code actually uses it as a feature. Of course, auto-quoting anything that isn't a plain variable name breaks the feature. And there's no progammatic way to tell whether "P/E" is intended as a variable name (price/earnings ratio) or as equal to "P + P:E", so if we want both possibilities there needs to be a way to choose between them.  Which puts us back at square one.

-pd 

> On 18 Apr 2019, at 22:21 , Saren Tasciyan <saren.tasciyan at ist.ac.at> wrote:
> 
> So here is it as txt file. It is funny that a R file is restricted in R-devel mailing list.
> 
> Anyhow, in this case R-core have a few choices here:
> 
> * ignore my solution
> * show that it is actually bad or worse
> * consider adding it
> 
> Considering, that it is a minor change from previous version and doesn't bother the existing usage, I saw the necessity to submit it here. But newer solution in the 3.6.0 may solve other problems too. I can't argue against that. This solves my part of the problem, without affecting existing usage of the function.
> 
> If R-core is hard to convince, because this is just who they are, then I should consider moving to other platforms. But so far, it seems to me that they are doing a great job. I don't mind also someone rejecting this tiny fix I have found, which works for me now. I can only thank for their time spent considering it.
> 
> Actually, I had in mind a more complex but cleaner solution with recursive functions to implement any kind of reformulation (not only with "+"). But I simple lack the big picture on R expressions, I need to read more. Maybe I will come back with that in the future.
> 
> Cheers to all,
> 
> Saren
> 
> On 18.04.19 17:51, Ben Bolker wrote:
>>   I appreciate your enthusiasm and persistence for this issue, but I
>> suspect you may have trouble convincing R-core to adopt your changes --
>> they are "better", "easier", "more intuitive" for you ... but how sure
>> are you they are completely backward compatible, have no performance
>> issues, will not break in unusual cases ... ?
> -- 
> Saren Tasciyan
> /PhD Student / Sixt Group/
> Institute of Science and Technology Austria
> Am Campus 1
> 3400 Klosterneuburg, Austria
> 
> 
> <reformulate_solution.txt>______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Bug in the "reformulate" function in stats package
peter dalgaard