[R] Issue with formula conversion

2014-08-27 Thread Gang Chen
A random effect formulation for R package nlme is read in as a string
of characters from an input file:

ranEff - pdCompSymm(~1+Age)

I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
~1 + Age

the pdCompSymm is lost in the conversion. Any solutions? Thanks!

Gang

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread David Winsemius

On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:
 
 ranEff - pdCompSymm(~1+Age)
 
 I need to convert 'ranEff' to a formula class. However, as shown below:
 
 as.formula(ranEff)
 ~1 + Age
 
 the pdCompSymm is lost in the conversion. Any solutions? 

as.formula(paste(~,ranEff))
~pdCompSymm(~1 + Age)
-- 

David Winsemius
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread Gang Chen
Thanks for the help! However, I just need to get

pdCompSymm(~1 + Age)

without a tilde (~) at the beginning.

On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:

 ranEff - pdCompSymm(~1+Age)

 I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
 ~1 + Age

 the pdCompSymm is lost in the conversion. Any solutions?

 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --

 David Winsemius
 Alameda, CA, USA


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread David Winsemius

On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:

 Thanks for the help! However, I just need to get
 
 pdCompSymm(~1 + Age)

That's not a formula in the R sense of the word. You should do a better job of 
posting a use case. Perhaps you want an expression?

-- 
David.
 
 without a tilde (~) at the beginning.
 
 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 
 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:
 
 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:
 
 ranEff - pdCompSymm(~1+Age)
 
 I need to convert 'ranEff' to a formula class. However, as shown below:
 
 as.formula(ranEff)
 ~1 + Age
 
 the pdCompSymm is lost in the conversion. Any solutions?
 
 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --
 
 David Winsemius
 Alameda, CA, USA
 

David Winsemius
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread Gang Chen
Good point!

Here is an example:

library(nlme)
fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdComSymm(~Variety-1)))

Now the problem I'm facing is that the following part

pdComSymm(~Variety-1)

is read in as a string of characters from an external source:

ranEff - 'pdComSymm(~Variety-1)'

The following

(ranEff1 - as.formula(ranEff))
~Variety - 1

is not what I want. Even though

fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))

works, I don't know the 'pdCompSymm' part in advance and would like to
make the process automatic.

On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:

 Thanks for the help! However, I just need to get

 pdCompSymm(~1 + Age)

 That's not a formula in the R sense of the word. You should do a better job 
 of posting a use case. Perhaps you want an expression?

 --
 David.

 without a tilde (~) at the beginning.

 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:

 ranEff - pdCompSymm(~1+Age)

 I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
 ~1 + Age

 the pdCompSymm is lost in the conversion. Any solutions?

 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --

 David Winsemius
 Alameda, CA, USA


 David Winsemius
 Alameda, CA, USA


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread David Winsemius

On Aug 27, 2014, at 1:11 PM, Gang Chen wrote:

 Good point!
 
 Here is an example:
 
 library(nlme)
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdComSymm(~Variety-1)))
 

One problem is that youa re misspelling the function name.


 Now the problem I'm facing is that the following part
 
 pdComSymm(~Variety-1)
 
 is read in as a string of characters from an external source:
 
 ranEff - 'pdComSymm(~Variety-1)'
 
 The following
 
 (ranEff1 - as.formula(ranEff))
 ~Variety - 1
 
 is not what I want. Even though
 
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))

 ranEff - 'pdCompSymm(~Variety-1)'
 tt - parse(text=ranEff)

# Returns an unevaluated expression

 fm - lme(yield ~ nitro, data=Oats, random=list(Block=eval(tt) ))
 fm
Linear mixed-effects model fit by REML
  Data: Oats 
  Log-restricted-likelihood: -296.5209
  Fixed: yield ~ nitro 
(Intercept)   nitro 
   81.8722273.7 
snipped rest of output.

-- 
David

 
 works, I don't know the 'pdCompSymm' part in advance and would like to
 make the process automatic.
 
 On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 
 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:
 
 Thanks for the help! However, I just need to get
 
 pdCompSymm(~1 + Age)
 
 That's not a formula in the R sense of the word. You should do a better job 
 of posting a use case. Perhaps you want an expression?
 
 --
 David.
 
 without a tilde (~) at the beginning.
 
 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 
 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:
 
 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:
 
 ranEff - pdCompSymm(~1+Age)
 
 I need to convert 'ranEff' to a formula class. However, as shown below:
 
 as.formula(ranEff)
 ~1 + Age
 
 the pdCompSymm is lost in the conversion. Any solutions?
 
 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --
 
 David Winsemius
 Alameda, CA, USA
 
 
 David Winsemius
 Alameda, CA, USA
 

