lecture10
========================================================
author: 
date: 

Today
========================================================
- Random numbers (and functions for distributions)
- Plotting (part 1)

r, d, p, q 
========================================================

-**r**andom

-**d**ensity

-**p**robability

-**q**uantile

-letter followed by distrubtion abbreviation is/does each one

-example abbreviations: norm, t, f, exp, binom, pois, unif, etc.

rnorm(n=100,mean=2,sd=3) makes n random #s
========================================================
```{r fig.width=10,tidy=TRUE}
par(cex=2)
hist(rnorm(n=100,mean=2,sd=3))
```

dnorm(x,mean=2,sd=3) evaluates pdf @ x
========================================================
```{r fig.width=10,tidy=TRUE}
par(cex=2)
curve(dnorm(x,mean=2,sd=3),from=2-3*3,to=2+3*3)
```

pnorm(x,mean=2,sd=3) evaluates cdf @ x
========================================================
```{r fig.width=10,tidy=TRUE}
par(cex=2)
curve(pnorm(x,mean=2,sd=3),from=2-3*3,to=2+3*3)
```

qnorm(p,mean=2,sd=3) inverse cdf @ p
========================================================
```{r fig.width=10,tidy=TRUE}
par(cex=2)
curve(qnorm(x,mean=2,sd=3),from=0,to=1,xlab="probability")
```

Plotting - If you can think it, R can draw it
========================================================
```{r eval=F}
example(plot) # try these on your own
example(barplot)
example(boxplot)
example(dotchart)
example(coplot)
example(hist)
example(fourfoldplot)
example(stars)
example(image)
example(contour)
example(filled.contour)
example(persp)
```

Five topics (1 and 2 today)
=======
1. Setting up the plotting frame: $\tt{plot()}$

2. Adding to an existing ploting frame: $\tt{lines(),points(),text(),polygon(),arrows(),\ldots}$

3. More control over the plotting frame: $\tt{par()}$

4. Multiple plots in a frame: $\tt{par(mfrow=c(rows,columns)), layout()}$

5. Making pdfs: 
$\tt{pdf(), dev.off()}$

Establish a plotting frame
=====
```{r tidy=T,fig.width=9,cache=T}
x <- c(1,2,5)
y <- c(5,3,7)
par(cex=2)
plot(x,y,xlab="x axis label",ylab="y axis label",main="main title",sub="sub title",xlim=c(0,10),ylim=c(2,20),type="p")
```
Establish a plotting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,xlab="x axis label",ylab="y axis label",main="main title",sub="sub title",xlim=c(0,10),ylim=c(2,20),type="l")
```
Establish a plotting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,xlab="x axis label",ylab="y axis label",main="main title",sub="sub title",xlim=c(0,10),ylim=c(2,20),type="n")
```

Establish a plotting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="b",axes=F,xlab="Animal")
```

Establish a plotting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="b",axes=F,xlab="Animal")
axis(1,at=c(1,2,5),labels=c("Cat","Dog","Rat"))
axis(2)
```

Adding to an existing ploting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="n",axes=F,xlab="Animal")
points(x,y,pch=16,cex=1.5)
axis(1)
axis(2)
```

Adding to an existing ploting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="n",axes=F,xlab="Animal")
lines(x,y,lty=2,lwd=4,col="red")
axis(1)
axis(2)
```

Adding to an existing ploting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="n",axes=F,xlab="Animal",xlim=c(0,6))
lines(x,y,lty=2,lwd=4,col="red")
text(x,y,c("Cat","Dog","Rat"))
axis(1)
axis(2)
```

Adding to an existing ploting frame
=====
```{r tidy=T,fig.width=9}
par(cex=2)
plot(x,y,type="n",axes=T,xlab="Animal",xlim=c(0,6),ylim=c(1,8))
lines(x,y,lty=2,lwd=4,col="red")
text(x,y,c("Cat","Dog","Rat"),pos=c(3,1,3))
```

Example:
========
```{r tidy=T,cache=T}
ests <- c(1,1.25,1.5)
ns <- c(20,15,10)
sds <- c(.3,.4,.2)
est.table <- data.frame(condition=c("Placebo","Treatment A","Treatment B"),
							est.effect=ests,
							lowers=ests-qt(.975,df=(ns-1))*sds/sqrt(ns),
							uppers=ests+qt(.975,df=(ns-1))*sds/sqrt(ns))
est.table
```

Example:
========
```{r tidy=T,eval=F}
plot(1:3,1:3,xlim=c(.75,3.25),ylim=range(est.table[,-1]),type="n",axes=F,
		xlab="Condition",ylab="Effect",sub="Estimates and 95% Confidence Interval")
axis(2)
axis(1,at=1:3,labels=est.table$condition)
for (i in 1:dim(est.table)[1])
{
	points(i,est.table$est.effect[i],pch=16,cex=1.5)
	lines(c(i,i),c(est.table$lowers[i],est.table$uppers[i]))	}
text(3,est.table$lowers[3],pos=1,"Diff from placebo (p<0.05)",cex=.6)
```
Example:
========
```{r tidy=T,echo=F,fig.width=10}
ests <- c(1,1.25,1.5)
ns <- c(20,15,10)
sds <- c(.3,.4,.2)
par(cex=1.75)
est.table <- data.frame(condition=c("Placebo","Treat A","Treat B"),
							est.effect=ests,
							lowers=ests-qt(.975,df=(ns-1))*sds/sqrt(ns),
							uppers=ests+qt(.975,df=(ns-1))*sds/sqrt(ns))

plot(1:3,1:3,xlim=c(.75,3.25),ylim=range(est.table[,-1]),type="n",axes=F,
		xlab="Condition",ylab="Effect",sub="Estimates and 95% Confidence Interval")
axis(2)
axis(1,at=1:3,labels=est.table$condition)
for (i in 1:dim(est.table)[1])
{
	points(i,est.table$est.effect[i],pch=16,cex=1.5)
	lines(c(i,i),c(est.table$lowers[i],est.table$uppers[i]))	}
text(3,est.table$lowers[3],pos=1,"Diff from placebo (p<0.05)",cex=.6)
```


To be continued
=====
(Next 3 topics on another day...)




