#################################################################### # [ reading in data ] prestige <- read.table("prestige.dat") prestige[1:10,] names(prestige) _ c( "education", "income", "pct.female", "occ.prestige", "occ.code", "occ.type" ) prestige[1:10,] ################################################################### # [ assignment ] x _ c(1,2,3) # Note the use of "_" as a synonym for "<-" y _ c(4,5,6) x+y x*y X _ matrix(scan(),byrow=T,ncol=3) # note the use of scan() Y _ matrix(sample(1:10,9,replace=T),byrow=T,ncol=3) X Y X %*% Y dimnames(X) _ list(c("first row","second row","third row"),c("fred", "barney","wilma")) X dimnames(Y) dimnames(X) ############################################################# # [ Numerical Summaries ] dim(prestige) for (i in 1:6) print(summary(prestige[,i])) prestige[1,] summary(prestige$education) summary(prestige$income) summary(prestige$pct.female) summary(prestige$occ.prestige) summary(prestige$occ.code) summary(prestige$occ.type) sqrt(var(prestige$income)) sqrt(diag(var(prestige[,1:5]))) ############################################################ # [ Attach and Detach ] attach(prestige) detach() ############################################################ # [ Using Attach and Detach ] attach(prestige) stem(education) stem(income) stem(pct.female) stem(occ.code) stem(occ.type) dimnames(prestige)[[1]][order(-income)][1:4] detach() #################################################################### # [ Graphics, Graphic devices ] motif() # or win.graph() or graphsheet() on a PC par(mfrow=c(2,2)) for (i in c("education","income","pct.female","occ.prestige")) { hist(prestige[,i]) title(i) } postscript("mygraph.ps") par(mfrow=c(2,2)) for (i in c("education","income","pct.female","occ.prestige")) { hist(prestige[,i]) title(i) } dev.off() #################################################################### # [ Kernel Density Estimators ] par(mfrow=c(2,4)) for (i in names(prestige)[1:4]) { plot(density(prestige[,i]),type="l") title(i) } for (i in names(prestige)[1:4]) { plot(density(prestige[,i],width=diff(range(prestige[,i]))/4),type="l") title(i) } #################################################################### # [ Boxplots ] par(mfrow=c(2,4)) for (i in names(prestige)[1:4]) { boxplot(prestige[,i]) title(i) } for (i in names(prestige)[1:4]) { boxplot(prestige[,i],style.bxp="old") title(i) } #################################################################### # [ boxplots on the same scale ] par(mfrow=c(1,1)) boxplot(split(prestige$income,prestige$occ.type),style.bxp="old") attach(prestige) dimnames(prestige)[[1]][occ.type=="bc"][order(-income[occ.type=="bc"])][1:3] dimnames(prestige)[[1]][occ.type=="prof"][order(-income[occ.type=="prof"])][1:3] detach() ################################################################### # [ Normal probability plot -- several frames of this ] par(mfrow=c(3,2)) for (i in 1:6) { zz _ qqnorm(rnorm(102)) abline(lmsreg(zz$x,zz$y)) } mtext("Some qqplots of Normal data", outer=T,cex=1.5) ################################################################### # [ Transformations to achieve normality ] attach(prestige) par(mfrow=c(3,2)) plot(density(income),type="l") points(income,rep(0,102)) zz _ qqnorm(income) abline(lmsreg(zz$x,zz$y)) plot(density(sqrt(income)),type="l") points(sqrt(income),rep(0,102)) zz _ qqnorm(sqrt(income)) abline(lmsreg(zz$x,zz$y)) plot(density(log(income)),type="l") points(log(income),rep(0,102)) zz _ qqnorm(log(income)) abline(lmsreg(zz$x,zz$y)) ################################################################### # [ Box-Cox transformations ] par(mfrow=c(3,1)) boxplot(split(income,occ.type),style.bxp="old") title("income") boxplot(split((income^.25-1)/.25,occ.type),style.bxp="old") title("(income^.25-1)/.25") boxplot(split(log(income),occ.type),style.bxp="old") title("log(income)") ################################################################### # [ transforming a percent ] stem(pct.female) range(pct.female/100) newpct _ 0.001 + .998*pct.female/100 range(newpct) stem(log(newpct/(1-newpct))) stem(qnorm(newpct)) stem(asin(sqrt(newpct))) ################################################################### [ brush/spin and scatterplot matrices ]