Re: [Rd] two small wishes (with code sugegstions) for R-core

2009-11-18 Thread Prof Brian Ripley
I've incorporated a more correct version of 1: think about what 
happens if the next method also copies over the class, for example.
(It was more complicated than that: [.AsIs was used for other classes 
in R -- did you actually test your suggestion, as 'make check' failed 
for me?)


But write.csv[2] are just wrappers, and if you want to do something 
complicated simply don't use them.  They are intended to help naive 
users of .csv files get them right, and banning append=TRUE seems the 
best way to help do that.  (A recent R-help thread shows that 
non-users of Excel are often unaware of its exact requirements -- and 
that includes me.)


The wishes may be small: the work to implement and test them is often 
not.


On Mon, 2 Nov 2009, Jens Oehlschlägel wrote:


Dear R developers,

It would be great if you could implement the two minor code changes suggested 
below, which would help processing large objects in R.


Jens Oehlschlägel


# Wish no. 1: let [.AsIs return the class AFTER subsetting, not the class of 
the original object
# Wish no. 2: adjust write.csv and write.csv2 for multiple calls in chunked 
writing

# Rationale no. 1: a couple of packages will return a different class than 
SomeClass when subsetting with [.SomeClass
# and still need to keep the AsIs property
# Examples for classes returning different classes on subscipting are in 
packages 'bit', 'ff', 'bigmemory'
# For classes where [.SomeClass will return class SomeClass, such a change will 
not hurt

# Code suggestion no. 1: please use
"[.AsIs" <- function (x, i, ...){
 ret <- NextMethod("[")
 oldClass(ret) <- c("AsIs", oldClass(ret))
 ret
}
# instead of
"[.AsIs" <- function (x, i, ...)
structure(NextMethod("["), class = class(x))


# Rationale no. 2: write.csv and write.csv2 currently enforce that a header 
must be written, even with append=TRUE
# This prevents a csv file being written in chunks.
# If argument append=TRUE is used, a header should not be enforced (may be even 
be forbidden)

# Code suggestion no. 2: please use
write.csv <-
function (...)
{
   Call <- match.call(write.table, expand.dots = TRUE)
   for (argname in c("col.names", "sep", "dec", "qmethod")) if 
(!is.null(Call[[argname]]))
   warning(gettextf("attempt to set '%s' ignored", argname),
   domain = NA)
   rn <- eval.parent(Call$row.names)
   ap <- eval.parent(Call$append)
   Call$col.names <- if (is.logical(ap) && ap) FALSE else {if (is.logical(rn) 
&& !rn) TRUE else NA}
   Call$sep <- ","
   Call$dec <- "."
   Call$qmethod <- "double"
   Call[[1L]] <- as.name("write.table")
   eval.parent(Call)
}
write.csv2 <-
function (...)
{
   Call <- match.call(write.table, expand.dots = TRUE)
   for (argname in c("col.names", "sep", "dec", "qmethod")) if 
(!is.null(Call[[argname]]))
   warning(gettextf("attempt to set '%s' ignored", argname),
   domain = NA)
   rn <- eval.parent(Call$row.names)
   ap <- eval.parent(Call$append)
   Call$col.names <- if (is.logical(ap) && ap) FALSE else {if (is.logical(rn) 
&& !rn) TRUE else NA}
   Call$sep <- ";"
   Call$dec <- ","
   Call$qmethod <- "double"
   Call[[1L]] <- as.name("write.table")
   eval.parent(Call)
}
# instead of
write.csv <- function (...)
{
   Call <- match.call(expand.dots = TRUE)
   for (argname in c("col.names", "sep", "dec", "qmethod")) if 
(!is.null(Call[[argname]]))
   warning(gettextf("attempt to set '%s' ignored", argname),
   domain = NA)
   rn <- eval.parent(Call$row.names)
   Call$col.names <- if (is.logical(rn) && !rn)
   TRUE
   else NA
   Call$sep <- ","
   Call$dec <- "."
   Call$qmethod <- "double"
   Call[[1L]] <- as.name("write.table")
   eval.parent(Call)
}
write.csv2 <-
function (...)
{
   Call <- match.call(expand.dots = TRUE)
   for (argname in c("col.names", "sep", "dec", "qmethod")) if 
(!is.null(Call[[argname]]))
   warning(gettextf("attempt to set '%s' ignored", argname),
   domain = NA)
   rn <- eval.parent(Call$row.names)
   Call$col.names <- if (is.logical(rn) && !rn)
   TRUE
   else NA
   Call$sep <- ";"
   Call$dec <- ","
   Call$qmethod <- "double"
   Call[[1L]] <- as.name("write.table")
   eval.parent(Call)
}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Ulrike Groemping

Dear developeRs,

I have not found anything recent about how to link to a vignette or other
documentation from within Rd-files. Is this now possible with the new help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.

Thanks for any suggestions,
regards, Ulrike 
-- 
View this message in context: 
http://old.nabble.com/Link-to-pdf-documentation-from-other-package---tp26405139p26405139.html
Sent from the R devel mailing list archive at Nabble.com.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Duncan Murdoch

Ulrike Groemping wrote:

Dear developeRs,

I have not found anything recent about how to link to a vignette or other
documentation from within Rd-files. Is this now possible with the new help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.

  


Yes, you can do it with a \url{} macro.  Use a relative link, acting as 
though the starting page is located in /library/AlgDesign/html/*.  So 
\url{../doc/AlgDesign.pdf}
should work from the same package, 
\url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let me 
know if this doesn't work, I haven't tested.


Duncan Murdoch

Thanks for any suggestions,
regards, Ulrike 



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Ulrike Groemping



Duncan Murdoch-2 wrote:
> 
> Ulrike Groemping wrote:
>> Dear developeRs,
>>
>> I have not found anything recent about how to link to a vignette or other
>> documentation from within Rd-files. Is this now possible with the new
>> help
>> system ? For example, I would like to link to the file AlgDesign.pdf
>> provided within the doc directory of package AlgDesign.
>>
>>   
> 
> Yes, you can do it with a \url{} macro.  Use a relative link, acting as 
> though the starting page is located in /library/AlgDesign/html/*.  So 
> \url{../doc/AlgDesign.pdf}
> should work from the same package, 
> \url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let me 
> know if this doesn't work, I haven't tested.
> 
> Duncan Murdoch
> 

Yes, this works, thanks! Is it also possible to display an alternative text
instead of the URL itself, like with other links (e.g. show text "Open pdf"
that operates the link) ?

Regards, Ulrike

-- 
View this message in context: 
http://old.nabble.com/Link-to-pdf-documentation-from-other-package---tp26405139p26406675.html
Sent from the R devel mailing list archive at Nabble.com.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] pt not monotonic with large noncentrality (PR#14069)

2009-11-18 Thread Jerry . Lewis
Full_Name: Jerry W. Lewis
Version: 2.10.0
OS: Windows XP Professional
Submission from: (NULL) (96.237.55.233)


pt(0,3,200)  # correctly returns 0
pt(-1000,3,200)  # erroniously returns 0.003116595

Since pt(0,df,nc) = pnorm(-nc), there is an easily computed upper bound for
pt(-t,df,nc) where t>0 and nc>0.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Duncan Murdoch

Ulrike Groemping wrote:


Duncan Murdoch-2 wrote:
  

Ulrike Groemping wrote:


Dear developeRs,

I have not found anything recent about how to link to a vignette or other
documentation from within Rd-files. Is this now possible with the new
help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.

  
  
Yes, you can do it with a \url{} macro.  Use a relative link, acting as 
though the starting page is located in /library/AlgDesign/html/*.  So 
\url{../doc/AlgDesign.pdf}
should work from the same package, 
\url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let me 
know if this doesn't work, I haven't tested.


Duncan Murdoch




Yes, this works, thanks! Is it also possible to display an alternative text
instead of the URL itself, like with other links (e.g. show text "Open pdf"
that operates the link) ?

  


There's no optional text in the \url{} tag, but you could embed it in an 
\ifelse tag, e.g.


\ifelse{html}{\url{   }}{ [open pdf ...] }

might work.  Using the \ifelse tag will make your package depend on R >= 
2.10.0.


Duncan Murdoch




__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Help with R on windows

2009-11-18 Thread Abhijit Bera
Hi

I have managed to link R.dll to my Visual Studio Win32 GUI project. However
I am unable to see any of the R messages on a console.

I'm hitting a dead end trying to use R_tryEval.

Is there a way I can obtain the error message when R_tryEval fails?

Regards

Abhijit Bera

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Ulrike Grömping

Duncan Murdoch schrieb:

Ulrike Groemping wrote:


Duncan Murdoch-2 wrote:
 

Ulrike Groemping wrote:
   

Dear developeRs,

I have not found anything recent about how to link to a vignette or 
other

documentation from within Rd-files. Is this now possible with the new
help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.


Yes, you can do it with a \url{} macro.  Use a relative link, acting 
as though the starting page is located in 
/library/AlgDesign/html/*.  So \url{../doc/AlgDesign.pdf}
should work from the same package, 
\url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let 
me know if this doesn't work, I haven't tested.


Duncan Murdoch




Yes, this works, thanks! Is it also possible to display an 
alternative text
instead of the URL itself, like with other links (e.g. show text 
"Open pdf"

that operates the link) ?

  


There's no optional text in the \url{} tag, but you could embed it in 
an \ifelse tag, e.g.


\ifelse{html}{\url{   }}{ [open pdf ...] }

might work.  Using the \ifelse tag will make your package depend on R 
>= 2.10.0.


Duncan Murdoch
  
Another thought: the link only works, if the two packages are installed 
in the same library, doesn't it ? Would there be a way to make it work 
independently of this restriction ?


Regards, Ulrike

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread ml-it-r-devel
Ulrike Grömping wrote, On 11/18/09 13:28:
> Duncan Murdoch schrieb:
>> Ulrike Groemping wrote:
>>>
>>> Duncan Murdoch-2 wrote:
>>>  
 Ulrike Groemping wrote:
   
> Dear developeRs,
>
> I have not found anything recent about how to link to a vignette or
> other
> documentation from within Rd-files. Is this now possible with the new
> help
> system ? For example, I would like to link to the file AlgDesign.pdf
> provided within the doc directory of package AlgDesign.
>
> 
 Yes, you can do it with a \url{} macro.  Use a relative link, acting
 as though the starting page is located in
 /library/AlgDesign/html/*.  So \url{../doc/AlgDesign.pdf}
 should work from the same package,
 \url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let
 me know if this doesn't work, I haven't tested.

 Duncan Murdoch

 
>>>
>>> Yes, this works, thanks! Is it also possible to display an
>>> alternative text
>>> instead of the URL itself, like with other links (e.g. show text
>>> "Open pdf"
>>> that operates the link) ?
>>>
>>>   
>>
>> There's no optional text in the \url{} tag, but you could embed it in
>> an \ifelse tag, e.g.
>>
>> \ifelse{html}{\url{   }}{ [open pdf ...] }
>>
>> might work.  Using the \ifelse tag will make your package depend on R
>> >= 2.10.0.
>>
>> Duncan Murdoch
>>>   
> Another thought: the link only works, if the two packages are installed
> in the same library, doesn't it ? Would there be a way to make it work
> independently of this restriction ?

you could use the Sweave like macro to compute the path inside the Rd 
environments

\Sexpr{file.path(system.file(package="AlgDesign", "doc"), "AlgDesign.pdf")}

described in 'Writing R extensions' 2.11


> Regards, Ulrike
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


-- 
Matthias Burger Project Manager/ Biostatistician
Epigenomics AGKleine Praesidentenstr. 110178 Berlin, Germany
phone:+49-30-24345-0fax:+49-30-24345-555
http://www.epigenomics.com   matthias.bur...@epigenomics.com
--
Epigenomics AG Berlin   Amtsgericht Charlottenburg HRB 75861
Vorstand:   Geert Nygaard (CEO/Vorsitzender)
Oliver Schacht PhD (CFO)
Aufsichtsrat:   Prof. Dr. Dr. hc. Rolf Krebs (Chairman/Vorsitzender)

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Duncan Murdoch

On 11/18/2009 7:28 AM, Ulrike Grömping wrote:

Duncan Murdoch schrieb:

Ulrike Groemping wrote:


Duncan Murdoch-2 wrote:
 

Ulrike Groemping wrote:
   

Dear developeRs,

I have not found anything recent about how to link to a vignette or 
other

documentation from within Rd-files. Is this now possible with the new
help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.


Yes, you can do it with a \url{} macro.  Use a relative link, acting 
as though the starting page is located in 
/library/AlgDesign/html/*.  So \url{../doc/AlgDesign.pdf}
should work from the same package, 
\url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let 
me know if this doesn't work, I haven't tested.


Duncan Murdoch




Yes, this works, thanks! Is it also possible to display an 
alternative text
instead of the URL itself, like with other links (e.g. show text 
"Open pdf"

that operates the link) ?

  


There's no optional text in the \url{} tag, but you could embed it in 
an \ifelse tag, e.g.


\ifelse{html}{\url{   }}{ [open pdf ...] }

might work.  Using the \ifelse tag will make your package depend on R 
>= 2.10.0.


Duncan Murdoch
  
Another thought: the link only works, if the two packages are installed 
in the same library, doesn't it ? Would there be a way to make it work 
independently of this restriction ?


With dynamic help, it doesn't matter where they are installed.  If you 
install the static HTML pages, then it matters.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Link to pdf documentation from other package ?

2009-11-18 Thread Duncan Murdoch

On 11/18/2009 8:01 AM, ml-it-r-de...@epigenomics.com wrote:

Ulrike Grömping wrote, On 11/18/09 13:28:

Duncan Murdoch schrieb:

Ulrike Groemping wrote:


Duncan Murdoch-2 wrote:
 

Ulrike Groemping wrote:
  

Dear developeRs,

I have not found anything recent about how to link to a vignette or
other
documentation from within Rd-files. Is this now possible with the new
help
system ? For example, I would like to link to the file AlgDesign.pdf
provided within the doc directory of package AlgDesign.



Yes, you can do it with a \url{} macro.  Use a relative link, acting
as though the starting page is located in
/library/AlgDesign/html/*.  So \url{../doc/AlgDesign.pdf}
should work from the same package,
\url{../../AlgDesign/doc/AlgDesign.pdf} from elsewhere.  Please let
me know if this doesn't work, I haven't tested.

Duncan Murdoch




Yes, this works, thanks! Is it also possible to display an
alternative text
instead of the URL itself, like with other links (e.g. show text
"Open pdf"
that operates the link) ?

  


There's no optional text in the \url{} tag, but you could embed it in
an \ifelse tag, e.g.

\ifelse{html}{\url{   }}{ [open pdf ...] }

might work.  Using the \ifelse tag will make your package depend on R
>= 2.10.0.

Duncan Murdoch
  

Another thought: the link only works, if the two packages are installed
in the same library, doesn't it ? Would there be a way to make it work
independently of this restriction ?


you could use the Sweave like macro to compute the path inside the Rd 
environments

\Sexpr{file.path(system.file(package="AlgDesign", "doc"), "AlgDesign.pdf")}

described in 'Writing R extensions' 2.11




That won't work with dynamic help. (It surprised me, too.)  You can't 
link to the file system from within the dynamic help pages.  You can't 
link to a file:// link, which is what you'd need to get that to work.


This is a browser restriction, for security reasons.  You don't want 
someone's Javascript to be able to read your private files, because it 
could then send them anywhere.


Within the dynamic help, you're also restricted in what files you can 
link to:  they need to be help pages, or files in the doc directory of a 
package, or a few other special cases.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] bug in '...' of constrOptim (PR#14071)

2009-11-18 Thread epurdom
Dear all,

There appears to be a bug in how constrOptim handles ... arguments that 
are suppose to be passed to optim, according to the documentation. This 
means you can't get the hessian to be returned, for example (so this is 
a real problem, and not just a question of mistaken documentation).

Looking at the code, it appears that a call to the user-defined f 
includes the ..., when the ... should only be passed to the optim call. 
  I get around it by putting a dummy 'hessian=TRUE' argument in my f 
function, but this is not how the function should work.

I show the relevant problem in the code, give an example, and also give 
my sessionInfo() call below.

Thanks,
Elizabeth

##Copy of the relevant segment of the code of constrOptim and where I 
think the problem might be:
for (i in 1L:outer.iterations) {
 obj.old <- obj
 r.old <- r
 theta.old <- theta
 fun <- function(theta, ...) { ##this one's okay
 R(theta, theta.old, ...)##this one's okay
 }
 gradient <- function(theta, ...) {##this one's okay
 dR(theta, theta.old, ...)##this one's okay
 }
 a <- optim(theta.old, fun, gradient, control = control,
 method = method, ...)##this one's okay
 r <- a$value
 if (is.finite(r) && is.finite(r.old) && abs(r - 
r.old)/(outer.eps +
 abs(r - r.old)) < outer.eps)
 break
 theta <- a$par
 obj <- f(theta, ...)##this one's NOT okay
 if (obj > obj.old)
 break
 }

###Here is an example modified from the examples of the help page of 
constrOptim:
 > fr <- function(x) {   ## Rosenbrock Banana function
+ x1 <- x[1]
+ x2 <- x[2]
+ 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
+ }
 > grr <- function(x) { ## Gradient of 'fr'
+ x1 <- x[1]
+ x2 <- x[2]
+ c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
+200 *  (x2 - x1 * x1))
+ }
 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), ci=c(-1,-1))
$par
[1] 0.761 0.522

$value
[1] 5.708626e-10

$counts
function gradient
61

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 14

$barrier.value
[1] -0.0001999198

 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
ci=c(-1,-1),hessian=TRUE)
Error in f(theta, ...) : unused argument(s) (hessian = TRUE)
#add in a dummy argument to my f function
 > fr <- function(x,hessian=TRUE) {   ## Rosenbrock Banana function
+ x1 <- x[1]
+ x2 <- x[2]
+ 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
+ }
 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
ci=c(-1,-1),hessian=TRUE)
$par
[1] 0.761 0.522

$value
[1] 5.708626e-10

$counts
function gradient
61

$convergence
[1] 0

$message
NULL

$hessian
   [,1]  [,2]
[1,]  801.9599 -399.9905
[2,] -399.9905  199.9952

$outer.iterations
[1] 14

$barrier.value
[1] -0.0001999198
###My Session info
 > sessionInfo()
R version 2.10.0 (2009-10-26)
x86_64-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] GEOquery_2.10.0RCurl_1.2-1bitops_1.0-4.1 
Biobase_2.5.8  biomaRt_2.1.0  projectManager_1.0 lattice_0.17-26 
RColorBrewer_1.0-2
[9] XML_2.6-0

loaded via a namespace (and not attached):
[1] grid_2.10.0  tools_2.10.0
-- 
Elizabeth Purdom
Assistant Professor
Department of Statistics
UC, Berkeley
Evans Hall, Rm 433
epur...@stat.berkeley.edu
(510) 642-6154 (office)
(510) 642-7892 (fax)

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] bug in '...' of constrOptim (PR#14071)

2009-11-18 Thread Ravi Varadhan
Elizabeth,

If you look at the arguments in optim(), you will see that the only
additional argument that is in optim but not in constrOptim is the
`hessian'.  So, it makes sense to add that explicitly in constrOptim() as
well.  The other additional arguments are passed to optim via the `control'
argument.  So, apart from `control' and `hessian' you cannot pass any other
argument to optim.  

Ravi.


---

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvarad...@jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 





-Original Message-
From: Elizabeth Purdom [mailto:epur...@stat.berkeley.edu] 
Sent: Wednesday, November 18, 2009 5:12 PM
To: Ravi Varadhan
Cc: r-de...@stat.math.ethz.ch; r-b...@r-project.org
Subject: Re: [Rd] bug in '...' of constrOptim (PR#14071)

Hi Ravi,
I just briefly looked at the code (and haven't run it). But it seems 
like there is still the underlying problem that '...' argument is sent 
to both optim and f. The documentation says that ... arguments are sent 
to optim, so at least based on this the ... should only appear in optim. 
This seems prefered since you don't need an explicit argument 'hessian' 
added; after all the user gets to define f, so the flexibility of ... 
there is minimal compared to being able to pass arguments to optim.

Here's the relevant code from what you sent:
a <- optim(theta.old, fun, gradient, control = control,
 method = method, hessian=hessian, ...)
 r <- a$value
 if (is.finite(r) && is.finite(r.old) && abs(r - 
r.old)/(outer.eps +
 abs(r - r.old)) < outer.eps)
 break
 theta <- a$par
 obj <- f(theta, ...) ###I don't think this should have the ...

Best,
Elizabeth

Ravi Varadhan wrote:
> Hi Elizabeth,
> 
> I have fixed this problem.  See the attached function.  There is also
> another bug that can create a problem when control$fnscale = -1, i.e. when
> the objective function is to be maximized.  I have commented this in the
> code and have also fixed this problem.
> 
> There is a word of caution.  Hessian is problematic when the converged
> parameter estimate is on the boundary of the feasible region, since the
> derivatives are not defined there. 
> 
> Also, note that the default method is Nelder-Mead.  If you want to use
> "BFGS", you have to specify the gradient function.  I have written an
> extension of constrOptim that can use numerical gradients, and can also
> handle nonlinear constraints. 
> 
> 
> Hope this helps,
> 
> Ravi.
> 
>

> ---
> 
> Ravi Varadhan, Ph.D.
> 
> Assistant Professor, The Center on Aging and Health
> 
> Division of Geriatric Medicine and Gerontology 
> 
> Johns Hopkins University
> 
> Ph: (410) 502-2619
> 
> Fax: (410) 614-9625
> 
> Email: rvarad...@jhmi.edu
> 
> Webpage:
>
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
> tml
> 
>  
> 
>

> 
> 
> 
> -Original Message-
> From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org]
> On Behalf Of epur...@stat.berkeley.edu
> Sent: Wednesday, November 18, 2009 1:10 PM
> To: r-de...@stat.math.ethz.ch
> Cc: r-b...@r-project.org
> Subject: [Rd] bug in '...' of constrOptim (PR#14071)
> 
> Dear all,
> 
> There appears to be a bug in how constrOptim handles ... arguments that 
> are suppose to be passed to optim, according to the documentation. This 
> means you can't get the hessian to be returned, for example (so this is 
> a real problem, and not just a question of mistaken documentation).
> 
> Looking at the code, it appears that a call to the user-defined f 
> includes the ..., when the ... should only be passed to the optim call. 
>   I get around it by putting a dummy 'hessian=TRUE' argument in my f 
> function, but this is not how the function should work.
> 
> I show the relevant problem in the code, give an example, and also give 
> my sessionInfo() call below.
> 
> Thanks,
> Elizabeth
> 
> ##Copy of the relevant segment of the code of constrOptim and where I 
> think the problem might be:
> for (i in 1L:outer.iterations) {
>  obj.old <- obj
>  r.old <- r
>  theta.old <- theta
>  fun <- function(theta, ...) { ##this one's okay
>  R(theta, theta.old, ...)##this one's okay
>  }
>  gradient <- function(theta, ...) {##this one's okay
>  dR(theta, theta.old, ...)##this one's okay
>  }
>  a <- optim(theta.old, fun, gradient, control = control,
>

Re: [Rd] bug in '...' of constrOptim (PR#14071)

2009-11-18 Thread Ravi Varadhan
Hi Elizabeth,

I have fixed this problem.  See the attached function.  There is also
another bug that can create a problem when control$fnscale = -1, i.e. when
the objective function is to be maximized.  I have commented this in the
code and have also fixed this problem.

There is a word of caution.  Hessian is problematic when the converged
parameter estimate is on the boundary of the feasible region, since the
derivatives are not defined there. 

Also, note that the default method is Nelder-Mead.  If you want to use
"BFGS", you have to specify the gradient function.  I have written an
extension of constrOptim that can use numerical gradients, and can also
handle nonlinear constraints. 


Hope this helps,

Ravi.


---

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvarad...@jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 





-Original Message-
From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org]
On Behalf Of epur...@stat.berkeley.edu
Sent: Wednesday, November 18, 2009 1:10 PM
To: r-de...@stat.math.ethz.ch
Cc: r-b...@r-project.org
Subject: [Rd] bug in '...' of constrOptim (PR#14071)

Dear all,

There appears to be a bug in how constrOptim handles ... arguments that 
are suppose to be passed to optim, according to the documentation. This 
means you can't get the hessian to be returned, for example (so this is 
a real problem, and not just a question of mistaken documentation).

Looking at the code, it appears that a call to the user-defined f 
includes the ..., when the ... should only be passed to the optim call. 
  I get around it by putting a dummy 'hessian=TRUE' argument in my f 
function, but this is not how the function should work.

I show the relevant problem in the code, give an example, and also give 
my sessionInfo() call below.

Thanks,
Elizabeth

##Copy of the relevant segment of the code of constrOptim and where I 
think the problem might be:
for (i in 1L:outer.iterations) {
 obj.old <- obj
 r.old <- r
 theta.old <- theta
 fun <- function(theta, ...) { ##this one's okay
 R(theta, theta.old, ...)##this one's okay
 }
 gradient <- function(theta, ...) {##this one's okay
 dR(theta, theta.old, ...)##this one's okay
 }
 a <- optim(theta.old, fun, gradient, control = control,
 method = method, ...)##this one's okay
 r <- a$value
 if (is.finite(r) && is.finite(r.old) && abs(r - 
r.old)/(outer.eps +
 abs(r - r.old)) < outer.eps)
 break
 theta <- a$par
 obj <- f(theta, ...)##this one's NOT okay
 if (obj > obj.old)
 break
 }

###Here is an example modified from the examples of the help page of 
constrOptim:
 > fr <- function(x) {   ## Rosenbrock Banana function
+ x1 <- x[1]
+ x2 <- x[2]
+ 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
+ }
 > grr <- function(x) { ## Gradient of 'fr'
+ x1 <- x[1]
+ x2 <- x[2]
+ c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
+200 *  (x2 - x1 * x1))
+ }
 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), ci=c(-1,-1))
$par
[1] 0.761 0.522

$value
[1] 5.708626e-10

$counts
function gradient
61

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 14

$barrier.value
[1] -0.0001999198

 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
ci=c(-1,-1),hessian=TRUE)
Error in f(theta, ...) : unused argument(s) (hessian = TRUE)
#add in a dummy argument to my f function
 > fr <- function(x,hessian=TRUE) {   ## Rosenbrock Banana function
+ x1 <- x[1]
+ x2 <- x[2]
+ 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
+ }
 > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
ci=c(-1,-1),hessian=TRUE)
$par
[1] 0.761 0.522

$value
[1] 5.708626e-10

$counts
function gradient
61

$convergence
[1] 0

$message
NULL

$hessian
   [,1]  [,2]
[1,]  801.9599 -399.9905
[2,] -399.9905  199.9952

$outer.iterations
[1] 14

$barrier.value
[1] -0.0001999198
###My Session info
 > sessionInfo()
R version 2.10.0 (2009-10-26)
x86_64-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] GEOquery_2.10.0RCurl_1.2-1bitops_1.0-4.1 
Biobase_2.5.8  biomaRt_2.1.0  projectManager_1.0 lattice_0.17-26 
RColorBrewer_1.0-2
[9] XML_2.6-0

loaded via a namespace (and not attached):
[1] grid_2.10.0  tools_2.10.0
-- 
Elizabeth Purdom
Assistant Professor
Department 

[Rd] Issue when calling deparse(substitute(x)) from C with "anonymous" R vectors ?

2009-11-18 Thread Laurent Gautier

Dear list,


When calling R from C, what appears like a spurious error can be 
triggered during the execution of chisq.test(x, y).


This is happening when the following conditions are met:

- x and y are "anonymous" C-level R vectors (they do not have a symbol),
but they are protected from garbage collection

- x and y are "not too small" (it was experienced as soon as they are 
longer than 17 elements).



The error is

Error in names(dimnames(x)) <- DNAME :
  'names' attribute [4] must be the same length as the vector [2]

and can be traced to the use of deparse(substitute(x)) and 
deparse(substitute(y)) in the R code.


What seems to be happening is that the deparse(substitute(x)) call
gives a character vector of length > 1 as soon as x is "not so small".

To demonstrate this, I am using rpy2 (as the problem was found when using it
http://sourceforge.net/mailarchive/forum.php?thread_name=4B043CA1.9050500%40salilab.org&forum_name=rpy-list
), but it will likely be present in other bridges to R as well.


#using R-2.10 and rpy2-2.1.dev
>>> import rpy2.robjects as robjects

>>> f = robjects.r('''function(x) return(deparse(substitute(x)))''')

>>> tuple(f(robjects.FloatVector(range(17
('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)',)
# length 1
>>> tuple(f(robjects.FloatVector(range(18
('c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17', ')')
# length 2 !!


Does it seem to others like an issue present in other bridges as well ?



L.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel