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.

<strong>Using</strong> R <strong>for</strong> introductory statistics 290or drop terms from a model and have the new model fit. This process is called updatingand is done with the update () function. The usage isupdate(model.object, <strong>for</strong>mula=. ~ .+new.terms)The model. object is the output of some modeling command, such as lm(). The <strong>for</strong>mula=argument uses a . to represent the previous value. In the template above, the . to the left ofthe ~ indicates that the previous left side of the model <strong>for</strong>mula should be reused. Theright-hand-side . refers to the previous right-hand side. In the template, the+new. termsmeans to add terms. Use- old. terms to drop terms.■ Example 10.6: Discovery of the parabolic trajectory The data set galileo(<strong>Using</strong>R) contains two variables measured by Galileo (described previously). One is theinitial height and one the horizontal distance traveled.A plot of the data illustrates why Galileo may have thought to prove that the correctshape is described by a parabola. Clearly a straight line does not fit the data well.However, with modern computers, we can investigate whether a cubic term is warranted<strong>for</strong> this data.To do so we fit three polynomial models. The update () function is used to add termsto the previous model to give the next model. To avoid a different interpretation of ~, thepowers are insulated with I ().> init.h = c(600,700,800,950,1100,1300,1500)> h.d = c(253, 337, 395, 451, 495, 534, 573)> res.1m=lm(h.d ~ init.h)> res.lm2=update(res.1m, . ~ .+I(init .h^2))> res.lm3=update(res.lm2, . ~ .+I(init .ITS))To plot these, we will use curve (), but first we define a simple function to help us plotpolynomials when we know their coefficients. The result is in Figure 10.8. The linearmodel is a poor fit, but both the quadratic and cubic fits seem good.Figure 10.8 Three polynomialmodels fit to the Galileo data> polynomial=function(x,coefs) {

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

Saved successfully!

Ooh no, something went wrong!