### R code from vignette source 'usingr'
### Encoding: UTF-8

###################################################
### code chunk number 1: usingr.rnw:2-6
###################################################
options( width=90,
#        prompt=" ", continue=" ",
         SweaveHooks=list( fig=function()
         par(mar=c(3,3,1,1),mgp=c(3,1,0)/1.6,las=1,bty="n") ) )


###################################################
### code chunk number 2: usingr.rnw:228-230
###################################################
x <- 1:10
x


###################################################
### code chunk number 3: usingr.rnw:247-258
###################################################
sum(x)
sd(x)
diff(x)
cumsum(x)
rev(x)
x > 7
x >= 7
sin(x)
pi
sin(pi*x)
sin(pi*x)==0


###################################################
### code chunk number 4: totry.rnw:19-20
###################################################
a <- 2+2


###################################################
### code chunk number 5: totry.rnw:26-27
###################################################
2+2 -> a


###################################################
### code chunk number 6: totry.rnw:34-35
###################################################
pnorm(1.96) 


###################################################
### code chunk number 7: totry.rnw:38-39
###################################################
pchisq(3.84,1) 


###################################################
### code chunk number 8: totry.rnw:43-44
###################################################
pchisq(3.84,1,lower.tail=FALSE) 


###################################################
### code chunk number 9: totry.rnw:69-70
###################################################
v <- c(4, 6, 1, 2.2)


###################################################
### code chunk number 10: totry.rnw:81-82
###################################################
m <- list(4, 6, "name of company")


###################################################
### code chunk number 11: totry.rnw:90-93
###################################################
v
3+v
3*v


###################################################
### code chunk number 12: totry.rnw:109-110
###################################################
seq(15, 85, by=5)


###################################################
### code chunk number 13: totry.rnw:114-115
###################################################
c(5,seq(20, 85, by=5))


###################################################
### code chunk number 14: totry.rnw:118-121
###################################################
rep( c(3,2,7), c(1,4,3) )
rep( c(3,2,7), 5 )
rep( c(3,2,7), each=5 )


###################################################
### code chunk number 15: totry.rnw:169-170
###################################################
library(Epi)


###################################################
### code chunk number 16: totry.rnw:173-174
###################################################
data(births)


###################################################
### code chunk number 17: totry.rnw:178-179 (eval = FALSE)
###################################################
## ?births


###################################################
### code chunk number 18: totry.rnw:188-189
###################################################
str(births)


###################################################
### code chunk number 19: totry.rnw:199-200
###################################################
data(diet)


###################################################
### code chunk number 20: totry.rnw:208-209
###################################################
rm(diet)


###################################################
### code chunk number 21: totry.rnw:220-221
###################################################
births[1,"bweight"]


###################################################
### code chunk number 22: totry.rnw:225-226
###################################################
births[2,"bweight"]


###################################################
### code chunk number 23: totry.rnw:231-232
###################################################
births[1:10, "bweight"]


###################################################
### code chunk number 24: totry.rnw:235-236
###################################################
births[, "bweight"]


###################################################
### code chunk number 25: totry.rnw:250-251
###################################################
summary(births)


###################################################
### code chunk number 26: totry.rnw:254-255
###################################################
names(births)


###################################################
### code chunk number 27: totry.rnw:263-264
###################################################
summary(births$hyp)


###################################################
### code chunk number 28: totry.rnw:286-289
###################################################
hyp <- factor( births$hyp )
str(births)
lls()


###################################################
### code chunk number 29: totry.rnw:296-298
###################################################
births <- transform( births, hyp=factor(hyp) )
str(births)


###################################################
### code chunk number 30: totry.rnw:304-307
###################################################
births <- transform( births,
                     hyp = factor(hyp,labels=c("normal","hyper")) )
str(births)


###################################################
### code chunk number 31: totry.rnw:327-330
###################################################
data( births )
stat.table( hyp, data=births )
stat.table( sex, data=births )


###################################################
### code chunk number 32: totry.rnw:334-335
###################################################
stat.table( list(hyp,sex), data=births )


###################################################
### code chunk number 33: totry.rnw:352-356
###################################################
births <- transform(births, agegrp=cut(matage, 
                                       breaks=c(20,30,35,40,45),
                                        right=FALSE) )
stat.table( agegrp, data=births )


