# Exercise 8.1 # 1 library(psych) plot(SATV ~ SATQ, data = sat.act, type = "n", xlab = "SAT (Quantitative)", ylab = "SAT (Verbal)", main = "Relationship between Subscales of SAT", xlim = c(200, 800), ylim = c(200, 800)) with(sat.act[sat.act$gender == 1,], points(SATQ, SATV, pch = 0, col = "red")) with(sat.act[sat.act$gender == 2,], points(SATQ, SATV, pch = 1, col = "green")) with(sat.act[sat.act$gender == 1,], abline(lm(SATV ~ SATQ), col = "red")) with(sat.act[sat.act$gender == 2,], abline(lm(SATV ~ SATQ), col = "green")) legend("topleft", c("Male", "Female"), lty = 1, pch = c(0, 1), col = c("red", "green")) # 2 boxplot(ACT ~ gender, data = sat.act, horizontal = TRUE, xlab = "ACT", boxwex = 0.5, col = c("blue", "red")) # 3 means <- with(sat.act, tapply(SATQ, list(gender, education), mean, na.rm = TRUE)) sds <- with(sat.act, tapply(SATQ, list(gender, education), sd, na.rm = TRUE)) barout <- barplot(means, beside = TRUE, ylim = c(0, 800), legend.text = TRUE, col = c("pink", "darkorange"), args.legend = list(x = 5, y = 900, legend = c("Male", "Female"))) arrows(barout, means + sds, barout, means - sds, angle = 90, code = 3) abline(h = 0) # 4 and 5 pdf("histogram.pdf", width = 5, height = 5) hist(sat.act$ACT, prob = TRUE, xlab = "ACT", main = "The Distribution of ACT Scores", ylim = c(0, 0.1)) lines(density(sat.act$ACT), lty = 2, col = "red") dev.off() # 6 par(mfrow = c(1, 3)) hist(sat.act$SATQ, prob = TRUE, xlab = "SAT (Quantitative)") hist(sat.act$SATQ, prob = TRUE, xlab = "SAT (Verbal)") hist(sat.act$ACT, prob = TRUE, xlab = "ACT") ### Assignment 8 grade1 <- c(2.1, 2.9, 2.6, 2.7, 2.9, 3.6, 2.6, 2.7, 3.3, 3.1, 2.4, 2.6, 3, 1.9, 2.8, 2.7, 2.4, 2.5, 2.8, 3.1) att1 <- c(41, 43, 40, 42, 49, 48, 35, 48, 51, 45, 44, 41, 49, 35, 48, 45, 36, 52, 48, 49) grade2 <- c(3.5, 2, 3.2, 3, 2.8, 4, 2.5, 3.2, 3.1, 2.1, 2.5, 2.3, 3.6, 2.5, 4, 2, 3.4, 2.4, 3, 2.9) att2 <- c(67, 46, 51, 56, 46, 62, 48, 57, 54, 47, 45, 41, 59, 48, 61, 44, 48, 42, 53, 47) grade3 <- c(2.8, 3.4, 3.7, 2, 2.8, 3.4, 3.7, 2.6, 2.7, 2.1, 3, 2.8, 2.5, 3.3, 3.7, 2.4, 3.2, 3.2, 3.5, 3.4) att3 <- c(52, 49, 58, 44, 54, 68, 61, 41, 47, 45, 42, 54, 55, 58, 56, 57, 52, 50, 50, 58) classroom <- factor(rep(1:3, each = 20)) dat <- data.frame(classroom, grade = c(grade1, grade2, grade3), att = c(att1, att2, att3)) # 1 pdf("plot1.pdf", width = 5, height = 5) plot(att ~ grade, data = dat, type = "n", xlab = "GPA", ylab = "Attitude toward teachers", main = "Relationship between GPA and \nAttitude Separated by Classes", xlim = c(1.5, 4.0), ylim = c(30, 70)) with(dat[dat$classroom == 1,], points(grade, att, pch = 0, col = "red")) with(dat[dat$classroom == 2,], points(grade, att, pch = 1, col = "green")) with(dat[dat$classroom == 3,], points(grade, att, pch = 2, col = "blue")) with(dat[dat$classroom == 1,], abline(lm(att ~ grade), col = "red", lty = 1)) with(dat[dat$classroom == 2,], abline(lm(att ~ grade), col = "green", lty = 2)) with(dat[dat$classroom == 3,], abline(lm(att ~ grade), col = "blue", lty = 3)) legend("topleft", c("Class 1", "Class 2", "Class 3"), lty = 1:3, pch = 0:2, col = c("red", "green", "blue")) dev.off() # 2 groupgrade <- factor(dat$grade > median(dat$grade), labels = c("lowgpa", "highgpa")) dat <- data.frame(dat, groupgrade) # 3 pdf("plot2.pdf", width = 5, height = 5) boxplot(att ~ classroom + groupgrade, data = dat) dev.off() # 4 pdf("plot3.pdf", width = 5, height = 5) means <- with(dat, tapply(att, list(classroom, groupgrade), mean, na.rm = TRUE)) sds <- with(dat, tapply(att, list(classroom, groupgrade), sd, na.rm = TRUE)) barout <- barplot(means, beside = TRUE, ylim = c(0, 80), legend.text = TRUE, col = c("pink", "darkorange", "skyblue"), args.legend = list(x = 5, y = 80, legend = c("Class 1", "Class 2", "Class 3"))) arrows(barout, means + sds, barout, means - sds, angle = 90, code = 3) abline(h = 0) dev.off()