Re: [Rd] constrained optimization

2010-07-07 Thread Paul Bailey

Have you considered using the optim function with L-BFGS-B for bounded
optimization. Obviously, you will have to do changes of variables so that
everything is in terms of a rectangle (which is the type of bounding that it
accepts).

I believe it is based on "A LIMITED MEMORY ALGORITHM FOR BOUND CONSTRAINED
OPTIMIZATION" By Richard H Byrd,  Peihuang Lu, Jorge Nocedal, and Ciyou Zhu.
Technical Report NAM-08, May 1994.

They also have a paper on the exact implementation. I also believe that R
uses the code from Nocedal et al.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/constrained-optimization-tp2280809p2281282.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] Accessing options of L-BFGS-B

2010-07-07 Thread Paul Bailey

Hi,

I use L-BFGS-B heavily and I want access to some of the options in the code
that R calls but does not provide access to. There are some knobs on the
convergence criteria that are not accessible via optim, specifically, I want
to require a maximum gradient (obviously, it is actually some norm of the
gradient vector so that the requirement is 1 dimensional). I want to do this
because rerunning from various starting points gives me poor convergence and
my own (pure-R) L-BFGS has a poor line search algorithm that sometimes
fails.

Is there a simple way to do this?

As an alternative, the BFGS type papers tend to be long on the BFGS
explanations and short on the line search explanations. If someone pointed
me to a good line search paper that uses the Wolfe conditions...

Thanks,
Paul Bailey
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Accessing-options-of-L-BFGS-B-tp2281324p2281324.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] pass-by-reference

2010-07-07 Thread Paul Bailey

I'm working with a large object that I want to modify slightly in a function.
Pass-by-reference would make a lot of sense, but I don't know how to do it.

I've searched this archive and thought that I can do something like

f <- function(x) {
  v1 <- list(a=x,b=3)
  g(x)
  v1
}
g <- function(x) {
  frame <- parent.frame()
  assign("v1",list(a=x,b=x),frame)
}
f(4)
returns list(a=4,b=4)

but what if I wanted to make v1[[1]] = v1[[1]] + v1[[2]] without creating a
copy of v1? 

f2 <- function(x) {
  v1 <- list(a=x,b=3)
  g2(x)
  v1
}
g2 <- function(x) {
  frame <- parent.frame()
  v1 <- get("v1",envir=frame)
  v1[[1]] <- v1[[1]] + v1[[2]]
}
f2(4)

but this fails. (it returns list(a=4,b=3) because v1 was copied into g2, not
passed by reference) Is there a way to do this?
-- 
View this message in context: 
http://r.789695.n4.nabble.com/pass-by-reference-tp2281802p2281802.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] aggregate(as.formula("some formula"), data, function) error when called from in a function

2011-01-26 Thread Paul Bailey
I'm having a problem with aggregate.formula when I call it in a function and 
the function is converted from a string in the funtion

I think my problem may also only occur when the left hand side of the formula 
is cbind(...)

Here is example code that generates a dataset and then the error. 

The first function "agg2" fails

> agg2(FALSE)
do agg 2
Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable

but, if I run it have it return what it is going to pass to aggregate and pass 
it myself, it works. I can use this for a workaround (agg3) where one function 
does this itself.

I'm confused by the behavior. Is there some way to not have to use a separate 
function to make the call ?


==
# start R code
# idea: in a function, count the number of instances
# of some factor (y) associated with another
# factor (x). aggregate.formula appears to be
# able to do this... but I have a problem if all of the following:
# (1) It is called in a function
# (2) the formula is created using as.formula(character)
# calling aggregate with the same formula (created with as.formula)
# outside the function works fine.
agg2 <- function(test=FALSE) {
  # create a factor y
  dat <- data.frame(y=sample(LETTERS[1:3],100,replace=TRUE))
  # create a factor x
  dat$x <- sample(letters[1:4],100,replace=TRUE)
  # make a column of 1s and zeros
  # 1 when that row has that level of y
  # 0 otherwise
  lvls <- levels(dat$y)
  dat$ya <- 1*(dat[,1] == lvls[1])
  dat$yb <- 1*(dat[,1] == lvls[2])
  dat$yc <- 1*(dat[,1] == lvls[3])
  # this works fine if you give the exact function
  agg1 <- aggregate(cbind(ya,yb,yc)~x,data=dat,sum)
  # and fine if you accept
  fo <- as.formula("cbind(ya,yb,yc)~x")
  if(test) {
return(list(fo=fo,data=dat))
  }
  cat("do agg 2\n")
  agg2 <- aggregate(fo,data=dat,sum)
  list(agg1,agg2)
}
agg2(FALSE)
ag <- agg2(TRUE)
ag$fo
aggregate(ag$fo,ag$data,sum)


agg3 <- function() {
  ag <- agg2(TRUE)
  ag$fo
  aggregate(ag$fo,ag$data,sum)
}
agg3()

# end R code
==
Paul Bailey
University of Maryland
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] help with S4 objects: trying to use a "link-glm" as a class in an object definition

2011-01-27 Thread Paul Bailey
Hi,

I'm trying to make a new S4 object with a slot for a "link-glm" object. R 
doesn't like me have a slot of class "link-glm"

> class(make.link("probit"))
[1] "link-glm"
> setClass("a",representation(item="link-glm"))
[1] "a"
Warning message:
undefined slot classes in definition of "a": item(class "link-glm") 
> fa <- function() {
+   new("a",item=make.link("probit"))
+ }> 
> fa()
Error in validObject(.Object) : 
  invalid class "a" object: undefined class for slot "item" ("link-glm")

# and a link-glm looks like a list to me, so I thought I would tell R it is a 
list and see what happens

> setClass("b",representation(item="list"))
[1] "b"
> fb <- function() {
+   new("b",item=make.link("probit"))
+ }
> fb()
Error in validObject(.Object) : 
  invalid class "b" object: invalid object for slot "item" in class "b": got 
class "link-glm", should be or extend class "list"

Any advice?

Regards,
Paul Bailey
Ph.D. candidate
Department of Economics
University of Maryland

## raw code #
setClass("a",representation(item="link-glm"))
fa <- function() {
  new("a",item=make.link("probit"))
}
fa()
setClass("b",representation(item="list"))
fb <- function() {
  new("b",item=make.link("probit"))
}
fb()
###
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Error in Rd[[which]] : subscript out of bounds

2011-11-27 Thread Paul Bailey
I'm getting the following form R CMD CHECK mypackage
---
* checking Rd files ... WARNING
Error in Rd[[which]] : subscript out of bounds

problem found in ‘myfunction.Rd’
-
This is... not the most helpful error.

I'd be happy to make a minimal .Rd example file if someone can point me to what 
a minimal .Rd file has in it.

The file is already pretty minimal, so it's possible I've already gone to small 
and that is the reason for the error.

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


Re: [Rd] Error in Rd[[which]] : subscript out of bounds

2011-11-27 Thread Paul Bailey
> sessionInfo()?  If not R v2.14.0, try with that version first.  Then
> have a look checkRd() of the 'tools' package, cf. help("checkRd",
> package="tools").  That function allows you to check your Rd file from
> within R so that you get more information/so that you can use
> traceback() etc.
> 

Sorry, should have said this is in 2.14.0.

Using traceback() as you suggested solved this problem for me, thanks. this 
made it pretty obvious:

2: checkUnique("\\description")

looks like I typed description when I wanted details.

Thanks!

Best,
Paul Bailey

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