Draw a scatter plot of the variables written and course scores from the data set Gcsemv{mlmRev} and superimpose another scatter plot on it using mean written and mean course scores by school as in the language and math example. You can start by editing in RStudio either the gcsemv rmarkdown or the exam rmarkdown file.
In this section, we load the exam scores data set, activate the help page for it, and examine the first 6 lines of the data frame object.
# install a contributed package
#install.packages("mlmRev")
# load the package to working directory
library(mlmRev)
# load the data set Gcsemv{mlmRev}
data(Gcsemv, package="mlmRev")
# invoke help document
?Gcsemv
# view first 6 lines
knitr::kable(head(Gcsemv))
school | student | gender | written | course |
---|---|---|---|---|
20920 | 16 | M | 23 | NA |
20920 | 25 | F | NA | 71.2 |
20920 | 27 | F | 39 | 76.8 |
20920 | 31 | F | 36 | 87.9 |
20920 | 42 | M | 16 | 44.4 |
20920 | 62 | F | 36 | NA |
We first compute the correlation coefficient between written scores and course scores using all students in the data set. We then compute the mean written scores and mean course scores by school. The correlation coefficient between mean school witten scores and mean school course scores is then calculated.
with(Gcsemv, cor(written,course,use="pairwise"))
[1] 0.47417
# compute the means by school
course_schmean <- with(Gcsemv, tapply(course, school, mean, na.rm=T))
written_schmean <- with(Gcsemv, tapply(written, school , mean, na.rm=T))
cor(course_schmean, written_schmean)
[1] 0.39568
We draw a scatter diagram of the exam scores against the LRT scores and add the regression line. Next we superimpose on the scatter plot the mean school exam scores and mean school LRT scores (in color cyan) and add the regression line based on the mean school scores.
with(Gcsemv, plot(course ~ written,
bty = 'n',
cex = 0.5,
xlab = 'written score',
ylab = 'course score'))
grid()
with(Gcsemv, abline(lm(course ~ written)))
points(course_schmean, written_schmean, pch=16, col=5)
abline(lm(course_schmean ~ written_schmean), col=5)
(上圖錯誤,待修正)