###################################################
### code chunk number 34: totry.rnw:367-371
###################################################
births <- transform( births, agegrp=cut(matage, 
                                        breaks=c(20,30,35),
                                         right=FALSE) )
summary(births)


###################################################
### code chunk number 35: totry.rnw:378-382
###################################################
births <- transform( births, agegrp=cut(matage,
                                        breaks=5,
                                         right=FALSE) )
stat.table( agegrp, data=births, margins=TRUE )


###################################################
### code chunk number 36: totry.rnw:401-402
###################################################
stat.table(sex, mean(bweight), data=births)


###################################################
### code chunk number 37: totry.rnw:405-408
###################################################
stat.table( sex, 
            list("Mean birth weight"=mean(bweight)),
            data=births )


###################################################
### code chunk number 38: totry.rnw:412-416
###################################################
stat.table( list(sex),
            mean(bweight),
            margins=TRUE,
            data=births )


###################################################
### code chunk number 39: totry.rnw:419-423
###################################################
stat.table( list(sex,hyp),
            list(count(),
                 mean(bweight)),
            data=births )


###################################################
### code chunk number 40: totry.rnw:430-433
###################################################
stat.table( hyp, 
            list("odds"=ratio(lowbw,1-lowbw,100)),
            data=births)


###################################################
### code chunk number 41: totry.rnw:437-441
###################################################
stat.table(sex, 
           mean(bweight),
           margins=TRUE,
           data=births )


###################################################
### code chunk number 42: totry.rnw:444-447
###################################################
stat.table(list(sex,hyp),mean(bweight),data=births,margins=c(TRUE,FALSE))
stat.table(list(sex,hyp),mean(bweight),data=births,margins=c(FALSE,TRUE))
stat.table(list(sex,hyp),mean(bweight),data=births,margins=TRUE)


###################################################
### code chunk number 43: totry.rnw:480-484
###################################################
births <- transform(births,
          num1=1,
          num2=2,
          logbw=log(bweight))


###################################################
### code chunk number 44: totry.rnw:495-497
###################################################
low <- births$bweight<2000
str( low )


###################################################
### code chunk number 45: totry.rnw:510-511
###################################################
births$bweight[births$hyp=="hyper"]


###################################################
### code chunk number 46: totry.rnw:514-515
###################################################
births[births$hyp=="hyper",]


###################################################
### code chunk number 47: totry.rnw:519-520
###################################################
subset(births, hyp=="hyper")


###################################################
### code chunk number 48: totry.rnw:532-535
###################################################
library(Epi)
data(births)
sweave.wd <- getwd()


###################################################
### code chunk number 49: totry.rnw:548-550
###################################################
sink("output.txt")
summary(births)


###################################################
### code chunk number 50: totry.rnw:560-561
###################################################
sink()


###################################################
### code chunk number 51: totry.rnw:582-583
###################################################
save(births, file="births.Rdata")


###################################################
### code chunk number 52: totry.rnw:589-590
###################################################
load("births.Rdata")


###################################################
### code chunk number 53: totry.rnw:600-601
###################################################
search()


###################################################
### code chunk number 54: totry.rnw:607-610
###################################################
objects()
ls()
lls()


###################################################
### code chunk number 55: totry.rnw:613-614
###################################################
objects(2)


###################################################
### code chunk number 56: totry.rnw:636-637
###################################################
attach(births)


###################################################
### code chunk number 57: totry.rnw:641-642
###################################################
objects(2)


###################################################
### code chunk number 58: totry.rnw:649-650
###################################################
hyp


###################################################
### code chunk number 59: totry.rnw:659-660
###################################################
subgrp <- bweight[hyp==1]


###################################################
### code chunk number 60: totry.rnw:664-666
###################################################
objects(1)
objects(2)


###################################################
### code chunk number 61: totry.rnw:677-679
###################################################
attach(births)
search()


###################################################
### code chunk number 62: totry.rnw:684-685
###################################################
detach(births)


###################################################
### code chunk number 63: totry.rnw:722-724
###################################################
library(Epi)
data(births)


###################################################
### code chunk number 64: totry.rnw:727-729
###################################################
attach(births)
hist(bweight)


###################################################
### code chunk number 65: totry.rnw:732-733 (eval = FALSE)
###################################################
## ?hist


