Scatter Plot

load("lottery.rda")
attach(lottery)
par(mfrow=c(1,2))
plot(lottery[1,], xlim=c(0,1000), ylim=c(0,1000), axes=F, xlab="Number", ylab="Payoff")
axis(side=1, at=c(0,500,810,1000), labels=c(0,500,810,1000))
axis(side=2, at=c(0,190, 500 ,1000), labels=c(0,190, 500,1000))
text(lottery[1,], labels=c("(810, 190)"), pos=1)
arrows(x0=810,y0=190,x1=810,y1=0, code=2, length=0.2)
arrows(x0=810,y0=190,x1=0,y1=190, code=2, length=0.2)

id.0<-which(lottery$lottery.number==0)
lottery[id.0,]
##    lottery.number lottery.payoff
## 99              0             96
id.499<-which(lottery$lottery.number==499)
lottery[id.499,]
##     lottery.number lottery.payoff
## 10             499          869.5
## 132            499          247.5
id.999<-which(lottery$lottery.number==999)
lottery[id.999,]
##     lottery.number lottery.payoff
## 168            999            239
par(mfrow=c(1,2))
plot(lottery[c(id.0,id.499,id.999),], xlim=c(0,1000), ylim=c(0,1000), axes=F, xlab="Number", ylab="Payoff")
axis(side=1, at=c(0,500,810,1000), labels=c(0,500,810,1000))
axis(side=2, at=c(0,190, 500 ,1000), labels=c(0,190, 500,1000))
text(lottery[id.0,], labels="(0, 96)", pos=4)
text(lottery[c(id.499,id.999),], labels=c("(499, 869.5)", "(499, 247.5)", "(999, 239)"), pos=2)

plot(lottery.number, lottery.payoff,pch=20, ylim=c(0,1000))
abline(lsfit(lottery.number, lottery.payoff)$coef)
abline(h=seq(0,1000,by=250),lty=2)
abline(v=seq(0,1000,by=100),lty=2)
abline(lsfit(lottery.number, lottery.payoff)$coef)
lines(lowess(lottery.number,lottery.payoff, f=1/3),col="blue")
lines(lowess(lottery.number,lottery.payoff, f=2/3),col="red")
legend(x=0,y=1000, lty=1, col=c("black","blue","red"), legend=c("lsfit","lowess, f=1/3","lowess, f=2/3"))

apply(lottery, 2, fivenum)
##      lottery.number lottery.payoff
## [1,]            0.0          83.00
## [2,]          230.0         194.00
## [3,]          440.5         270.25
## [4,]          735.0         365.00
## [5,]          999.0         869.50
par(mfrow=c(1,2))
boxplot(lottery.number, main="Numbers Drawn")
boxplot(lottery.payoff, main="Payoff in Dollars")

lottery.fac<-lottery
lottery.fac$classes.10<-cut(lottery.fac$lottery.number, breaks=c(seq(0,900, by=100),999), right=F)
head(lottery.fac)
##   lottery.number lottery.payoff classes.10
## 1            810          190.0  [800,900)
## 2            156          120.5  [100,200)
## 3            140          285.5  [100,200)
## 4            542          184.0  [500,600)
## 5            507          384.5  [500,600)
## 6            972          324.5  [900,999)
detach()
attach(lottery.fac)
boxplot(lottery.payoff~classes.10, data=lottery.fac)

lottery.fac$classes<-factor(classes.10, labels=0:9)
head(lottery.fac)
##   lottery.number lottery.payoff classes.10 classes
## 1            810          190.0  [800,900)       8
## 2            156          120.5  [100,200)       1
## 3            140          285.5  [100,200)       1
## 4            542          184.0  [500,600)       5
## 5            507          384.5  [500,600)       5
## 6            972          324.5  [900,999)       9
boxplot(lottery.payoff~classes, data=lottery.fac, main="Payoff by Numbers Drawn")

opar<-par(no.readonly=TRUE)
par(fig=c(0,0.8,0,0.8))
plot(lottery.payoff~lottery.number, data=lottery.fac, pch=20, xlab="Numbers Drawn", ylab="Payoff in Dollars")
par(fig=c(0,0.8,0.55,1), new=TRUE)
hist(lottery.number, axes=F, ann=F)
par(fig=c(0.65,1,0,0.8), new=TRUE)
boxplot(lottery.payoff, horiz=TRUE, axes=F)

par(fig=c(0,0.8,0,0.8))
plot(lottery.payoff~lottery.number, data=lottery.fac, pch=20, xlab="Numbers Drawn", ylab="Payoff in Dollars")
par(fig=c(0,0.8,0.55,1), new=TRUE)
hist(lottery.number, axes=F, ann=F)
par(fig=c(0.7,1,0,0.8), new=TRUE)
barplot(table(cut(lottery.payoff, breaks=11)), horiz=T, space=0, col="white", axes=F, axisnames=F)

detach()
par(opar)
save(file="lottery.RData",list=ls())
savehistory("lottery.Rhistory")