[R] Unexpected nls behaviour: Solved

Keith Jewell k.jewell at campden.co.uk
Mon Aug 4 10:58:14 CEST 2008


Hi Everyone,

I'd omitted the non-optional 'parameters' argument to selfStart. Making this 
change to SSbatch gives the same (successful) result from the two calls to 
nls.

SSbatch<-selfStart(
model=function(Batch, Coeffs)
{
Coeffs[Batch]
}
,initial=function(mCall, data, LHS)
{
 # Estimate coefficients as mean of each batch
        xy <- sortedXyData(mCall[["Batch"]], LHS, data)
 Batch <- data[[as.character(mCall[["Batch"]])]]
 # check Batch is successive integers starting at 1
 if ((min(xy$x) !=1) | (any(diff(xy$x)!=1))) stop(
   "Batch is not a successive integers sequence")
 Lval <- list(xy$y)
 names(Lval) <- mCall["Coeffs"]
 Lval
}
, parameters = c("Coeffs")
)

Sorry for wasting anyones time.

Keith Jewell
-------------------------------------------------
"Keith Jewell" <k.jewell at campden.co.uk> wrote in message news:...
> Hi everyone,
>
> I thought that for a selfStart function, these two should be exactly 
> equivalent
>> nls(Aform, DF)
>> nls(Aform, DF, start=getInitial(Aform, DF))
> but in this example that is not the case in R (although it is in S-plus 
> V6.2)
> ------------------------------
> SSbatch<-selfStart(
> model=function(Batch, Coeffs)
> {
> Coeffs[Batch]
>
> }
> ,initial=function(mCall, data, LHS)
> {
> # Estimate coefficients as mean of each batch
>        xy <- sortedXyData(mCall[["Batch"]], LHS, data)
> Batch <- data[[as.character(mCall[["Batch"]])]]
> # check Batch is successive integers starting at 1
> if ((min(xy$x) !=1) | (any(diff(xy$x)!=1))) stop(
>   "Batch is not a successive integers sequence")
> Lval <- list(xy$y)
> names(Lval) <- mCall["Coeffs"]
> Lval
> }
> )
> DF <- data.frame(A=c(0.9, 1.1, 1.9, 2.0, 2.1, 2.9, 3.0), 
> Batch=c(1,1,2,2,2,3,3))
> Aform <- formula(A~SSbatch(Batch,cA))
> nls(Aform, DF, start=getInitial(Aform, DF))
> nls(Aform, DF)
> ------------------------------------
> Don't ask why I'd want such a silly selfStart, that's a long story.
> I guess wherever I would have used nls(Aform, DF)
> I could use nls(Aform, DF, start=getInitial(Aform, DF))
> but that seems clumsy.
>
> Can anyone point out my mistake? Or is this a limitation of nls in R (I 
> hesitate to use the b*g word).
>
> Thanks in advance,
>
> Keith Jewell
> ----------------------------------
>
> I don't think it's relevant but, for completeness:
>
>> sessionInfo()
>
> version 2.7.0 (2008-04-22)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United 
> Kingdom.1252;LC_MONETARY=English_United 
> Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
>
> attached base packages:
> [1] stats     graphics  grDevices datasets  tcltk     utils     methods 
> base
>
> other attached packages:
> [1] xlsReadWrite_1.3.2 svSocket_0.9-5     svIO_0.9-5         R2HTML_1.58 
> svMisc_0.9-5       svIDE_0.9-5
>
> loaded via a namespace (and not attached):
> [1] tools_2.7.0 VGAM_0.7-7
> -------------------------------- 
>
>



More information about the R-help mailing list