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

Conceptual Questions

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"))
     )

More visualization

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

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).

Exercises

## 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)

Scatter plots

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.

Exercises

## 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.")

Plotting two sets of points

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.

Exercises

## 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)))
)

Finalizing your scatter plot

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.

Exercises

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")

Submit

submission_ui
submission_server()


mattblackwell/qsslearnr documentation built on Sept. 17, 2022, 6:25 p.m.