1.A sample of 40 psychology students at a large southwestern university took four subtests (Vocabulary, Similarities, Block Design, and Picture Completion) of the Wechsler (1981) Adult Intelligence Scale-Revised. The researchers also used Magnetic Resonance Imaging (MRI) to determine the brain size of the subjects.
bra <- read.table("data/brainsize.txt",h=T)
head(bra)
Sbj Gender FSIQ VIQ PIQ Weight Height MRICount
1 1 Female 133 132 124 118 64.5 816932
2 2 Male 140 150 124 NA 72.5 1001121
3 3 Male 139 123 150 143 73.3 1038437
4 4 Male 133 129 128 172 68.8 965353
5 5 Female 137 132 134 147 65.0 951545
6 6 Female 99 90 110 146 69.0 928799
1.Are there gender differences in the three IQ scores?
library("lattice")
densityplot(~ FSIQ+VIQ+PIQ, data = bra, groups = Gender, plot.points = FALSE,
ref = TRUE,auto.key = list(space = "right"),
layout=c(3,1))
2.Is the relationship between height and weight gender dependent?
xyplot(Height ~ Weight, group=Gender, data = bra,auto.key = list(column = 2),
type = c("g","r","p"), par.settings = simpleTheme(col = c("red", "darkblue"),
pch = 20, col.line = c("red", "darkblue") ))
3.Is the relationship between IQ and brainsize (as measured by MRIcount) gender dependent?
xyplot(MRICount~FSIQ+VIQ+PIQ , group=Gender, data = bra,auto.key = list(column = 2),
type = c("g","r","p"), par.settings = simpleTheme(col = c("red", "darkblue"),
pch = 20, col.line = c("red", "darkblue") ),layout=c(3,1))
2.The heights of 26 boys in Oxford, UK, were measured on nine occasions over two years, starting at 12 years old. Draw the graph below.
boyheight <- read.table("data/boyHeights.txt",h=T)
head(boyheight)
sbj age ht occasion
1 1 -1.0000 140.5 1
2 1 -0.7479 143.4 2
3 1 -0.4630 144.8 3
4 1 -0.1643 147.1 4
5 1 -0.0027 147.7 5
6 1 0.2466 150.2 6
boyheight$age <- boyheight$age+13
xyplot(ht ~ age, group=sbj ,data=boyheight,
xlab="Age(in year)",ylab="Height(in cm)",
type="b",lty=2,lwd=0.5,cex=1.2)
3.The data set concerns student evaluation of instructor’s beauty and teaching quality for several courses at the University of Texas. The teaching evaluatons were done at the end of the semester, and the beauty judgments were made later, by six students who had not attended the classes and were not aware of the course evaluations.
bc <- read.table("data/beautyCourseEval.txt",h=T)
head(bc)
eval beauty sex age minority tenure courseID
1 4.3 0.2015666 1 36 1 0 3
2 4.5 -0.8260813 0 59 0 1 0
3 3.7 -0.6603327 0 51 0 1 4
4 4.3 -0.7663125 1 40 0 1 2
5 4.4 1.4214450 1 31 0 0 0
6 4.2 0.5002196 0 62 0 1 0
xyplot(eval~beauty,data=bc,xlab="Beauty score",ylab="Average teaching evaluation",
panel=function(x,y){
panel.xyplot(x, y)
panel.lmline(x, y)
},
par.settings = standard.theme(color = F)
)
xyplot(eval~beauty|as.factor(courseID),data=bc
,xlab="Beauty judgement score",ylab="Average course evaluation score",
panel=function(x,y){
panel.grid()
panel.xyplot(x, y)
panel.lmline(x, y)
},
layout = c(6, 6)
)
4.Use trellis graphics to explore various ways to display the sample data from the National Longitudinal Survey of Youth.
NLSY <- read.csv("data/nlsy86long.csv",h=T)
NLSY$grade <- factor(NLSY$grade,levels=c(0,1,2),
labels=c("Kindergarten", "First grade", "Second grade" ))
NLSY$time <- factor(NLSY$time,levels=c(1:4),
labels=c("First", "Second","Third","Fourth" ))
head(NLSY)
id sex race time grade year month math read
1 2390 Female Majority First Kindergarten 6 67 14.285714 19.047619
2 2560 Female Majority First Kindergarten 6 66 20.238095 21.428571
3 3740 Female Majority First Kindergarten 6 67 17.857143 21.428571
4 4020 Male Majority First Kindergarten 5 60 7.142857 7.142857
5 6350 Male Majority First First grade 7 78 29.761905 30.952381
6 7030 Male Majority First Kindergarten 5 62 14.285714 17.857143
str(NLSY)
'data.frame': 664 obs. of 9 variables:
$ id : int 2390 2560 3740 4020 6350 7030 7200 7610 7680 7700 ...
$ sex : Factor w/ 2 levels "Female","Male": 1 1 1 2 2 2 2 2 1 2 ...
$ race : Factor w/ 2 levels "Majority","Minority": 1 1 1 1 1 1 1 1 1 1 ...
$ time : Factor w/ 4 levels "First","Second",..: 1 1 1 1 1 1 1 1 1 1 ...
$ grade: Factor w/ 3 levels "Kindergarten",..: 1 1 1 1 2 1 1 1 1 1 ...
$ year : int 6 6 6 5 7 5 6 7 6 6 ...
$ month: int 67 66 67 60 78 62 66 79 76 67 ...
$ math : num 14.29 20.24 17.86 7.14 29.76 ...
$ read : num 19.05 21.43 21.43 7.14 30.95 ...
# Box and whisker plot
NLSY$race <- with(NLSY, reorder(race, math, median))
bwplot(math ~ race | time, data=NLSY, ylab="Math score",layout=c(4,1))
bwplot(read ~ race | time, data=NLSY, ylab="Read score",layout=c(4,1))
# qqmath
qqmath( ~ math | time+sex, data = NLSY, groups = race,
xlab = " Normal quantile", ylab = "Math score",
auto.key = list(columns = 2), type = c("p", "g", "r"),
aspect = "xy",layout=c(4,2))
qqmath( ~ read | time+sex, data = NLSY, groups = race,
xlab = " Normal quantile", ylab = "Read score",
auto.key = list(columns = 2), type = c("p", "g", "r"),
aspect = "xy",layout=c(4,2) )
# xyplot
xyplot(read ~ math | race+sex, groups = time, data = NLSY,
xlab = "Math score", ylab = "Read score",
type = c("p", "r", "g"),
auto.key = list(column = 4),
par.settings = simpleTheme(col = c("deeppink", "deepskyblue","orange","limegreen"),pch = 20,
col.line = c("deeppink", "deepskyblue","orange","limegreen") )
,layout=c(4,1))
# Dot plot
m0 <- aggregate(math ~ race+sex+time, data=NLSY, mean)
dotplot(math ~ time |race, group=sex ,data = m0 ,type="b",
xlab = "Time", ylab = "Mean math score", cex = 1.2, lwd = 1.2,
auto.key = list(points = T, lines = T, columns = 2) )
m1 <- aggregate(read ~ race+sex+time, data=NLSY, mean)
dotplot(read ~ time |race, group=sex ,data = m1, type="b",
xlab = "Time", ylab = "Mean read score", cex = 1.2, lwd = 1.2,
auto.key = list(points = T, lines = T, columns = 2) )