[R] optimize and mcparallel problem
Matthew Wolak
matthew.wolak at email.ucr.edu
Wed Oct 3 18:21:31 CEST 2012
Dear list,
I am running into 2 problems when using the optimize function from the
stats package (note: I've also tried unsuccessfully to use optim,
nlm, & nlminb). The second problem is caused by my solution to the
first, so I am asking if anyone has a better solution to the first
question, or if there exists a solution to the second problem. I
should also mention that what I am
working on is a function for a package - so I need the code
to be applicable to all platforms (I understand that 'multicore'
doesn't really work on Windows, but for the second problem I mean "all
platforms, except windows")
The first problem:
I have a function that runs a linear mixed model with a constrained
variance for one of the random effects, computes a loglikelihood ratio
test statistic (LRT), and returns the absolute value of the difference
between the LRT and some pre-defined value (e.g., 2). I have made a
dummy function, called "foo" below that has the same inputs and
outputs without the complicated inner workings of my actual function.
My first problem, is that I don't just want to know the end value
("x") that minimizes the output to "foo" (i.e., "diff"), but every "x"
and the corresponding "diff" used by the optimize function. My
solution to this is to create an object ("vals") outside of "foo" and
write to this object.
foo <- function(x){
vals <<- c(vals, x)
diff <- abs(x - 2)
diff
}
This works well so far:
vals <- NULL
out1 <- optimize(foo, interval = seq(0, 4, 0.2))
vals
However, the second problem arises if I want to use the "parallel"
function in the "multicore" package:
library(multicore)
vals <- NULL
out2_tmp <- mcparallel(optimize(foo, interval = seq(0, 4, 0.2)))
out2 <- collect(out2_tmp, wait = TRUE)
vals
Predictably, the child process does not return the "vals" object when
I use the "collect" function.
To summarize, my first question is whether or not there is a better
way to return all of the values over which "optimize" evaluates my
function. The second question is if I do use my solution to the first
question, how can I get the "vals" object returned from the child
process?
Thanks anyone very much for any and all help!!
Sincerely,
Matthew
--
Matthew Wolak
PhD Candidate
Evolution, Ecology, and Organismal Biology Graduate Program
University of California Riverside
http://student.ucr.edu/~mwola001/
More information about the R-help
mailing list