David Winsemius
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread Gang Chen
Sorry for the misspelling! And more importantly, thanks a lot for the
nice solution and for the quick help!

On Wed, Aug 27, 2014 at 4:22 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Aug 27, 2014, at 1:11 PM, Gang Chen wrote:

 Good point!

 Here is an example:

 library(nlme)
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdComSymm(~Variety-1)))


 One problem is that youa re misspelling the function name.


 Now the problem I'm facing is that the following part

 pdComSymm(~Variety-1)

 is read in as a string of characters from an external source:

 ranEff - 'pdComSymm(~Variety-1)'

 The following

 (ranEff1 - as.formula(ranEff))
 ~Variety - 1

 is not what I want. Even though

 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))

 ranEff - 'pdCompSymm(~Variety-1)'
 tt - parse(text=ranEff)

 # Returns an unevaluated expression

 fm - lme(yield ~ nitro, data=Oats, random=list(Block=eval(tt) ))
 fm
 Linear mixed-effects model fit by REML
   Data: Oats
   Log-restricted-likelihood: -296.5209
   Fixed: yield ~ nitro
 (Intercept)   nitro
81.8722273.7
 snipped rest of output.

 --
 David


 works, I don't know the 'pdCompSymm' part in advance and would like to
 make the process automatic.

 On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:

 Thanks for the help! However, I just need to get

 pdCompSymm(~1 + Age)

 That's not a formula in the R sense of the word. You should do a better job 
 of posting a use case. Perhaps you want an expression?

 --
 David.

 without a tilde (~) at the beginning.

 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:

 ranEff - pdCompSymm(~1+Age)

 I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
 ~1 + Age

 the pdCompSymm is lost in the conversion. Any solutions?

 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --

 David Winsemius
 Alameda, CA, USA


 David Winsemius
 Alameda, CA, USA


 David Winsemius
 Alameda, CA, USA


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread Richard M. Heiberger
do you have control over the external source?

if so, then something like

BlockFunction - pdComSymm
ranEff1 - ~Variety -1

fm - lme(yield ~ nitro, data=Oats,
random=list(Block=get(BlockFunction)(ranEff1)))

The above is untested.  An example if get() is
 get(sum)(1:4)
[1] 10

The main problem with David's solution, which does work, is the use
of the eval(parse()) idiom.  This is usually strongly discouraged.  See, for
example,

 fortunes::fortune(106)

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)

On Wed, Aug 27, 2014 at 4:11 PM, Gang Chen gangch...@gmail.com wrote:
 Good point!

 Here is an example:

 library(nlme)
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdComSymm(~Variety-1)))

 Now the problem I'm facing is that the following part

 pdComSymm(~Variety-1)

 is read in as a string of characters from an external source:

 ranEff - 'pdComSymm(~Variety-1)'

 The following

 (ranEff1 - as.formula(ranEff))
 ~Variety - 1

 is not what I want. Even though

 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))

 works, I don't know the 'pdCompSymm' part in advance and would like to
 make the process automatic.

 On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:

 Thanks for the help! However, I just need to get

 pdCompSymm(~1 + Age)

 That's not a formula in the R sense of the word. You should do a better job 
 of posting a use case. Perhaps you want an expression?

 --
 David.

 without a tilde (~) at the beginning.

 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:

 ranEff - pdCompSymm(~1+Age)

 I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
 ~1 + Age

 the pdCompSymm is lost in the conversion. Any solutions?

 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --

 David Winsemius
 Alameda, CA, USA


 David Winsemius
 Alameda, CA, USA


 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread David Winsemius

On Aug 27, 2014, at 1:33 PM, Richard M. Heiberger wrote:

 do you have control over the external source?
 
 if so, then something like
 
 BlockFunction - pdComSymm
 ranEff1 - ~Variety -1

I doubt that would work, since it is not a formula object.
 
 fm - lme(yield ~ nitro, data=Oats,
 random=list(Block=get(BlockFunction)(ranEff1)))

After correcting the misspelling of the function name, I tested this approach:

 BlockFunction - pdCompSymm
 ranEff1 - as.formula(~Variety -1)
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=do.call(BlockFunction, 
list(form=ranEff1) )
  ))
 fm
#--
Linear mixed-effects model fit by REML
  Data: Oats 
  Log-restricted-likelihood: -296.5209
  Fixed: yield ~ nitro 
snipped

