[R] Error in nls model.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Oct 24 16:17:06 CEST 2007


On Wed, 24 Oct 2007, Rafael Barros de Rezende wrote:

>
>   Error in model.frame
>
>   When I run the following nls model an error message appears and I dont
>   know how to solve that. Could you help me??
>
>   mat = c(1,2,3,4,5,6,7,8,9,12,16,24,36,48,60)
>
>   for (i in 1:length(j30)) {
>   bliss = nls(c(j[i,1:length(mat)]) ~ b0 + b1*((1-exp(-k1*mat))/(k1*mat)) +
>   b2*(((1-exp(-k2*mat))/(k2*mat))-exp(-k2*mat)),
>   start = list(k1=0.1993, k2=0.1993, b0= 22.0046, b1=-2.7661, b2=-5.8912),
>   trace = TRUE)
>   }
>
>   Error in  model.frame(formula,  rownames, variables, varnames, extras,
>   extranames,  :
>           variable lengths differ (found in 'mat')
>
>   j is a (1883 x 15) matrix so I have 1883 nls regressions of j lines on mat
>   as we can see in model bliss.
>
>   The traceback() shows the following:
>
>   > traceback()
>   6: model.frame.default(formula = ~j + mat)
>   5: model.frame(formula = ~j + mat)
>   4: eval(expr, envir, enclos)
>   3: eval(expr, p)
>   2: eval.parent(mf)
>   1: nls(c(j[i, 1:length(mat)]) ~ b0 + b1 * ((1 - exp(-k1 * mat))/(k1 *
>          mat)) + b2 * (((1 - exp(-k2 * mat))/(k2 * mat)) - exp(-k2 *
>          mat)), start = list(k1 = 0.1993, k2 = 0.1993, b0 = 22.0046,
>          b1 = -2.7661, b2 = -5.8912), trace = TRUE)
>
>   It seems that the error is in 6: model.frame.default(formula = ~j + mat) or
>   5: model.frame(formula = ~j + mat), cause the formula should be (formula = j
>   ~ mat).

No, nls uses a one-sided formula to get the model frame.  The error is 
that 'mat' and 'j' are not of the same length.  You can't use 
c(j[i,1:length(mat)]) in an nls formula: assign it to a variable and use 
that instead.

>   Am I right about that?? Again, how could I fix this problem?? I need a help
>   here...
>
>   Thanks.
>   Rafael Barros de Rezende
>   Economics MPhill Candidate - Curriculum Lattes:
>   [1]http://lattes.cnpq.br/9826095609825249
>   Cedeplar - Development and Regional Planning Center
>   Face, UFMG ([2]http://www.cedeplar.ufmg.br) - Brazil

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list