days = c(1,2,3,4,5,7)
oxy = c(8.3,10.3,19,16,15.6,19.8)
bod = data.frame(days,oxy)
# Compute starting values. The value '20' was chosen as an
approximate
# asymptote, upon examining the data
theta1 = 20
theta2 = -1*lsfit(x=days,y=log(1-oxy/theta1),interce
# Various regression techniques for the 'cars' data
# Y = braking distance, X = speed
# Data already in R; called 'cars'
x = cars$speed
y = cars$dist
par(mfrow=c(2,2)
of plots
# Sets the plotting function to give a 2 by 2 panel
# Two Least Squares fits
pl
#R example; acetylene data
# Data from Montgomery & Peck Example 8.1
# Response variabe (conv) is % conversion of n-heptane to
acetylene
# Explanatory variables temp (reactor temperature), mole (ratio
of H2 to n-heptane), cont (contact time)
# Enter the d
# Effect of influential observations
x
y
x
y
=
=
=
=
seq(0,10, length=20)
x + rnorm(length(x)
c(x,40)
c(y, 1)
par(mfrow=c(1,2)
# Data and two regressions; one outlier:
dev.set(which=2)
plot(x,y, title(sub="one outlier")
fit1 = lm(y~x)
yhat1 = predict.lm(f
# GAM fitting to rock data
# Downlad the package gam from the appropriate CRAN mirror site,
then:
library(gam)
attach(rock)
# First do a linear fit:
rock.lm = lm(log(perm) ~ area + peri + shape)
rock.gam1 = gam(log(perm) ~ s(area) + s(peri) + s(shape)
# O
Motorcycle data
# Load the MASS package; data are in mcycle
library(MASS)
attach(mcycle)
plot(mcycle)
#
par(mfrow=c(2,1)
# Fit orthogonal polynomials
plot(mcycle)
for(k in 2:6) lines(mcycle$times,
predict(lm(accel~poly(times,k), data=mcycle), lty=k-1)
tit
# Motorcycle data
# Load the MASS package; data are in mcycle
library(MASS)
attach(mcycle)
# Running means and medians:
par(mfrow=c(2,1)
n = length(times)
runningmean = function(k) cfw_
runm = rep(0, n)
for(i in (k+1):(n-k) runm[i] = mean(accel[(i-k):(i+k