[R] nls() and numerical integration (e.g. integrate()) working together?

Michael Lindner lindnerm at student.ethz.ch
Thu Aug 23 08:07:21 CEST 2007

Dear List-Members,

since 3 weeks I have been heavily working on reproducing the results of an
economic paper. The method there uses the numerical solution of an integral
within nonlinear least squares. Within the integrand there is also some
parameter to estimate. Is that in the end possible to implement in R
[Originally it was done in GAUSS]? I'm nearly into giving up.

I constucted an example to showing the problems I face.

I have three questions - related to three errors shown below: 
1) How to make clear that in the integrand z is the integration variable and
b1 is a parameter while x1 is a data variable
2) and 3) How to set up a correct estimation of the integral?

y <- c(2,15,24,21,5,6,)
x1 <- c(2.21,5,3,5,2,1)
x2 <- c(4.51,6,2,11,0.4,3)
f <- function(z) {z + b1*x1}
vf <- Vectorize(f)
g <- function(z) {z + x1}
vg <- Vectorize(f)

Error 1: 
> nls(y ~ integrate(vf,0,1)+b2*x2,start=list(b1=0.5,b2=2))
Error in function (z)  : object "b1" not found

Error 2:
> nls(y ~ integrate(vg,0,1)+b2*x2,start=list(b1=0.5,b2=2))
Error in integrate(vg, 0, 1) : REAL() can only be applied to a 'numeric',
not a 'list'

Error 3:
> nls(y ~ integrate(g,0,1)+b2*x2,start=list(b1=0.5,b2=2))
Error in integrate(g, 0, 1) + b2 * x2 : non-numeric argument to binary
In addition: Warning messages:
1: longer object length
        is not a multiple of shorter object length in: z + x1

With a lot of thanks in advance,


More information about the R-help mailing list