library(gradethis) library(learnr) library(qsslearnr) tutorial_options(exercise.checker = gradethis::grade_learnr) knitr::opts_chunk$set(echo = FALSE) tut_reptitle <- "QSS Tutorial 4: Output Report" data(STAR, package = "qss") star <- STAR
quiz(caption = "", question("In section 3.6 of QSS, what kind of plot are we using to visualize ideological differences between Democrats and Republicans?", answer("scatterplot", correct = TRUE), answer("bar plot"), answer("histogram")), question("To estimate the correlation between two variables x and y, we need to know the mean of each as well as the:", answer("maximum"), answer("standard deviation", correct = TRUE), answer("length")), question("What type of relationships does correlation measure?", answer("linear relationships", correct = TRUE), answer("nonlinear relationships"), answer("both")) )
We'll continue to analyze data from the STAR project, which is a four-year randomized trial on the effectiveness of small class sizes on education performance. The star
data frame as been loaded into your space so that you can play around with it a bit.
Boxplots are useful tools to visualize how the distribution of a continuous variable changes across levels of a categorical variable. There are several ways to specify a boxplot, but one of the most useful is with what R calls a formula:
boxplot(numvar ~ catvar, data = mydata)
Here, the first argument are the two variables of interest separated by the tilde character ~
. On the left hand side is the numerical variable that you are investigating and on the right hand side is the categorical variable we want to subset by. The data
argument tells the function where the function can find these variables (that is, in what data frame are these variables).
boxplot
function using the g4math
is the numerical variable and classtype
is the categorical variable. Pass the star
data frame to the data
argument. Include an informative y-axis label with ylab
argument. Finally, pass the classnames
vector to the names
argument to place labels on the boxplots.## labels for the class type classnames <- c("Small class", "Regular class", "Regular class with aid") ## create a box plot with the characteristics specified in the instructions
classnames <- c("Small class", "Regular class", "Regular class with aid") boxplot(g4math ~ classtype, data = star, ylab = "Math scores", names = classnames)
Now you'll think more about to measure bivariate relationships---that is, the relationship between two variables. The plot(x,y)
function will take two vectors and plot a series of points on a two-dimensional grid where the x-coordinates come from the x
vector and the y-coordinates come from the y
vector. For instance, plot(x = c(1,2), y = c(3,4))
will plot two points, one at (1,3) and the other at (2,4). You will use this plot to explore the relationship between math and reading test scores in the star
data.
plot
function with g4math
on the x-axis and g4reading
on the y-axis, both from the star
data.$
symbol.## produce a scatterplot of g4math on the x-axis and g4reading on the y-axis
## produce a scatterplot of g4math on the x-axis and g4reading on the y-axis plot(x = star$g4math, y = star$g4reading)
grade_code("Ok, great plot. Let's make a bit more polished.")
Often we want to plot certain pints differently than others. For instance, maybe you want to see how the relationship between math and reading scores differs between students in small classes versus those not in small classes. To do this, we can use subsets and the points
function.
After using the plot
function, you can add more points to the current plot by using the points(x,y)
function, where the points are plotted very similarly to plot
.
star
data called small.class
which is when classtype
is equal to 1. Create a second subset called reg.class
which is when classtype
is not equal to 1.plot
function to create a scatter plot of g4math
and g4reading
from the small.class
. Change the color of the points by including the col = "indianred"
argument.points
function to add the points of g4math
and g4reading
from the reg.class
. Change the color of the points by including the col = "dodgerblue"
argument.x == y
to test if x
is equal to y
and x != y
to test if x
is not equal to y
## create a subset for small classes called small.class (classtype is 1) ## create a subset for regular classes called reg.class (classtype is not 1) ## use the plot command for the small.class and use one color ## use the points command for the reg.class and use another color
## create a subset for small classes (classtype is 1) small.class <- subset(star, classtype == 1) ## create a subset for regular classes (classtype is not 1) reg.class <- subset(star, classtype != 1) ## use the plot command for the small.class and use one color plot(small.class$g4math, small.class$g4reading, col = "indianred") ## use the points command for the reg.class and use another color points(reg.class$g4math, reg.class$g4reading, col = "dodgerblue")
grade_result_strict( pass_if(~ identical(small.class, subset(star, classtype == 1))), pass_if(~ identical(reg.class, subset(star, classtype != 1))) )
The scatter plot is looking very good, but it could use a little bit of polish. Let's add axis labels and a title. Remember that you can add these to the plot via the xlab
, ylab
, and main
. Also, it's important to note that you can only add these to the initial plot
function; adding them to the points
or text
or abline
functions won't have any effect.
xlab
, ylab
, and main
. Make them informative.small.class <- subset(star, classtype == 1) ## create a subset for regular classes (classtype is not 1) reg.class <- subset(star, classtype != 1) ## add the arguments to make the plot more readable plot(small.class$g4math, small.class$g4reading, col = "indianred") points(reg.class$g4math, reg.class$g4reading, col = "dodgerblue")
small.class <- subset(star, classtype == 1) ## create a subset for regular classes (classtype is not 1) reg.class <- subset(star, classtype != 1) ## add the arguments to make the plot more readable plot(small.class$g4math, small.class$g4reading, col = "indianred", xlab = "Fourth Grad Math Scores", ylab = "Fourth Grade Reading Score", main = "Math vs Reading") points(reg.class$g4math, reg.class$g4reading, col = "dodgerblue")
submission_ui
submission_server()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.