Hi: This is pure speculation since you didn't provide a minimal data set to test this with, but is it possible that theta is meant to be a parameter *vector*? If so, then you should have
eta <- theta[1] K <- theta[2] The code theta[, 1] means that you want to extract the first column of the _matrix_ theta. More below. On Tue, Aug 3, 2010 at 2:44 PM, éª ³Ì <jnju...@yahoo.com.cn> wrote: > Hi, > How to solve this problem. The following is the code. > > betabinexch0=function(theta,data) > + { > + eta=theta[,1] > + K=theta[,2] > + y=data[,1]; n=data[,2] > + N=length(y) > + val=0*K; > + for (i in 1:N) > + val=val+lbeta(K*eta+y[i],K*(1-eta)+n[i]-y[i]) > This indicates that n[i] and y[i] are scalars, but as you have it defined, K*eta is a vector and so is K * (1 - eta). That's capable of causing dimension problems. > + val=val-N*lbeta(K*eta,K*(1-eta)) > N * lbeta(K * eta, K * (1 - eta)) will yield a vector given the way you defined K and eta. + val=val-2*log(1+K)-log(eta)-log(1-eta) # Again, this part yields a > vector. > + return(val) > + } > > data(cancermortality) > > mycontour(betabinexch0,c(.0001,.003,1,20000),cancermortality) > If eta and K are meant to be scalars, then the loop is unnecessary as you can do everything using vectorized arithmetic, which in turn means that the code should run faster. Here's a modification, assuming theta is a vector rather than a matrix: betabinexch0=function(theta,data) { eta=theta[1] K=theta[2] y=data[,1]; n=data[,2] N=length(y) # val=0*K; # for (i in 1:N) val= lbeta(rep(K*eta, N) + y, rep(K*(1-eta), N) + n - y) - rep(N * lbeta(K*eta,K*(1-eta)), N) - rep(2*log(1+K)-log(eta)-log(1-eta), N) ifelse(!any(is.nan(val)), sum(val), NaN) } # The rep(whatever, N) trick is a cheap way of expanding a scalar into a vector. (There are others...) # Create some fake data and call the function: d <- data.frame(y = rpois(10, 5), n = rpois(10, 10)) theta <- c(0.5, 10) betabinexch0(theta, d) # example [1] -127.1016 It is possible for the function to yield Inf or NaN as its result, so you have to be careful about the inputs. How this plays into your mycontour call I have no idea. I would expect that the betabinexch0 argument would have at least an () at the end, if not two arguments within parentheses, but again, without seeing what mycontour looks like, it's hard to say. HTH, Dennis Error in theta[, 1] : incorrect number of dimensions > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.