File: tree.demo.R

package info (click to toggle)
r-cran-teachingdemos 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 2
file content (33 lines) | stat: -rw-r--r-- 842 bytes parent folder | download | duplicates (7)
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
"tree.demo" <-
function(x,y){
	old.opt <- options(locatorBell = FALSE)
        on.exit( options(old.opt) )
	cuts <- range(x)
	
	repeat {
		
		cut2 <- numeric(0)
		repeat {
			plot(x,y,xlab=deparse(substitute(x)),
                             ylab=deparse(substitute(y)))
			abline( v=cuts, col='blue' )
			abline( v=cut2, col='red' )
			cuts3 <- sort( c(cuts,cut2) )
			cats <- cut( x, cuts3, include.lowest=T)
			means <- tapply(y, cats, mean )
			index <- tapply(y, cats )
			segments(cuts3[-length(cuts3)], means, cuts3[-1], means, col='green' )
			resid <- y-means[index]
			ss <- round(resid %*% resid)
			title( paste( "Residual sum of squares =", ss ) )
			tempx <- locator(1)$x
			if (length(tempx) < 1) break
			cut2 <- tempx
		}
		if(length(cut2) < 1) break
		cuts <- sort( c(cuts,cut2) )
	}
	
	
}