## Chapter 1: Introduction ## Sec 1.1: Commentary on R ## General ## Getting help ## Use of an editor as a run-time environment ## The development model, and development strategies ## Unifying ideas ## Retrospect, prospect and alternatives to R ## Data set size, and databases ## The statistics of data collection ## Sec 1.2: Installation of R and of R Packages ## Help for installation under windows ## Sec 1.3: Documentation ## Chapter 2: An Overview of R ## Sec 2.1: Use of the console (i.e., command line) window 2+2 q() ## Practice with R commands 1:5 # The numbers 1, 2, 3, 4, 5 mean(1:5) sum(1:5) # Apply the sum function to the vector # of numbers 1, 2, 3, 4, 5 (1:5) > 2 # Returns FALSE FALSE TRUE TRUE TRUE # Other relational operators are: >=, <, <=, ==, != (2:5)^10 # 2 to the power of 10, 3 to the power of 10, ... log2(c(0.5, 1, 2, 4, 8)) # Values that differ by a factor of 2 # are, on this scale, one unit apart. help() # help on use of the help function help(plot) # the help page for the plot function example(plot) # Runs the examples from the help page for plot() par(ask=FALSE) # Do not now ask, before displaying a new plot. ## Sec 2.2: A Short R Session ## Entry of vector elements from the command line volume <- c(351, 955, 662, 1203, 557, 460) weight <- c(250, 840, 550, 1360, 640, 420) description <- c("Aird's Guide to Sydney", "Moon's Australia handbook", "Explore Australia Road Atlas", "Australian Motoring Guide", "Penguin Touring Atlas", "Canberra - The Guide") ## Operations with \txtt{vectors} volume # Final element of volume volume[6] ## Ratio of weight to volume, i.e., density round(weight/volume,2) ## A simple plot ## Code plot(weight ~ volume, pch=16, cex=1.5) # pch=16: use solid blob as plot symbol # cex=1.5: point size is 1.5 times default ## Alternative plot(volume, weight, pch=16, cex=1.5) plot(weight ~ volume, pch=16, cex=1.5, xlab="Volume (cubic mm)", ylab="Weight (g)") identify(weight ~ volume, labels=description) ## Formatting and layout of plots ## Sec 2.3: Data frames -- Grouping together columns of data ## NB, the row names will now be shortened travelbooks <- data.frame( thickness = c(1.3, 3.9, 1.2, 2, 0.6, 1.5), width = c(11.3, 13.1, 20, 21.1, 25.8, 13.1), height = c(23.9, 18.7, 27.6, 28.5, 36, 23.4), weight = weight, # Include values of weight, entered earlier volume = volume, # Include values of volume, entered earlier type = c("Guide", "Guide", "Roadmaps", "Roadmaps", "Roadmaps", "Guide"), row.names = description ) ## Remove objects that are not now needed. rm(volume, weight, description) ## Accessing the columns of data frames travelbooks[, 4] travelbooks[, "weight"] travelbooks$weight travelbooks[["weight"]] # This treats the data frame as a list. ## 1: Use the data parameter in the function call plot( weight ~ volume, data=travelbooks) # ## 2: Use with(); take columns from the specified data frame with(travelbooks, plot(weight ~ volume)) # ## 3: Use attach() to include the column names in the search list attach(travelbooks) plot( weight ~ volume) detach(travelbooks) # Detach when no longer required ## Sec 2.4: Input of Data from a File ## Place the file in the working directory library(DAAGxtras) # DAAGxtras has the needed function dataFile("travelbooks") # Place file in directory dir() # List contents of the working directory file.show("travelbooks.txt") # Display travelbooks.txt ## Now input the file, to the data frame travelbooks travelbooks <- read.table("travelbooks.txt") # Row 1 of the file gives column names. Column 1 gives row names ## Explicitly specify details of header and row name information travelbooks <- read.table("travelbooks.txt", header=TRUE, row.names=1) ## Sec 2.5: Summary ## Sec 2.6: Exercise ## Chapter 3: The Working Environment of an R Session ## Sec 3.1: The Working Directory and the Workspace ## Listing Workspace Contents ls() ls(pattern="^w") ## Setting the Working Directory ## Sec 3.2: Saving and retrieving R objects save.image(file="archive.RData") save(volume, weight, file="books.RData") # Can save many objects in the same file load("books.RData") # Recover the saved objects ## Writing data frames to text files ## Sec 3.3: Installations, packages and sessions ## ss 3.3.1: The architecture of an R installation -- Packages sessionInfo() ## Installation of R packages install.packages(pkgs="D:/DAAG_0.91.zip", repos=NULL) ## ss 3.3.2: The search path: library() and attach() search() ## Attachment of R packages ## Attachment of image files attach("books.RData") detach("file:books.RData") ## Sec 3.4: Demonstrations, Help \& Help Examples demo() demo(image) demo(graphics) demo(persp) demo(plotmath) # Mathematical symbols can be visually interesting library(lattice) demo(lattice) # Demonstrates lattice graphics demo(package = .packages(all.available = TRUE)) library(vcd) # The vcd package must of course be installed. demo(mosaic) ## Help pages, and examples that are included on help pages help(help) # Get help on help() help(mean) example(plot) ## Access to help resources from a browser screen ## Other resources ## Sec 3.5: Summary ## Chapter 4: Worked Examples ## Sec 4.1: World record times for track and field events ## Data exploration str(worldRecords) library{DAAGxtras} # Version 0.6-6 or later of DAAGxtras plot(Time ~ Distance, data=worldRecords) ## Now use a log scale plot(Time ~ Distance, data=worldRecords, log="xy") ## Code ## Attach lattice package library(lattice) ## Left panel xyplot(Time ~ Distance, groups=roadORtrack, data=worldRecords, scales=list(log=10), auto.key=list(columns=2)) ## Right panel xyplot(log(Time) ~ log(Distance), groups=roadORtrack, data=worldRecords, auto.key=list(columns=2)) ## Fiting a regression line lm(log(Time) ~ log(Distance), worldRecords) ## ss 4.1.1: Summary information from model objects ## Store the result in worldrec.lm worldrec.lm <- lm(log(Time) ~ log(Distance), worldRecords) plot(log(Time) ~ log(Distance), data = worldRecords) abline(worldrec.lm) print(worldrec.lm) # Equivalent to typing wtvol.lm at the command line summary(worldrec.lm) ## Diagnostic plots par(mfrow=c(1,2)) # Subsequent plots appear in a 1 x 2 layout plot(worldrecs.lm, which=1:2) par(mfrow=c(1,1)) # Reset to 1 plot per page, for any later plots ## ss 4.1.2: The model object worldrec.lm$call coef(worldrec.lm) ## Sec 4.2: Regression with two explanatory variables ## Data exploration str(nihills) ## Code library(lattice) splom(~nihills) ## Correlation matrix round(cor(nihills), 2) ## Code lognihills <- log(nihills) names(lognihills) <- c("ldist", "lclimb", "ltime", "ltimef") library(lattice) splom(~lognihills) ## Correlation matrix round(cor(lognihills), 2) library(lattice) splom(~nihills) lognihills <- log(nihills) splom(~lognihills), varnames=c("log(dist)", "log(climb)", "log(time)", "log(timef)")) ## ss 4.2.1: The regression fit > names(lognihills) <- paste("l", names(nihills), sep="") > lognihills.lm <- lm(ltime ~ ldist + lclimb, data=lognihills) > round(coef(lognihills.lm),3) (Intercept) ldist lclimb 3.205 0.686 0.502 > nihills$gradient <- with(nihills, climb/dist) > lognihills <- log(nihills) > names(lognihills) <- paste("l", names(nihills), sep="") > lognigrad.lm <- lm(ltime ~ ldist + lgradient, data=lognihills) > round(coef(lognigrad.lm),3) (Intercept) ldist lgradient 3.228 1.147 0.466 ## Plot the terms in the model termplot(lognigrad.lm, col.term="gray", partial=TRUE, col.res="black", smooth=panel.smooth) ## Sec 4.3: Time series data -- Australian rain records ## Code for Panel A plot(seRain~Year, data=bomrain) with(bomrain, lines(lowess(seRain~Year))) ## Footnote Code ## Sec 4.4: Exercises huron <- data.frame(year=as(time(LakeHuron), "vector"), mean.height=LakeHuron) identify(huron$year, huron$mean.height, labels=huron$year) lag.plot(huron$mean.height) plot(LakeHuron) identify(LakeHuron, labels=time(LakeHuron))