selfStart               package:stats               R Documentation

_C_o_n_s_t_r_u_c_t _S_e_l_f-_s_t_a_r_t_i_n_g _N_o_n_l_i_n_e_a_r _M_o_d_e_l_s

_D_e_s_c_r_i_p_t_i_o_n:

     Construct self-starting nonlinear models.

_U_s_a_g_e:

     selfStart(model, initial, parameters, template)

_A_r_g_u_m_e_n_t_s:

   model: a function object defining a nonlinear model or a nonlinear
          formula object of the form '~expression'.

 initial: a function object, taking three arguments: 'mCall', 'data',
          and 'LHS', representing, respectively, a matched call to the
          function 'model', a data frame in which to interpret the
          variables in 'mCall', and the expression from the left-hand
          side of the model formula in the call to 'nls'. This function
          should return initial values for the parameters in 'model'.

parameters: a character vector specifying the terms on the right hand
          side of 'model' for which initial estimates should be
          calculated.  Passed as the 'namevec' argument to the 'deriv'
          function.

template: an optional prototype for the calling sequence of the
          returned object, passed as the 'function.arg' argument to the
          'deriv' function.  By default, a template is generated with
          the covariates in 'model' coming first and the parameters in
          'model' coming last in the calling sequence.

_D_e_t_a_i_l_s:

     This function is generic; methods functions can be written to
     handle specific classes of objects.

_V_a_l_u_e:

     a function object of class '"selfStart"', for the 'formula' method
     obtained by applying 'deriv' to the right hand side of the 'model'
     formula.  An 'initial' attribute (defined by the 'initial'
     argument) is added to the function to calculate starting estimates
     for the parameters in the model automatically.

_A_u_t_h_o_r(_s):

     Jose Pinheiro and Douglas Bates

_S_e_e _A_l_s_o:

     'nls'

_E_x_a_m_p_l_e_s:

     ## self-starting logistic model

     SSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)),
       function(mCall, data, LHS)
       {
         xy <- sortedXyData(mCall[["x"]], LHS, data)
         if(nrow(xy) < 4) {
           stop("Too few distinct x values to fit a logistic")
         }
         z <- xy[["y"]]
         if (min(z) <= 0) { z <- z + 0.05 * max(z) } # avoid zeroes
         z <- z/(1.05 * max(z))              # scale to within unit height
         xy[["z"]] <- log(z/(1 - z))         # logit transformation
         aux <- coef(lm(x ~ z, xy))
         parameters(xy) <- list(xmid = aux[1], scal = aux[2])
         pars <- as.vector(coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)), 
                                  data = xy, algorithm = "plinear")))
         value <- c(pars[3], pars[1], pars[2])
         names(value) <- mCall[c("Asym", "xmid", "scal")]
         value
       }, c("Asym", "xmid", "scal"))

     # 'first.order.log.model' is a function object defining a first order
     # compartment model 
     # 'first.order.log.initial' is a function object which calculates initial
     # values for the parameters in 'first.order.log.model'

     # self-starting first order compartment model
     ## Not run: 
     SSfol <- selfStart(first.order.log.model, first.order.log.initial)
     ## End(Not run)

