-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdemoLeastSquares.r
37 lines (36 loc) · 1.16 KB
/
demoLeastSquares.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# title: Interactive Demo of Least Squares and Other Fitting
# major: regression modeling
# minor: curve fitting
require(Hmisc)
require(MASS)
while (TRUE) {
curves <- vector('list',4)
names(curves) <- c('Least Squares','Least Sum of Absolute Deviations',
'Least Median Squared Deviation','Loess')
plot(0:1,0:1,xlab='x', ylab='y', type='n')
cat('\nClick any points you desire. Terminate points by right mouse click (Esc under RStudio).\n')
z <- locator(type='p')
f <- lsfit(z$x, z$y)
abline(f, lwd=3)
segments(z$x, z$y, z$x, z$y - f$residuals, lwd=1, col=gray(.8))
x <- c(0,1)
curves[[1]] <- list(x, f$coef[1]+(0:1)*f$coef[2])
f <- rlm(cbind(1,z$x), z$y)
abline(coef(f), col=2)
curves[[2]] <- list(x, f$coef[1] + (0:1)*f$coef[2])
n <- length(z$x)
if(n > 4) {
f <- lmsreg(z$x, z$y)
abline(f, col=3)
curves[[3]] <- list(x, f$coef[1] + (0:1)*f$coef[2])
}
f <- lowess(z)
lines(f, col=4)
curves[[4]] <- f
if(n <= 4) curves[[3]] <- NULL
labcurve(curves, col=1:4, tilt=T, keys='lines', cex=.75)
title(sub='Left click for another example. Click outside plot to stop',
adj=0)
z <- locator(1)
if(any(z$x < 0 | z$x > 1 | z$y < 0 | z$y > 1)) break
}