hardCompareLP: hardCompareLP

Description Usage Arguments Value Author(s) Examples

Description

Fit a linear hard margin comparison model to linearly separable data. The linear program (LP) is max_μ\in R, w\in R^p mu subject to these constraints:

Usage

1

Arguments

Pairs

see check.pairs.

Value

Comparison model fit. You can do fit$rank(X) to get m numeric ranks for the rows of the m x p numeric matrix X. For two feature vectors xi and xip, we predict no significant difference if their absolute rank difference is less than 1. You can do fit$predict(Xi,Xip) to get m predicted comparisons in c(-1,0,1), for m by p numeric matrices Xi and Xip. Also, fit$weight is the optimal vector of p numeric weights, and if fit$margin is positive then the data are separable.

Author(s)

Toby Dylan Hocking

Examples

 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
library(rankSVMcompare)
data(separable, envir=environment())
sol <- hardCompareLP(separable)
## check to make sure we have perfect prediction.
y.hat <- with(separable, sol$predict(Xi, Xip))
stopifnot(separable$yi == y.hat)
## This should also be the same:
fxdiff <- with(separable, sol$rank(Xip)-sol$rank(Xi))
y.hat2 <- ifelse(fxdiff < -1, -1L,
                 ifelse(fxdiff > 1, 1L, 0L))
stopifnot(y.hat == y.hat2)

## Calculate which points are on the margin.
margin <- ifelse(separable$yi==0,{
  1-abs(fxdiff)
},{
  -1 + separable$yi * fxdiff
})
on.margin <- abs(margin - sol$margin)<1e-6
diffs <- with(separable, {
  data.frame(Xip-Xi, yi,
             constraint=ifelse(on.margin, "active", "inactive"))
})

## Calculate the decision and margin lines.
arange <- range(diffs$angle)
seg <- function(v, line){
  d <- with(sol, (v-weight[2]*arange)/weight[1])
  data.frame(t(c(distance=d, angle=arange)), line)
}
seg.df <- rbind(seg(1-sol$margin,"margin"),
                seg(1+sol$margin,"margin"),
                seg(-1-sol$margin,"margin"),
                seg(-1+sol$margin,"margin"),
                seg(1,"decision"),
                seg(-1,"decision"))
library(ggplot2)
comparePlot <- ggplot()+
geom_point(aes(distance,angle,colour=factor(yi),
               size=constraint), data=diffs)+
scale_size_manual(values=c(active=2,inactive=1))+
geom_segment(aes(distance1,angle1,xend=distance2,yend=angle2,
                 linetype=line),data=seg.df)+
scale_linetype_manual(values=c(decision="solid",margin="dotted"))
print(comparePlot)

tdhock/rankSVMcompare documentation built on May 31, 2019, 7:38 a.m.