Skip to content

GLS, as.formula and subsitute

3 messages · Christoph Scherber, Emmanuel Curis, Sven Hohenstein

#
Dear all,

I am trying to feed a formula object into GLS using substitute() and as.formula(). What am I doing
wrong here?

##
models <- function(resp=quote(resp),DF=quote(DF)) {
f1=substitute(resp~Time,list(resp=quote(resp)))
gls(as.formula(f1),data=DF)
}

models(resp="follicles","Ovary")

##
Many thanks for your help!

Best wishes
Christoph
#
Why not use character strings?

models <- function( resp, DF ) {
  frm <- as.formula( paste( resp, 'Time', sep = '~' ) )
  md <- glm( frm, data = DF )
}

models( resp = 'follicles', DF = Ovary )

should work...

Best regards,
On Tue, May 14, 2013 at 10:32:18AM +0200, Christoph Scherber wrote:
? Dear all,
? 
? I am trying to feed a formula object into GLS using substitute() and as.formula(). What am I doing
? wrong here?
? 
? ##
? models <- function(resp=quote(resp),DF=quote(DF)) {
? f1=substitute(resp~Time,list(resp=quote(resp)))
? gls(as.formula(f1),data=DF)
? }
? 
? models(resp="follicles","Ovary")
? 
? ##
? Many thanks for your help!
? 
? Best wishes
? Christoph
? 
? -- 
? PD Dr Christoph Scherber
? Georg-August University Goettingen
? Department of Crop Science
? Agroecology
? Grisebachstrasse 6
? D-37077 Goettingen
? Germany
? phone 0049 (0)551 39 8807
? fax 0049 (0)551 39 8806
? http://www.gwdg.de/~cscherb1

? _______________________________________________
? R-sig-mixed-models at r-project.org mailing list
? https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
#
Dear Christoph,

(1) The function substitute is used for expressions, not for formulas.  
You can dynamically create a formula with the reformulate function:

reformulate("Time", resp)

The first argument denotes the right-hand side of the formula, the  
second argument denotes the response.

(2) In your example, DF refers to the string "Ovary". This is not the  
same as the object named Ovary. To access an object by its name  
string, the function get can be used.

The complete code:

##
library(nlme) # for gls()

models <- function(resp = resp, DF = DF) {
   f1 <- reformulate("Time", resp)
   gls(f1, data = get(DF))
}

models(resp = "follicles", DF = "Ovary")
##

Best,
Sven


Quoting Christoph Scherber <christoph.scherber at agr.uni-goettingen.de>: