Dear all,
we are writing a wrapper for the nls function in library stats. We are
having a problem with one of the arguments (weightsArgument) which seems not
to reach nls even if we explicitly assign it in the function call. We are
attaching the simplest code reproducing the error and the output calling the
wrapper and calling nls directly. We are using R 2.9.0
library(stats)
wrappernls <-
function(modelArgument,dataArgument,startArgument,weightsArgument)
{
F.mod <-try(nls(formula=modelArgument,data=dataArgument,start=startArgument,
weights=weightsArgument,
control=nls.control(maxiter = 500, tol = 1e-05, minFactor
1/102400)),silent=TRUE)
return(F.mod)
}
F<-c(0.4091867,0.4060938,0.4032078,0.4089090,0.4138126,0.4183426,0.4073004,0.4145457,0.4137699,
0.4161127,0.4228770,0.4231176,0.4295189,0.4290417,0.4348761,0.4517475,0.4899147,0.5463731,
0.6273890,0.7458752,0.8960531,1.0280455,1.1753147,1.3122100,1.4370375,1.5610782,1.6666418,
1.7771591,1.8853868,1.9628042,2.0407750,2.1179830,2.1647203,2.2147800,2.2848194,2.3442066,
2.3706858,2.4121310,2.4508073,2.4767710)
skipCycles=3;
y00<-max(F);
a0<- min(F)-y00;
x00<-which.min(abs(F-(a0/2+y00)));
derivativeAtc0= (F[x00+1]-F[x00-1])/2
b0=-4*x00*derivativeAtc0/a0
w<-c(rep(0,skipCycles), rep(1,length(F)-skipCycles))
x=1:length(F);
modelArg=F ~ y0+D*x+a/(1+(x/x0)^b)
dataArg=data.frame(F,x)
startArg=list(y0 = y00, a=a0, x0=x00, b=b0, D=0)
weightsArg=w
F.modWrapper <- wrappernls(modelArg,dataArg,startArg,weightsArg)
F.mod <-
nls(formula=modelArg,data=dataArg,start=startArg,weights=weightsArg,
control=nls.control(maxiter = 500, tol = 1e-05, minFactor = 1/102400))
*Output in R:*
> F.mod
Nonlinear regression model
model: F ~ y0 + D * x + a/(1 + (x/x0)^b)
data: dataArg
y0 a x0 b D
2.891357 -2.434011 25.604522 5.880144 -0.007322
weighted residual sum-of-squares: 0.02664
Number of iterations to convergence: 8
Achieved convergence tolerance: 3.517e-06> F.modWrapper
[1] "Error in eval(expr, envir, enclos) : object 'weightsArgument'
not
found\n"
attr(,"class")
[1] "try-error"
Any help about this will be welcome.
Thanks
[[alternative HTML version deleted]]