###################################################
### code chunk number 66: totry.rnw:736-737
###################################################
hist(bweight, col="gray", border="white")


###################################################
### code chunk number 67: totry.rnw:740-741
###################################################
plot(gestwks, bweight)


###################################################
### code chunk number 68: totry.rnw:745-746
###################################################
plot(1:25, pch=1:25)


###################################################
### code chunk number 69: totry.rnw:749-752
###################################################
plot( rep(1:5,5), rep(1:5,each=5), pch=1:25, 
      cex=5, xlim=c(0,6), ylim=c(0,6), lwd=4 )
text(rep(1:5,5)+0.3, rep(1:5,each=5), 1:25 )


###################################################
### code chunk number 70: totry.rnw:757-758
###################################################
plot(matage, bweight)


###################################################
### code chunk number 71: totry.rnw:761-762
###################################################
plot(matage, bweight, xlab="Maternal age", ylab="Birth weight (g)")


###################################################
### code chunk number 72: totry.rnw:773-774
###################################################
plot(gestwks, bweight, pch=16, col="green")


###################################################
### code chunk number 73: totry.rnw:780-781
###################################################
points(gestwks, bweight)


###################################################
### code chunk number 74: totry.rnw:793-794
###################################################
plot(gestwks, bweight, type="n")


###################################################
### code chunk number 75: totry.rnw:800-802
###################################################
points(gestwks[sex==1], bweight[sex==1], col="blue")
points(gestwks[sex==2], bweight[sex==2], col="red")


###################################################
### code chunk number 76: totry.rnw:805-806 (eval = FALSE)
###################################################
## legend("topleft", pch=1, legend=c("Boys","Girls"), col=c("blue","red"))


###################################################
### code chunk number 77: totry.rnw:811-812 (eval = FALSE)
###################################################
## title("Birth weight vs gestational weeks in 500 singleton births")


###################################################
### code chunk number 78: totry.rnw:832-834
###################################################
c("blue","red")
births$sex


###################################################
### code chunk number 79: totry.rnw:837-838
###################################################
c("blue","red")[sex]


###################################################
### code chunk number 80: totry.rnw:845-846
###################################################
plot( gestwks, bweight, pch=16, col=c("blue","red")[sex] )


###################################################
### code chunk number 81: totry.rnw:851-853
###################################################
magr <- cut( matage, c(0,30,35,100) )
table( magr )


###################################################
### code chunk number 82: totry.rnw:860-863
###################################################
plot( gestwks, bweight, pch=(15:17)[magr], col=c("blue","red")[sex] )
legend("topleft", pch=15:17, legend=levels(magr), col=1, bty="n")
text( 28, 4200+0:1*200, c("Boys","Girls"), col=c("blue","red"), adj=0 )


###################################################
### code chunk number 83: totry.rnw:874-875
###################################################
rainbow(4)


###################################################
### code chunk number 84: totry.rnw:885-887
###################################################
plot( 0:10, pch=16, cex=3, col=gray(0:10/10) )
points( 0:10, pch=1, cex=3 )


###################################################
### code chunk number 85: totry.rnw:900-901
###################################################
plot( gestwks, bweight, pch=(15:17)[magr], col=c("blue","red")[sex] )


###################################################
### code chunk number 86: totry.rnw:904-906 (eval = FALSE)
###################################################
## legend( locator(1), pch=15:17, legend=levels(magr), col=1, bty="n")
## text( locator(2), c("Boys","Girls"), col=c("blue","red"), adj=0 )


###################################################
### code chunk number 87: totry.rnw:917-918 (eval = FALSE)
###################################################
## identify( gestwks, bweight )


###################################################
### code chunk number 88: totry.rnw:963-966 (eval = FALSE)
###################################################
## pdf(file="plot1.pdf", height=3, width=4)
## plot(gestwks, bweight)
## dev.off()


###################################################
### code chunk number 89: totry.rnw:970-973 (eval = FALSE)
###################################################
## win.metafile(file="plot1.emf", height=3, width=4)
## plot(gestwks, bweight)
## dev.off()


