10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Two extensions of the linear model 33512.2 Nonlinear modelsThe linear model is called “linear” because of the way the coefficients β i enter into the<strong>for</strong>mula <strong>for</strong> the mean. These coefficients simply multiply some term. A nonlinear modelallows <strong>for</strong> more complicated relationships. For example, an exponential model mighthave the response modeled asHere,is not linear in the parameters due to the β 1 . It does not appear as anadditive term like β 1 x i .Variations on the exponential model areThe exponential model, with β 1 >0, may be used when the response variable decays as thepredictor increases. The second model has a growth-then-decay phase, and the third adecay, not to a but to some threshold amount β 0·β 2 .In general, a single-covariate, nonlinear model can be written as follows:Y i =f(x i |β 0 ,β 1 ,…,β r )+ε i .We have r+1 parameters and only one predictor with an additive error. More generalmodels could have more predictors and other types of errors, such as multiplicative.The possibilities seem endless but in fact are constrained by the problem we aremodeling. When using nonlinear models we typically have some idea of which types ofmodels are appropriate <strong>for</strong> the data and then fit just those. If the model has i.i.d. errorsthat are normally distributed, then using the method of least squares allows us to findparameter estimates and use AIC to compare models.12.2.1 Fitting nonlinear models with nls()Nonlinear models can be fit in R using nls(). The nls() function computes nonlinear leastsquares. Its usage is similar to, but different from lm(). A basic template isres=nls(<strong>for</strong>mula, data=…, start=c(…), trace=FALSE)The model <strong>for</strong>mula is different <strong>for</strong> nonlinear models. The <strong>for</strong>mula again looks likeresponse ~ mean, but the mean is specified using ordinary math notations. For example,the exponential model <strong>for</strong> the mean could be written y ~ N * exp (−r* (t−t0)), where N, r,and t0 are parameters. It is often convenient to use a function to return the mean, such asy ~ f (x, beta0, beta1,…). That is, a function that specifies the parameter values by name.The method of nonlinear least squares uses an algorithm that usually needs to startwith parameter values that are close to the actual ones. The argument start=c (…) iswhere we put the initial guesses <strong>for</strong> the parameters. This can be a vector or list usingnamed values, such as start=c (beta0=1 ,betal=2). Finally, the optional argumenttrace=TRUE can be used if we want to see what is happening during the execution of thealgorithm. This can be useful in<strong>for</strong>mation if the algorithm does not converge. By defaultit is FALSE.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!