Non-parametric Smoothers
You can see non-parametric smoothers in action for fitting a curve through a scatterplot in Chapter 5 (p. 151). Here we are concerned with using non-parametric smoothers in statistical modelling where the object is to assess the relative merits of a range of different models in explaining variation in the response variable. One of the simplest model-fitting functions is loess (which replaces its predecessor called lowess).
The following example shows population change, Delta = log(N(t + 1)11(t)) as a function of population density (N(t)) in an investigation of density dependence in a sheep population. This is what the data look like:
soay<-read.table("c:\\temp\\soaysheep.txt",header=T) attach(soay) names(soay) [1] "Year" "Population" "Delta" plot(Population,Delta)
Broadly speaking, population change is positive at low densities (Delta > 0) and negative at high densities (Delta < 0) but there is a great deal of scatter, and it is not at all obvious what shape of smooth function would best describe the data. Here is the default loess:
model<-loess(Delta~Population) summary(model) Call: loess(formula = Delta~Population) Number of Observations: 44 Equivalent Number of Parameters: 4.66 Residual Standard Error: 0.2616 Trace of smoother matrix: 5.11 Control settings: normalize:TRUE span : 0.75 degree : 2 family : gaussian surface : interpolate cell = 0.2
Now draw the smoothed line using predict to extract the predicted values from model:
xv<-seq(600,2000,1) ...
Get The R Book now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.