library(learnr)
library(tidyverse)
library(tutorialExtras)
library(gradethis)

#for exercises
library(ISLR)

credit_ch6 <- Credit %>%
  as_tibble() %>%
  select(debt = Balance, 
         credit_limit = Limit,
         income = Income, 
         credit_rating = Rating,
         age = Age)

gradethis_setup()
knitr::opts_chunk$set(echo = FALSE)
grade_server("grade")
question_text("Name:",
              answer_fn(function(value){
                              if(length(value) >= 1 ) {
                                return(mark_as(TRUE))
                                }
                              return(mark_as(FALSE) )
                              }),
              correct = "submitted",
              allow_retry = FALSE )
grade_button_ui(id = "grade")

Instructions

Complete this tutorial while reading Sections 6.0 - 6.1 of the textbook. Each question allows 3 'free' attempts. After the third attempt a 10% deduction occurs per attempt.

You can click the "View Grade" button as many times as you would like to see your current grade and the number of attempts you are on. Before submitting make sure your grade is as expected.

Goals

Exercises

Exercise 1:

When creating the data frame credit_ch6, we renamed several variables as we selected them. First, fill in the blanks of the code that accomplished this.

library(___)

credit_ch6 ___ Credit ___ 
  as_tibble() %>%
  select(debt = Balance, 
         ___ = Limit,
         ___ = Income, 
         credit_rating = ___ ,
         age = Age)
library(ISLR)

credit_ch6 <- Credit %>%
  as_tibble() %>%
  select(debt = Balance, 
         credit_limit = Limit,
         income = Income, 
         credit_rating = Rating,
         age = Age)
grade_this_code()

Exercise 2:

Fill in the blanks for the code that creates the following plot.

htmltools::img(src="images/Figure_06_1.png", height = 470, width = 410)
ggplot(credit_ch6, aes(x = ___, y = ___)) +
  geom____() +
  ___(x = ___, y = ___, ___ = "Debt and income") + 
  ___(method = ___, se = FALSE)
Part 2 Hint: Remember to include labels for the axes
ggplot(credit_ch6, aes(x = income, y = debt)) +
  geom_point() +
  labs(x = "Income (in $1000)", y = "Credit card debt (in $)",
  title = "Debt and income") + 
  geom_smooth(method = "lm", se = FALSE)
grade_this_code()

Reading Quiz

quiz(
  caption = NULL,
  # Q1
  question_numeric("Q1) What is the value for the largest amount of debt in the credit_ch6 data set?", 
           answer(1999, correct = TRUE),
           allow_retry = TRUE),
  # Q2
  question_numeric("Q2) Consider the following model:\n
    debt_model <- lm(debt ~ credit_limit + income, data = credit_ch6)\n
  How many quantities does this model estimate? That is, how many values will there be in the Estimate column of the regression table for this model? ", 
           answer(3, correct = TRUE),
           allow_retry = TRUE),
  # Q3
  question_numeric("Q3) Consider the following model:\n
    debt_model <- lm(debt ~ credit_limit + income, data = credit_ch6)\n
  What is the estimated slope of credit_limit that results from this model? Round to 3 decimal places.", 
           answer(0.264, correct = TRUE),
           step = 0.001,
           allow_retry = TRUE),
  # Q4
  question("Q4) What is the name of the phenomenon where relationships that exist in aggregate disappear or reverse when the data are broken into groups?", 
           type = "learnr_text",
           answer_fn(function(value){
            if (str_remove_all(str_to_lower(value), " ") %in% 
                c("simpson'sparadox", "simpsonparadox", "simpsonsparadox")) {
                 return(mark_as(TRUE))}
                 return(mark_as(FALSE) ) }),
           allow_retry = TRUE),
  # Q5
  question("Q5) What is the name of the function that provides you with the fitted values for a model?", 
           type = "learnr_text",
           answer_fn(function(value){
            if (str_remove_all(str_to_lower(value), " ") %in% 
                c("fitted", "fitted()", "fitted(model)", "fitted(debt_model")) {
                 return(mark_as(TRUE))}
                 return(mark_as(FALSE) ) }),
           allow_retry = TRUE),
  question("Q6) What is the name of the function that provides you with the residuals for a model?", 
           type = "learnr_text",
           answer_fn(function(value){
            if (str_remove_all(str_to_lower(value), " ") %in% 
                c("residuals", "residuals()", "residuals(model)", "residuals(debt_model")) {
                 return(mark_as(TRUE))}
                 return(mark_as(FALSE) ) }),
           allow_retry = TRUE),
  # Q7
  question_numeric("Q7) How many columns are in debt_model_data?", 
           answer(6, correct = TRUE),
           allow_retry = TRUE),
  # Q8
  question_numeric("Q8) How many rows are in debt_model_data?", 
           answer(400, correct = TRUE),
           allow_retry = TRUE)
)

Submit

Once you are finished:

grade_print_ui("grade")


NUstat/ISDStutorials documentation built on April 17, 2025, 6:15 p.m.