# R script for part 4 # ladder of powers and roots postscript("transformations-ladder.ps",horizontal=FALSE) # create vector of x-coordinates x <- seq(from=0,to=4,by=.01) # Use the command 'plot' to plot the first line. # type = what type of plot should be drawn ("l" stands for lines) # lty = line type, lwd = line width, ylim = range on y-axis plot(x, (x^(-1)-1)/(-1), col=1, lty=1, lwd=2, type="l", ylim=c(-4,6), main="Family of powers and roots",xlab="x", ylab="x^{(p)}") # Use the command 'lines' to add the other lines to the plot lines(x, log(x), col=2, lty=2, lwd=2) lines(x, (x-1)/1, col=3, lty=3, lwd=2) lines(x, (x^2-1)/2, col=4, lty=4, lwd=2) lines(x, (x^3-1)/3, col=5, lty=5, lwd=2) # Add a legend: legend(0,6,c("p=-1", "p=0 (log)", "p=1", "p=2", "p=3"), col=c(1,2,3,4,5), lty=c(1,2,3,4,5), lwd=2) dev.off() ##### ##### ##### # negative values -> use positive start postscript("transformations-NegativeValues.ps", horizontal=FALSE) # subsequent figures will be drawn in a 2 x 1 array par(mfrow=c(2,1)) x <- seq(from=-2, to=2, by=.01) plot(x,x^2,type="l",lwd=2, main="Negative values give nonmonotone transformation") plot(x,(x+3)^2,type="l",lwd=2,main="Solution: use a positive 'start'") dev.off() ##### ##### ##### # ratio of larges to smallest value close to 1 -> use negative start postscript("transformations-RatioCloseTo1.ps", horizontal=FALSE) par(mfrow=c(2,1)) x <- seq(from=1991,to=1995, by=.01) plot(x,log(x),type="l", lwd=2, main="If the ratio between the largest and smallest value is close to 1, then the power tranformations are almost linear and hence ineffective") plot(x,log(x-1990), type="l", lwd=2, main="Solution: use a negative 'start'") dev.off() ##### ##### ##### # transforming skewness library(car) library(Hmisc) # needed to add tick marks to histograms data(Angell) attach(Angell) hist(mobility,probability=TRUE) scat1d(mobility,side=1) lines(density(mobility)) hist(log(mobility),probability=TRUE) scat1d(log(mobility),side=1) lines(density(log(mobility))) hist(sqrt(mobility),probability=TRUE) scat1d(sqrt(mobility),side=1) lines(density(sqrt(mobility))) ##### ##### ##### # transforming nonlinearity data(Leinhardt) attach(Leinhardt) ?Leinhardt summary(Leinhardt) # remove observations with missing values # '!' stand for not # is.na(..) indicates which elements are missing # we only take the rows for which infant mortality is not missing cleaned <- Leinhardt[!is.na(infant),] attach(cleaned) plot(income, infant, xlab="Per-capita income in U.S. dollars", ylab="Infant mortality rate per 1,000") lines(loess.smooth(income, infant), col="red", lwd=2) plot(log(income),log(infant),xlab="Log(per-capita income)", ylab="Log(infant mortality)") lines(loess.smooth(log(income),log(infant)), col="red", lwd=2)