EXERCISE 1: c <- read.table("http://www.stat.ucla.edu/~nchristo/statistics13/countries_life.txt", header=TRUE) #Part (a): plot(c$Income, c$Life) #Comment: As the Income increases we see see an increase of Life. But #when Income > $1000 the life expectancy levels off. #Part (b): boxplot(c$Income) boxplot(c$Life) hist(c$Income) hist(c$Life) #Or you can use the UsingR package: library(UsingR) simple.hist.and.boxplot(c$Income) simple.hist.and.boxplot(c$Life) #Part (c): c1 <- c[which(c$Income<1000), ] c2 <- c[which(c$Income>1000), ] #Part (d): plot(c1$Income, c1$Life) cor(c1$Income, c1$Life) ======================================================================= ======================================================================= EXERCISE 2: d <- read.table("http://www.stat.ucla.edu/~nchristo/statistics13/body_fat.txt", header=TRUE) #Part (a): summary(d$y) var(d$y) sd(d$y) #Part (b): boxplot(d$y) hist(d$y) #Or you can use the UsingR package: library(UsingR) simple.hist.and.boxplot(d$y) #Part(c): d1 <- d[which(d$x3<30), ] d2 <- d[which(d$x3>60), ] boxplot(d1$y, d2$y) #Part (d): summary(d1$y) var(d1$y) sd(d1$y) summary(d2$y) var(d2$y) sd(d2$y) #Part (e): cor(d1$y, d1$x10) cor(d2$y, d2$x10) ======================================================================= ======================================================================= EXERCISE 3: a <- read.table("http://www.stat.ucla.edu/~nchristo/statistics13/soil.txt", header=TRUE) #Part (a): summary(a$lead) summary(a$zinc) var(a$lead) var(a$zinc) sd(a$lead) sd(a$zinc) #Part (b): hist(a$lead) loglead <- log(a$lead) hist(loglead) #Part (c): logzinc <- log(a$zinc) plot(logzinc, loglead) #Part (d): lead_colors <- c("green", "yellow", "orange") lead_levels <- cut(a$lead, c(0, 150, 400, max(a$lead))) plot(a$x, a$y, "n", xlab="x", ylab="y") points(a$x, a$y, cex=a$lead/mean(a$lead), col=lead_colors[as.numeric(lead_levels)], pch=19) ======================================================================= ======================================================================= EXERCISE 4: b <- read.table("http://www.stat.ucla.edu/~nchristo/statistics13/la_data.txt", header=TRUE) #Part (a): library(maps) range(b$Longitude) #[1] -118.6322 -118.1746 range(b$Latitude) #[1] 33.780 34.313 #Comment: I computed the range for Longitude and Latitude so that we can #have an idea about where to extend the map. See the command below. plot(b$Longitude, b$Latitude, xlab="Longitude", ylab="Latitude", xlim=c(-118.8, -118.1), ylim=c(33.7, 34.4)) map("county", "ca", add=TRUE) #Part (b): plot(b$Income, b$Schools) ======================================================================= =======================================================================