# [R] creating a grid of function values

Jim Robison-Cox jimrc at gauss.math.montana.edu
Thu Feb 10 23:57:13 CET 2000

```On 10 Feb 2000, Douglas Bates wrote:

> I want to create a grid of function values for use in `contour' or
> `persp'.  The function is the log-likelihood for the gamma.  The
> sample is stored as vector of length 20 called `Survival'.
>
> A single evaluation of the log-likelihood at, say, scale = 9 and shape
> = 10 would be obtained by
>  sum(dgamma(Survival, scale = 9, shape = 10, log = TRUE))
> (This may work only 0.99.0, I'm not sure.)
>
> I would like to evaluate such a function on a grid of scale and shape
> values.  I don't think I can use `outer' because of the way the
> evaluation of the dgamma function would vectorize.  Although I could
> write the calculation for the grid in `for' loops, I have a nagging
> suspicion that there is a cleaner way that I have forgotten.
>
> Can anyone remind me of a slick way of generating the grid?
>

Here's one way which avoids looping by using apply.

gridS <- expand.grid(scales, shapes)
survLilel <- function(ss) sum(dgamma(Survival,ss[1],ss[2]))
Likel <- apply(gridS,1,survLilel)

Regards,
Jim

Jim Robison-Cox               ____________
Department of Math Sciences  |            |       phone: (406)994-5340
2-214 Wilson Hall             \   BZN, MT |       FAX:   (406)994-1789
Montana State University       |  *_______|
Bozeman, MT 59717-2400          \_|      e-mail: jimrc at math.montana.edu

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```