###################################################
### code chunk number 90: totry.rnw:986-1002 (eval = FALSE)
###################################################
## myplfn <- function() # Define the function that does the plot
## {
## plot( gestwks, bweight, pch=(15:17)[magr], col=c("blue","red")[sex] )
## legend("topleft", pch=15:17, legend=levels(magr), col=1, bty="n")
## text( 28, 4200+0:1*200, c("Boys","Girls"), col=c("blue","red"), adj=0 )
## }
## 
## myplfn() # on the screen
## 
## pdf( "plot1.pdf", height=8, width=10 ) # pdf graph
## myplfn() 
## dev.off()
## 
## postscript( "plot1.eps", height=8, width=10 ) # eps graph
## myplfn() 
## dev.off()


###################################################
### code chunk number 91: totry.rnw:1027-1028
###################################################
par( mfrow=c(2,3) )


###################################################
### code chunk number 92: totry.rnw:1035-1036
###################################################
par( mfrow=c(1,1) )


###################################################
### code chunk number 93: Get the UCBAdmissions data
###################################################
str(UCBAdmissions)


###################################################
### code chunk number 94: totry.rnw:1060-1061
###################################################
ftable( UCBAdmissions )


###################################################
### code chunk number 95: Convert the 2x2x6 contingency table to a data frame
###################################################
ucb <- as.data.frame( UCBAdmissions )
head(ucb)
str(ucb)


###################################################
### code chunk number 96: Convert Admit to numeric coded 0/1
###################################################
ucb$Admit <- (ucb$Admit=="Rejected") * 1


###################################################
### code chunk number 97: totry.rnw:1092-1106
###################################################
effx( Admit=="Rejected", "binary", 
      exposure=Gender, 
        weight=Freq,
          data=ucb ) 
effx( Admit=="Rejected", "binary", 
      exposure=Gender, 
       control=Dept, 
        weight=Freq,
          data=ucb ) 
effx( Admit=="Rejected", "binary", 
      exposure=Gender, 
        strata=Dept, 
        weight=Freq,
          data=ucb ) 


###################################################
### code chunk number 98: mosaic
###################################################
dimnames(UCBAdmissions)[[1]] <- c("Adm","Rej")
par( mar=c(.1,1,1,.1))
mosaicplot( UCBAdmissions, sort=3:1, col=TRUE,
            main="", ylab = "", xlab = "", off=c(0,1,15) )


###################################################
### code chunk number 99: totry.rnw:1185-1189
###################################################
mA <- NArray( list(resp = c("bweight","gestwks"),
                    sex = c("M","F"),
                    res = c("est","lo","hi","P")) )
str( mA )


###################################################
### code chunk number 100: totry.rnw:1197-1207
###################################################
births$sex <- factor( births$sex, labels=c("M","F") )
for( is in c("M","F") )
for( ir in c("bweight","gestwks") )
   {
   mod <- lm( births[births$sex==is,ir] ~ births[births$sex==is,"matage"] )
   mA[ir,is,] <- ci.lin(mod,subset="sex")[,c("Estimate","2.5%","97.5%","P")]
   }
round( ftable(mA), 3 )
round( ftable(mA,row.vars=2:1), 3 )
round( ftable(mA,col.vars=1:2), 3 )


###################################################
### code chunk number 101: totry.rnw:1246-1248
###################################################
data( diet )
str( diet ) 


###################################################
### code chunk number 102: totry.rnw:1253-1254
###################################################
head( diet )


###################################################
### code chunk number 103: totry.rnw:1260-1262
###################################################
as.Date( "14/07/1952", format="%d/%m/%Y" ) 
as.numeric( as.Date( "14/07/1952", format="%d/%m/%Y" ) )


###################################################
### code chunk number 104: totry.rnw:1279-1280
###################################################
newx <- pmin( diet$dox, as.Date( "1975-4-1", format="%F" ) )


###################################################
### code chunk number 105: totry.rnw:1287-1288
###################################################
newx <- pmin( diet$dox, as.Date("1975-4-1") )


###################################################
### code chunk number 106: totry.rnw:1291-1292
###################################################
as.Date(c("1997-02-28","1997-02-29","1997-13-29"))


###################################################
### code chunk number 107: totry.rnw:1296-1298
###################################################
  bdat <- as.Date( "1952-7-14", format="%F" )
  format( bdat, format="%A %d %B %Y" )


###################################################
### code chunk number 108: totry.rnw:1306-1311
###################################################
cal.yr( as.Date(c('1970-1-1',
                  '1971-1-1',
                  '1972-1-1',
                  '1973-1-1',
                  '1974-1-1')) )


