0413 Trellis graphics in class exercise
in class exercise 2
Create a new student-teacher ratio variable from the enrltot and teachers variables in the data set Caschool{Ecdat} to generate the following plot in which reading scores (readscr) for grade span assignment grspan equals “KK-08” in the data set are split into three levels: lower-third, middle-third, and upper-third
loading data and check data structure
distcod county district grspan enrltot teachers
1 75119 Alameda Sunol Glen Unified KK-08 195 10.90
2 61499 Butte Manzanita Elementary KK-08 240 11.15
3 61549 Butte Thermalito Union Elementary KK-08 1550 82.90
4 61457 Butte Golden Feather Union Elementary KK-08 243 14.00
calwpct mealpct computer testscr compstu expnstu str avginc elpct
1 0.5102 2.0408 67 690.8 0.3435898 6384.911 17.88991 22.690 0.000000
2 15.4167 47.9167 101 661.2 0.4208333 5099.381 21.52466 9.824 4.583333
3 55.0323 76.3226 169 643.6 0.1090323 5501.955 18.69723 8.978 30.000002
4 36.4754 77.0492 85 647.7 0.3497942 7101.831 17.35714 8.978 0.000000
readscr mathscr
1 691.6 690.0
2 660.5 661.9
3 636.3 650.9
4 651.9 643.5
[ reached 'max' / getOption("max.print") -- omitted 2 rows ]
'data.frame': 420 obs. of 17 variables:
$ distcod : int 75119 61499 61549 61457 61523 62042 68536 63834 62331 67306 ...
$ county : Factor w/ 45 levels "Alameda","Butte",..: 1 2 2 2 2 6 29 11 6 25 ...
$ district: Factor w/ 409 levels "Ackerman Elementary",..: 362 214 367 132 270 53 152 383 263 94 ...
$ grspan : Factor w/ 2 levels "KK-06","KK-08": 2 2 2 2 2 2 2 2 2 1 ...
$ enrltot : int 195 240 1550 243 1335 137 195 888 379 2247 ...
$ teachers: num 10.9 11.1 82.9 14 71.5 ...
$ calwpct : num 0.51 15.42 55.03 36.48 33.11 ...
$ mealpct : num 2.04 47.92 76.32 77.05 78.43 ...
$ computer: int 67 101 169 85 171 25 28 66 35 0 ...
$ testscr : num 691 661 644 648 641 ...
$ compstu : num 0.344 0.421 0.109 0.35 0.128 ...
$ expnstu : num 6385 5099 5502 7102 5236 ...
$ str : num 17.9 21.5 18.7 17.4 18.7 ...
$ avginc : num 22.69 9.82 8.98 8.98 9.08 ...
$ elpct : num 0 4.58 30 0 13.86 ...
$ readscr : num 692 660 636 652 642 ...
$ mathscr : num 690 662 651 644 640 ...
data manipulation
dta$st.ratio<-dta$enrltot/dta$teachers # create new variable
# find the 1/3, 1/3, 1/3 cutpoint
library(dplyr)
dta$level <- with(dta, factor(findInterval(readscr, c(-Inf,
quantile(readscr, probs=c(1/3, 2/3)), Inf)),
labels=c("L","M","H")))plot
library(lattice)
dta.2<-dta%>%filter(grspan=="KK-08") # filter graspan="KK-08"
xyplot(readscr~st.ratio|level, data=dta.2,
xlab="Student-Teacher ratio", ylab="Reading score", type=c("p", "g", "r"),
layout=c(3,1), pch=1)in class exercise 3
loading data and check data structure
dta<-read.table("C:/Users/USER/Desktop/R_data management/0413/beautyCourseEval.txt", sep="", header=T)
head(dta) 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
plot
# reoder by coefficient
library(nlme)
m<-lmList(eval~beauty| courseID, data = dta)
m1<-data.frame(courseID=rownames(coef(m)),coef(m),check.names=FALSE)
m2<-m1[with(m1,order(beauty, decreasing=F)),]
m.order<-list(m2$courseID)
xyplot(eval~beauty|courseID, data=dta1, type=c("p", "g", "r"), layout=c(6,6), index.cond=m.order, xlab="Beauty judgment score", ylab="Average course evaluation score")in class exercise 4
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. Source: Willerman, L., Schultz, R., Rutledge, J.N., & Bigler, E. (1991), In Vivo Brain Size and Intelligence, Intelligence, 15, 223-228.
Use appropriate lattice graphics to answer the following questions.
1.Are there gender differences in the three IQ scores?
2.Is the relationship between height and weight gender dependent?
3.Is the relationship between IQ and brainsize (as measured by MRIcount) gender dependent?
loading data and check data structure
dta<-read.table("C:/Users/USER/Desktop/R_data management/0413/brainsize.txt", sep="", header=T)
head(dta) 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)
library(reshape)
dta.melt<-melt(dta, id=c("Sbj", "Gender", "Weight", "Height", "MRICount"))
bwplot(value ~ Gender|variable,
data=dta.melt,
par.settings=standard.theme(color=FALSE), xlab="Gender", ylab="IQ score")$FSIQ
Welch Two Sample t-test
data: x by dta$Gender
t = -0.40267, df = 37.892, p-value = 0.6895
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-18.68639 12.48639
sample estimates:
mean in group Female mean in group Male
111.9 115.0
$VIQ
Welch Two Sample t-test
data: x by dta$Gender
t = -0.77262, df = 36.973, p-value = 0.4447
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-21.010922 9.410922
sample estimates:
mean in group Female mean in group Male
109.45 115.25
$PIQ
Welch Two Sample t-test
data: x by dta$Gender
t = -0.1598, df = 37.815, p-value = 0.8739
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-15.72079 13.42079
sample estimates:
mean in group Female mean in group Male
110.45 111.60
Three IQ scores did not have significant difference in gender
2.Is the relationship between height and weight gender dependent?
$Weight
Call:
lm(formula = x ~ Gender, data = dta, na.action = na.omit)
Residuals:
Min 1Q Median 3Q Max
-34.444 -15.383 3.678 13.306 37.800
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 137.200 4.132 33.203 < 2e-16 ***
GenderMale 29.244 6.004 4.871 2.23e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 18.48 on 36 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.3972, Adjusted R-squared: 0.3805
F-statistic: 23.73 on 1 and 36 DF, p-value: 2.227e-05
$Height
Call:
lm(formula = x ~ Gender, data = dta, na.action = na.omit)
Residuals:
Min 1Q Median 3Q Max
-5.132 -2.432 0.235 2.152 5.568
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 65.7650 0.6298 104.42 < 2e-16 ***
GenderMale 5.6666 0.9023 6.28 2.62e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.816 on 37 degrees of freedom
(1 observation deleted due to missingness)
Multiple R-squared: 0.516, Adjusted R-squared: 0.5029
F-statistic: 39.44 on 1 and 37 DF, p-value: 2.624e-07
Weight and height both have significant difference in gender
3.Is the relationship between IQ and brainsize (as measured by MRIcount) gender dependent?
$FSIQ
Call:
lm(formula = MRICount + x ~ Gender, data = dta)
Residuals:
Min 1Q Median 3Q Max
-74893 -34603 -7286 19996 128676
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 862767 12502 69.008 < 2e-16 ***
GenderMale 92204 17681 5.215 6.77e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 55910 on 38 degrees of freedom
Multiple R-squared: 0.4171, Adjusted R-squared: 0.4018
F-statistic: 27.19 on 1 and 38 DF, p-value: 6.774e-06
$VIQ
Call:
lm(formula = MRICount + x ~ Gender, data = dta)
Residuals:
Min 1Q Median 3Q Max
-74888 -34605 -7294 20001 128677
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 862764 12502 69.010 < 2e-16 ***
GenderMale 92207 17681 5.215 6.77e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 55910 on 38 degrees of freedom
Multiple R-squared: 0.4172, Adjusted R-squared: 0.4018
F-statistic: 27.2 on 1 and 38 DF, p-value: 6.767e-06
$PIQ
Call:
lm(formula = MRICount + x ~ Gender, data = dta)
Residuals:
Min 1Q Median 3Q Max
-74894 -34596 -7278 19994 128671
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 862765 12503 69.007 < 2e-16 ***
GenderMale 92202 17681 5.215 6.78e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 55910 on 38 degrees of freedom
Multiple R-squared: 0.4171, Adjusted R-squared: 0.4018
F-statistic: 27.19 on 1 and 38 DF, p-value: 6.778e-06
IQ and brainsize are gender dependent