Hi: I am using R 1.7.0 on Windows. I am having trouble getting "outer" to work on one of my functions. Here is a simple example illustrating my problem:> b1 <- c(1.2,2.3) > b2 <- c(0.5,0.6) > x <- c(3e+01, 1e+02, 3e+02, 5e+02, 1e+03, 1e+04, 1e+05, 1e+06) > y <- c(2,4,2,5,2,3,1,1) > n <- c(5,8,3,6,2,3,1,1)> outer(b1,b2,FUN=bpllkd,x,y,n)[,1] [,2] [1,] 17.78031 17.78031 [2,] 17.78031 17.78031 These values should all be different. What is the problem here? The function "bpllkd" is given below: thanks for any help, Ravi.> bpllkdfunction(t1,t2,x,y,n){ p <- 1 - (1+x/10^t1)^(-t2) keep <- !((p==0 & y==0) | (p==1 & n==y)) llk <- sum(y[keep]*log(p[keep])+(n-y)[keep]* log(1-p[keep])) return(-llk) }
Have you tried: outer(b1,b2,FUN=bpllkd,x=x,y=y,n=n) I don't think "outer" quite knows what to do with "x, y, n)". However, "x=x1, y=y1, n=n1") clearly tells outer to pass the object x1 to the argument x of function bpllkd, etc. hope this helps. spencer graves p.s. I was just bitten by that snake last week. Ravi Varadhan wrote:> Hi: > > I am using R 1.7.0 on Windows. I am having trouble getting "outer" to > work on one of my functions. Here is a simple example illustrating my > problem: > > >>b1 <- c(1.2,2.3) >>b2 <- c(0.5,0.6) >>x <- c(3e+01, 1e+02, 3e+02, 5e+02, 1e+03, 1e+04, 1e+05, 1e+06) >>y <- c(2,4,2,5,2,3,1,1) >>n <- c(5,8,3,6,2,3,1,1) > > >>outer(b1,b2,FUN=bpllkd,x,y,n) > > [,1] [,2] > [1,] 17.78031 17.78031 > [2,] 17.78031 17.78031 > > These values should all be different. What is the problem here? > The function "bpllkd" is given below: > > thanks for any help, > Ravi. > > >>bpllkd > > function(t1,t2,x,y,n){ > p <- 1 - (1+x/10^t1)^(-t2) > keep <- !((p==0 & y==0) | (p==1 & n==y)) > llk <- sum(y[keep]*log(p[keep])+(n-y)[keep]* > log(1-p[keep])) > return(-llk) > } > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Wed, 09 Jul 2003 15:33:11 -0400, Ravi Varadhan <rvaradha at jhsph.edu> wrote :>Hi: > >I am using R 1.7.0 on Windows. I am having trouble getting "outer" to >work on one of my functions.Most likely the problem is that the function you give doesn't work on array arguments. Your function needs to take two arrays of the same shape as the first two arguments, and return an array of answers. outer() doesn't work by looping, it works by constructing big arrays of inputs and making just one function call. Duncan Murdoch
Possibly Parallel Threads
- How top print intermediate values from inside a function?
- Help on simple problem with optim
- Likelihood optimization numerically
- Warning: number of items to replace is not a multiple of replacement length
- Problem with numerical integration and optimization with BFGS