# [R] Cubic spline in nonlinear optimisation

Simon Hoyle SimonH at spc.int
Thu Nov 17 05:23:50 CET 2011

```How can I specify 'starting values' for a cubic spline ns()? I need
starting values for all pars when fitting using the nonlinear least
squares optimiser nls(). The y variable (length) is a function of 2
variables: alpha and age. The functional form for the age-length
relationship is specified as a standard nonlinear growth function, but
alpha modifies the age-length relationship in an unknown manner, so I
want the flexibility of a cubic spline.

nls() is probably the wrong optimiser to handle this, but can someone
suggest a suitable optimiser, and how to specify starting values for the
spline?

For example (simplifying the real example):

x <- 1:100
alpha <- runif(100)
y <- (1 + 2*alpha + 2*alpha^2) * x + rnorm(100)/5

nls(y ~ (par0 + par1* alpha + par2* alpha ^2)* x,
start=c(par0=0,par1=0,par2=0))  # polynomial version - this works
nls(y ~ ns(alpha,df=3)*x,start=list(theta=ns(1:100,df=3)))
# cubic spline version - this doesn't work

Thanks a lot,
Simon

```