-- 
David.
 
 The above is untested.  An example if get() is
 get(sum)(1:4)
 [1] 10
 
 The main problem with David's solution, which does work, is the use
 of the eval(parse()) idiom.  This is usually strongly discouraged.  See, for
 example,
 
 fortunes::fortune(106)
 
 If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)
 
 On Wed, Aug 27, 2014 at 4:11 PM, Gang Chen gangch...@gmail.com wrote:
 Good point!
 
 Here is an example:
 
 library(nlme)
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdComSymm(~Variety-1)))
 
 Now the problem I'm facing is that the following part
 
 pdComSymm(~Variety-1)
 
 is read in as a string of characters from an external source:
 
 ranEff - 'pdComSymm(~Variety-1)'
 
 The following
 
 (ranEff1 - as.formula(ranEff))
 ~Variety - 1
 
 is not what I want. Even though
 
 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))
 
 works, I don't know the 'pdCompSymm' part in advance and would like to
 make the process automatic.
 
 On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 
 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:
 
 Thanks for the help! However, I just need to get
 
 pdCompSymm(~1 + Age)
 
 That's not a formula in the R sense of the word. You should do a better job 
 of posting a use case. Perhaps you want an expression?
 
 --
 David.
 
 without a tilde (~) at the beginning.
 
 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:
 
 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:
 
 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:
 
 ranEff - pdCompSymm(~1+Age)
 
 I need to convert 'ranEff' to a formula class. However, as shown below:
 
 as.formula(ranEff)
 ~1 + Age
 
 the pdCompSymm is lost in the conversion. Any solutions?
 
 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --
 
 David Winsemius
 Alameda, CA, USA
 
 
 David Winsemius
 Alameda, CA, USA
 
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Issue with formula conversion

2014-08-27 Thread Richard M. Heiberger
David,

you caught my typo of excess quotation marks.  this should work

ranEff1 - ~Variety -1
random=list(Block=get(BlockFunction)(ranEff1)))

On Wed, Aug 27, 2014 at 4:41 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Aug 27, 2014, at 1:33 PM, Richard M. Heiberger wrote:

 do you have control over the external source?

 if so, then something like

 BlockFunction - pdComSymm
 ranEff1 - ~Variety -1

 I doubt that would work, since it is not a formula object.

 fm - lme(yield ~ nitro, data=Oats,
 random=list(Block=get(BlockFunction)(ranEff1)))

 After correcting the misspelling of the function name, I tested this approach:

  BlockFunction - pdCompSymm
  ranEff1 - as.formula(~Variety -1)
  fm - lme(yield ~ nitro, data=Oats, random=list(Block=do.call(BlockFunction, 
 list(form=ranEff1) )
   ))
  fm
 #--
 Linear mixed-effects model fit by REML
   Data: Oats
   Log-restricted-likelihood: -296.5209
   Fixed: yield ~ nitro
 snipped

 --
 David.

 The above is untested.  An example if get() is
 get(sum)(1:4)
 [1] 10

 The main problem with David's solution, which does work, is the use
 of the eval(parse()) idiom.  This is usually strongly discouraged.  See, for
 example,

 fortunes::fortune(106)

 If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)

 On Wed, Aug 27, 2014 at 4:11 PM, Gang Chen gangch...@gmail.com wrote:
 Good point!

 Here is an example:

 library(nlme)
 fm - lme(yield ~ nitro, data=Oats, 
 random=list(Block=pdComSymm(~Variety-1)))

 Now the problem I'm facing is that the following part

 pdComSymm(~Variety-1)

 is read in as a string of characters from an external source:

 ranEff - 'pdComSymm(~Variety-1)'

 The following

 (ranEff1 - as.formula(ranEff))
 ~Variety - 1

 is not what I want. Even though

 fm - lme(yield ~ nitro, data=Oats, random=list(Block=pdCompSymm(ranEff1)))

 works, I don't know the 'pdCompSymm' part in advance and would like to
 make the process automatic.

 On Wed, Aug 27, 2014 at 3:49 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 12:44 PM, Gang Chen wrote:

 Thanks for the help! However, I just need to get

 pdCompSymm(~1 + Age)

 That's not a formula in the R sense of the word. You should do a better 
 job of posting a use case. Perhaps you want an expression?

 --
 David.

 without a tilde (~) at the beginning.

 On Wed, Aug 27, 2014 at 3:34 PM, David Winsemius dwinsem...@comcast.net 
 wrote:

 On Aug 27, 2014, at 11:19 AM, Gang Chen wrote:

 A random effect formulation for R package nlme is read in as a string
 of characters from an input file:

 ranEff - pdCompSymm(~1+Age)

 I need to convert 'ranEff' to a formula class. However, as shown below:

 as.formula(ranEff)
 ~1 + Age

 the pdCompSymm is lost in the conversion. Any solutions?

 as.formula(paste(~,ranEff))
 ~pdCompSymm(~1 + Age)
 --

 David Winsemius
 Alameda, CA, USA


 David Winsemius
 Alameda, CA, USA


 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

 David Winsemius
 Alameda, CA, USA


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.