[R] Optimise parameter of a generic function

Samuel Dennis sjdennis3 at gmail.com
Thu Sep 24 05:12:57 CEST 2009


I would like to know how to determine the best value of a particular
parameter in a generic function.

My function is:
nbin <- function(k,Dt) {
R <- Dt / (k + Dt)
q <- (k + Dt) / k
Pt <- c(0:7)
for (r in c(0:7)) {
Pt[r+1] <- (factorial(k+r-1)/(factorial(r)*factorial(k-1)))*((R^r)/(q^k)) }
Pt }

This function generates a vector of eight values, for example:
nbin(1.5,0.63)
returns
[1] 0.5909727875 0.2621921522 0.0969372394 0.0334501742
[5] 0.0111304277 0.0036213082 0.0011603487 0.0003677162

Leaving the second parameter as 0.63, I would like to find the value of the
first parameter that most closely replicates the following observed data:
data <- c(0.588,0.269,0.095,0.030,0.013,.002,.003,0)

Through trial and error I have found 1.5 to be fairly close, but I am not
sure how to mathematically find the optimum value.

I have attempted:
nbinopt <- function(k) {
pred <- nbin(k,0.63)
data <- c(0.588,0.269,0.095,0.030,0.013,.002,.003,0)
max(pred - data)  }
optimise(f = nbin063, interval = c(0,100), maximum=FALSE, tol=0.01)

However this returns a value of 0.09, which actually generates a vector that
is considerably different to the observed data. There must be a better way
of doing it.

I would be grateful for any help you can offer.
-- 
View this message in context: http://www.nabble.com/Optimise-parameter-of-a-generic-function-tp25531369p25531369.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list