# HW 2 solution R code. # This is merely the R code; # Actual HWs should also include clearly stated answers # and insightful comments! airpol.full <- read.table("http://www.stat.sc.edu/~hitchcock/airpoll.txt", header=T) city.names <- as.character(airpol.full[,1]) airpol.data <- airpol.full[,2:8] attach(airpol.data) ################################# # Problem 1 stars(airpol.data, labels=city.names) library(TeachingDemos) faces( as.matrix(airpol.data), fill=T, labels=city.names) # For this function, the variables correspond to facial features in this order: # 1-height of face, 2-width of face, 3-shape of face, 4-height of mouth, 5-width of mouth, # 6-curve of smile, 7-height of eyes, 8-width of eyes, 9-height of hair, 10-width of hair, # 11-styling of hair, 12-height of nose, 13-width of nose, 14-width of ears, 15-height of ears. # Here's a way to generate a "key": faces.features <- c("Face height", "Face width", "Face shape", "Mouth height", "Mouth width", "Smile curve", "Eyes height", "Eyes width", "Hair height", "Hair width", "Hair styling", "Nose height", "Nose width", "Ears width", "Ears height") cbind( names(airpol.data), faces.features[1:length( names(airpol.data) )]) ################################# # problem 2 pairs(airpol.data) ################################# # problem 3 # ONLY GIVE A FEW OF THESE! Give general comments based on what you see. chiplot(SO2,Mortality,vlabs=c("SO2","Mortality")) chiplot(Nonwhite,NOX,vlabs=c("Nonwhite","NOX")) chiplot(Rainfall,Education,vlabs=c("Rainfall","Education")) ################################# # problem 4: bivbox(cbind(Education, Mortality), xlab = "Education", ylab = "Mortality", method ="O") # The robust method (which is the default): bivbox(cbind(Education, Mortality), xlab = "Education", ylab = "Mortality", method ="robust") ################################# # problem 5: plot(Education, Mortality, pch=1, lwd=2, ylim=c(780,1120), xlim=c(8,13)) # The x limits and y limits should be adjusted so that all the bubbles fit within the plotting window symbols(Education, Mortality, circles=Popden, inches=0.4, add=T, lwd=2) # Or, including abbreviated city names: plot(Education, Mortality, type='n', lwd=2, ylim=c(780,1120), xlim=c(8,13)) text(Education, Mortality, abbreviate(city.names)) symbols(Education, Mortality, circles=Popden, inches=0.4, add=T, lwd=2) ######################### # problem 6: library(carData); data(Salaries, package="carData") #(a): yarrr::pirateplot(formula = salary ~ sex:discipline, data = Salaries, main = "Pirateplot of Salaries", xlab = "group", ylab = "Salary", theme=1) boxplot(salary~sex:discipline, data = Salaries) #(b) and (c) # install.packages("ggplot2") # run this if 'ggplot2' package is not already installed library(ggplot2) qplot(yrs.since.phd, salary, col=factor(sex), shape=factor(sex), data=Salaries) + geom_smooth(method='lm',se=FALSE) + facet_wrap(~discipline)