train <- read.table("loans.dat",header=F,col.names=c("x1","x2","y"));

x1 <- train$x1;
x2 <- train$x2;
y  <- train$y;

fit <- lsfit(cbind(x1,x2),y,intercept=TRUE);
b <- as.numeric(fit$coef);

y <- (y==1);

size <- 100;
grid <- mat.or.vec((size+1)*(size+1),2);
for (i in 0:size) {
for (j in 0:size) {
  grid[(size+1)*j+i+1,1] <- i;
  grid[(size+1)*j+i+1,2] <- j;
}
}
grid <- grid/(size+1);

gx1 <- grid[,1];
gx2 <- grid[,2];

pred <- b[1] + b[2]*gx1 + b[3]*gx2;
dflt <- (pred > 0.5);

edge <- ((0.49<pred)&(pred<0.51));

source("psopts.r");
postscript(file="linr.eps");

plot(x=x1,y=x2,type='n',xlab="FICO Score",ylab="P-Index");
points(x=x1[y],y=x2[y],pch='o',col="red");
points(x=x1[!y],y=x2[!y],pch='o',col="green");
points(grid[dflt,],pch='.',col="red");
points(grid[!dflt,],pch='.',col="green");
lines(lowess(grid[edge,]));

dev.off();

test <- read.table("eval.dat",header=F,col.names=c("x1","x2","y"));

tx1 <- test$x1;
tx2 <- test$x2;
ty  <- test$y;

pred <- b[1] + b[2]*tx1 + b[3]*tx2;
dflt <- (pred > 0.5);

dflt <- as.numeric(dflt);

err <- abs(dflt-ty);
print(mean(err));
