File: hinged.R

package info (click to toggle)
r-cran-quantreg 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,224 kB
  • sloc: fortran: 6,741; ansic: 288; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,036 bytes parent folder | download
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
38
39
40
41
42
43
44
45
# A Demo of simple bivariate rqss fitting of a hinge function

require(quantreg)
if(requireNamespace("interp")){
## Make sure the demo does not ``die'' when rgl is not available:
do.rgl <- interactive() && require(rgl)

#generate the data

n <- 1000
x <- runif(n)
y <- runif(n)
z <- -abs(x-y)

### Make an initial quite rough fit of the data
fit <- rqss(z ~ qss(cbind(x,y),lambda = .005))
print(summary(fit)$penalty)

if(do.rgl) {
    plot(fit, render = "rgl")
    cat("Now orient the plot as desired:",
	"Resize window,",
	"mouse button 1 to change viewpoint,",
	"mouse button 2 to zoom,",
	"and hit return when ready",sep="\n")
    scan()
    rgl.bg(color="8")
    cat("To try another value of lambda:",
	"Type a positive number",
	"To quit hit return", sep="\n")
    repeat{
	cat("lambda:  ")
	lam <- scan(what = double(1))
	if(length(lam)>0){
		fit <- rqss(z ~ qss(cbind(x,y),lambda = lam))
		rgl.clear()
		plot(fit, render = "rgl")
	} else 
		break
	}
} else {
    if(!interactive()) pdf(file = "hinge-demo.pdf")
    plot(fit)
}
}