Course Outline

list Items Test Book

Book
  • Items Test Book

ckcode ⌲ chapter-a4-explaining-variation

require(coursekata) # Create a histogram of Thumb from the Fingers data set gf_histogram(~Thumb, data=Fingers) hist(Fingers$Thumb) ex() %>% check_or( check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, check_function(., "hist") %>% check_arg("x") %>% check_equal() )
CK Code: ch4-1
require(coursekata) # Change the default colors for the different values of the explanatory variable gf_histogram(~ Thumb, data = Fingers, fill = ~ Sex) gf_histogram( ~ Thumb, data = Fingers, fill = ~ Sex) %>% gf_refine(scale_fill_manual(values = c("red","blue"))) msg <- "Did you make sure to use the pipe (`%>%`) to send the output of `gf_histogram` to `gf_refine`?" ex() %>% { check_function(., "gf_histogram") check_function(., "gf_refine") %>% { check_arg(., "object", arg_not_specified_msg = msg) %>% check_equal(incorrect_msg = msg, append = FALSE, eq_fun = function(x, y) all(class(x) == class(y))) check_arg(., "...", arg_not_specified_msg = msg) } check_function(., "scale_fill_manual") %>% check_arg("values") }
CK Code: ch4-2
require(coursekata) # Modify this code to create density histograms gf_histogram(~ Thumb, data = Fingers) %>% gf_facet_grid(Sex ~ .) # Modify this code to create density histograms gf_dhistogram(~ Thumb, data = Fingers) %>% gf_facet_grid(Sex ~ .) ex() %>% { check_function(., "gf_dhistogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() } check_function(., "gf_facet_grid") %>% { check_arg(., 1) %>% check_equal() check_arg(., 2) %>% check_equal() } }
CK Code: ch4-3
require(coursekata) # Write code to create paneled density histograms of Thumb to explore variables that would do a poor job of explaining variation (categorical variables) # Any of these would be marked correct gf_dhistogram(~Thumb, data = Fingers, fill = ~RaceEthnic) %>% gf_facet_grid(RaceEthnic ~ .) gf_dhistogram(~Thumb, data = Fingers, fill = ~Year) %>% gf_facet_grid(Year ~ .) gf_dhistogram(~Thumb, data = Fingers, fill = ~Job) %>% gf_facet_grid(Job ~ .) gf_dhistogram(~Thumb, data = Fingers, fill = ~MathAnxious) %>% gf_facet_grid(MathAnxious ~ .) gf_dhistogram(~Thumb, data = Fingers, fill = ~Interest) %>% gf_facet_grid(Interest ~ .) ex() %>% { check_function(., "gf_dhistogram") %>% { check_arg(., "object") check_arg(., "data") } check_or(., check_function(., "gf_facet_grid", index = 1) %>% check_arg("object"), check_function(., "gf_facet_grid", index = 2) %>% check_arg("object"), check_function(., "gf_facet_grid", index = 3) %>% check_arg("object"), check_function(., "gf_facet_grid", index = 4) %>% check_arg("object"), check_function(., "gf_facet_grid", index = 5) %>% check_arg("object") ) }
CK Code: ch4-4
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(WtLost = ifelse(Wt2 < Wt, "lost", "not lost")) # Write code to make the most appropriate visualization for the outcome variable gf_bar(~WtLost, data = MindsetMatters) ex() %>% check_function("gf_bar") %>% { check_arg(., "object") %>% check_equal(incorrect_msg = "Did you specify the outcome variable `~WtLost`?") check_arg(., "data") %>% check_equal(incorrect_msg = "Did you call `gf_bar()` with the `MindsetMatters` data set?") }
CK Code: ch4-5
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(WtLost = ifelse(Wt2 < Wt, "lost", "not lost")) # Create a bar graph of WtLost, then use gf_facet_grid() to compare the outcome across conditions # Create a bar graph of WtLost, then use gf_facet_grid() to compare the outcome across conditions gf_bar(~ WtLost, data = MindsetMatters) %>% gf_facet_grid(Condition ~ .) ex() %>% { check_or(., check_function(., "gf_bar") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_bar(MindsetMatters, ~ WtLost)") %>% check_function("gf_bar") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() } ) check_function(., "gf_facet_grid") %>% check_arg(2) %>% check_equal() }
CK Code: ch4-6
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(WtLost = ifelse(Wt2 < Wt, "lost", "not lost")) # Play around with gf_jitter gf_jitter(Thumb ~ Sex, data = Fingers) ex() %>% check_function("gf_jitter")
CK Code: ch4-7
require(coursekata) # Modify this boxplot to look at Thumb length by Job gf_boxplot(Thumb ~ Sex, data = Fingers, color = ~Sex) %>% gf_jitter(height = 0, color = "gray", alpha = .5, size = 3) gf_boxplot(Thumb ~ Job, data = Fingers) %>% gf_jitter(height = 0, color = "gray", alpha = .5, size = 3) ex() %>% check_function(., "gf_boxplot") %>% { check_arg(., "data") %>% check_equal() check_arg(., "object") %>% check_equal() }
CK Code: ch4-8
require(coursekata) survey <- data.frame(any1_20 = Survey$Any1_20) # Make a histogram of any1_20 gf_histogram(~ any1_20, data = survey) ex() %>% { check_or(., check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution_code(., 'gf_histogram(~survey$any1_20)') %>% check_function("gf_histogram") %>% check_arg("object") %>% check_equal() ) }
CK Code: ch4-9
require(coursekata) set.seed(13) # create a random sample of 211 numbers between 1 and 20 any1_20 <- # this puts any1_20 into a new data frame called computer computer <- data.frame(any1_20) # make a histogram of any1_20 from computer # create a random sample of 211 numbers between 1 and 20 any1_20 <- resample(1:20, 211) # this puts any1_20 into a new data frame called computer computer <- data.frame(any1_20) # make a histogram of any1_20 from computer gf_histogram(~ any1_20, data = computer, bins = 20) ex() %>% { check_object(., "any1_20") %>% check_equal() check_object(., "computer") %>% check_equal() check_or(., check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution_code(., '{ any1_20 <- resample(1:20, 211) computer <- data.frame(any1_20) gf_histogram(~computer$any1_20) }') %>% check_function("gf_histogram") %>% check_arg("object") %>% check_equal() ) }
CK Code: ch4-10
require(coursekata) set.seed(13) # modify this to generate a random sample of 10,000 any1_20 <- resample(1:20, 211) # modify this to put any1_20 into a new data frame called computer computer <- # this makes a histogram of any1_20 from computer gf_histogram(~ any1_20, data = computer, fill = "dodgerblue", color = "gray", bins = 20) %>% gf_labs(title = "Computer generated random numbers (n = 211)") # modify this to generate a random sample of 10,000 any1_20 <- resample(1:20, 10000) # modify this to put any1_20 into a new data frame called computer computer <- data.frame(any1_20) # this makes a histogram of any1_20 from computer gf_histogram(~any1_20, data = computer, fill = "dodgerblue", color = "gray", bins = 20) %>% gf_labs(title = "Computer generated random numbers (n = 10,000)") check_10000 <- function(x, y) stringr::str_detect(x, "10,?000") ex() %>% { check_object(., "any1_20") check_object(., "computer") check_function(., "gf_labs") %>% check_arg("title") %>% check_equal(eq_fun = check_10000) check_or(., check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution_code(., '{ any1_20 <- resample(1:20, 10000) computer <- data.frame(any1_20) gf_histogram(~computer$any1_20) }') %>% check_function("gf_histogram") %>% check_arg("object") %>% check_equal() ) }
CK Code: ch4-11
require(coursekata) # Use ntile() to cut Height into groups Fingers$Height2Group <- # This prints out the first 6 observations of Height and Height2Group head(select(Fingers, Height, Height2Group)) # Use ntile() to cut Height into groups Fingers$Height2Group <- ntile(Fingers$Height, 2) # This prints out the first 6 observations of Height and Height2Group head(select(Fingers, Height, Height2Group)) ex() %>% check_correct( check_object(., "Fingers") %>% check_column("Height2Group") %>% check_equal(), { check_error(.) check_function(., "ntile", not_called_msg = "Have you called ntile()?") %>% check_arg("n") %>% check_equal() } ) incorrect_msg <- "Did you remember to use select() to select the Height and Height2Group columns from the Fingers data frame before calling head()?" ex() %>% check_or( check_output_expr(., "head(select(Fingers, Height, Height2Group))", missing_msg = incorrect_msg), check_output_expr(., "head(select(Fingers, Height2Group, Height))", missing_msg = incorrect_msg) )
CK Code: ch4-21
require(coursekata) # Here we create the variable Height2Group Fingers$Height2Group <- ntile(Fingers$Height, 2) # Try creating histograms of Thumb in a grid by Height2Group # Here we create the variable Height2Group Fingers$Height2Group <- ntile(Fingers$Height, 2) # Try creating histograms of Thumb in a grid by Height2Group gf_histogram(~Thumb, data=Fingers) %>% gf_facet_grid(Height2Group ~ .) ex() %>% check_or( check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_histogram(Fingers, ~Thumb)") %>% check_function("gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() } ) ex() %>% check_function(., "gf_facet_grid", not_called_msg = "Have you called gf_facet_grid() to put your histograms in a grid?") %>% check_arg(., "object") %>% check_equal() ex() %>% check_or( check_function(., "gf_facet_grid") %>% check_arg(2) %>% check_equal(), override_solution(., "gf_facet_grid(gf_histogram(Fingers, ~Thumb), . ~ Height2Group)") %>% check_function('gf_facet_grid') %>% check_arg(2) %>% check_equal() )
CK Code: ch4-22
require(coursekata) # This code will cut Height from Fingers into 2 categories Fingers$Height2Group <- ntile(Fingers$Height, 2) # Try using factor() to label the groups "short" and "tall" # This code recreates the faceted histogram gf_histogram(~ Thumb, data = Fingers) %>% gf_facet_grid(Height2Group ~ .) Fingers$Height2Group <- ntile(Fingers$Height, 2) Fingers$Height2Group <- factor(Fingers$Height2Group, levels = c(1,2), labels = c("short", "tall")) gf_histogram(~ Thumb, data = Fingers) %>% gf_facet_grid(Height2Group ~ .) ex() %>% check_correct( check_object(., "Fingers") %>% check_column("Height2Group") %>% check_equal(incorrect_msg = "Did you remember to save the factored variable back into Fingers$Height2Group?"), { check_error(.) check_function(., "factor") %>% check_arg("levels") %>% check_equal() check_function(., "factor") %>% check_arg("labels") %>% check_equal() } ) success_msg("Wow! You're a rock staR. Keep up the good work!")
CK Code: ch4-23
require(coursekata) Fingers <- Fingers %>% mutate(Height2Group = factor(ntile(Height, 2), 1:2, c("short", "tall"))) # Create a scatterplot of Thumb by Height2Group # Create boxplots of Thumb by Height2Group # Create a scatterplot of Thumb by Height2Group gf_point(Thumb ~ Height2Group, data = Fingers) # Create boxplots of Thumb by Height2Group gf_boxplot(Thumb ~ Height2Group, data = Fingers) ex() %>% { check_or(., check_function(., "gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_point(Fingers, Thumb ~ Height2Group)") %>% check_function("gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_point(Fingers$Thumb ~ Fingers$Height2Group)") %>% check_function("gf_point") %>% check_arg("object") %>% check_equal(), override_solution(., "gf_jitter(Thumb ~ Height2Group, data = Fingers)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_jitter(Fingers, Thumb ~ Height2Group)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_jitter(Fingers$Thumb ~ Fingers$Height2Group)") %>% check_function("gf_jitter") %>% check_arg("object") %>% check_equal() ) check_or(., check_function(., "gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_boxplot(Fingers, Thumb ~ Height2Group)") %>% check_function("gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_boxplot(Fingers$Thumb ~ Fingers$Height2Group)") %>% check_function("gf_boxplot") %>% check_arg("object") %>% check_equal() ) }
CK Code: ch4-14
require(coursekata) Fingers <- Fingers %>% mutate(Height2Group = factor(ntile(Height, 2), 1:2, c("short", "tall"))) # Modify this code to break Height into 3 categories: "short", "medium", and "tall" Fingers$Height3Group <- ntile(Fingers$Height, 2) Fingers$Height3Group <- factor( , levels = 1:2, labels = c("short", "tall")) # Create a scatterplot of Thumb by Height3Group # Create boxplots of Thumb by Height3Group Fingers$Height3Group <- ntile(Fingers$Height, 3) Fingers$Height3Group <- factor(Fingers$Height3Group, 1:3, c("short", "medium", "tall")) gf_point(Thumb ~ Height3Group, data = Fingers) gf_boxplot(Thumb ~ Height3Group, data = Fingers) ex() %>% { check_object(., "Fingers") %>% check_column("Height3Group") %>% check_equal(incorrect_msg = "Did you remember to use `ntile()`?") } ex() %>% check_or( check_function(., "gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_point(Fingers, Thumb ~ Height3Group)") %>% check_function("gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_point(Fingers$Thumb ~ Fingers$Height3Group)") %>% check_function("gf_point") %>% { check_arg(., "object") %>% check_equal() }, override_solution(., "gf_jitter(Thumb ~ Height3Group, data = Fingers)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_jitter(Fingers, Thumb ~ Height3Group)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_jitter(Fingers$Thumb ~ Fingers$Height3Group)") %>% check_function("gf_jitter") %>% check_arg("object") %>% check_equal() ) ex() %>% check_or( check_function(., "gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_boxplot(Fingers, Thumb ~ Height3Group)") %>% check_function("gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_boxplot(Fingers$Thumb ~ Fingers$Height3Group)") %>% check_function("gf_boxplot") %>% check_arg("object") %>% check_equal() ) success_msg("Keep it up!")
CK Code: ch4-15
require(coursekata) Fingers <- Fingers %>% mutate(Height2Group = factor(ntile(Height, 2), 1:2, c("short", "tall"))) # create a scatterplot of Thumb by Height # create a scatterplot of Thumb by Height gf_point(Thumb ~ Height, data = Fingers) ex() %>% check_or( check_function(., "gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_point(Fingers, Thumb ~ Height)") %>% check_function("gf_point") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_point(Fingers$Thumb ~ Fingers$Height)") %>% check_function("gf_point") %>% check_arg("object") %>% check_equal(), override_solution(., "gf_jitter(Thumb ~ Height, data = Fingers)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_jitter(Fingers, Thumb ~ Height)") %>% check_function("gf_jitter") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_jitter(Fingers$Thumb ~ Fingers$Height)") %>% check_function("gf_jitter") %>% check_arg("object") %>% check_equal() )
CK Code: ch4-16
require(coursekata) # complete the jitter plot gf_jitter( ~ , data = , width = .1) %>% gf_model(Tip ~ Condition, color = "red") # complete the jitter plot gf_jitter(Tip ~ Condition, data = TipExperiment, width = .1) %>% gf_model(Tip ~ Condition, color = "red") ex() %>% { check_function(., "gf_jitter") %>% check_arg("data") %>% check_equal() check_or(., check_function(., "gf_jitter") %>% check_arg("object") %>% check_equal(), override_solution(., "gf_jitter(Condition ~ Tip, data = TipExperiment)") %>% check_function(., "gf_jitter") %>% check_arg("object") %>% check_equal() ) }
CK Code: A4_Code_Randomness_01
require(coursekata) # shuffle the tips in the jitter plot gf_jitter(Tip ~ Condition, data = TipExperiment, width = .1) %>% gf_labs(title = "Shuffled Data") # shuffle the tips in the jitter plot gf_jitter(shuffle(Tip) ~ Condition, data = TipExperiment, width = .1) %>% gf_labs(title = "Shuffled Data") ex() %>% check_or( check_function(., "gf_jitter") %>% check_arg("object") %>% check_equal(), override_solution(., "gf_jitter(shuffle(Tip) ~ shuffle(Condition), data = TipExperiment)") %>% check_function("gf_jitter") %>% check_arg("object") %>% check_equal(), override_solution(., "gf_jitter(Tip ~ shuffle(Condition), data = TipExperiment)") %>% check_function("gf_jitter") %>% check_arg("object") %>% check_equal() )
CK Code: A4_Code_Shuffling_01
require(coursekata) # add the shuffle function to this code TipExperiment$ShuffTip <- TipExperiment$Tip # this makes a jitter plot of the shuffled data and adds means for both groups gf_jitter(ShuffTip ~ Condition, data = TipExperiment, width = .1) %>% gf_labs(title = "Shuffled Data") %>% gf_model(ShuffTip ~ Condition, color = "orchid") # add the shuffle function to this code TipExperiment$ShuffTip <- shuffle(TipExperiment$Tip) # this makes a jitter plot of the shuffled data and adds means for both groups gf_jitter(ShuffTip ~ Condition, data = TipExperiment, width = .1) %>% gf_labs(title = "Shuffled Data") %>% gf_model(ShuffTip ~ Condition, color = "orchid") ex() %>% { check_function(., "shuffle") %>% check_arg(1) %>% check_equal() check_function(., "gf_jitter") %>% check_arg(1) %>% check_equal() }
CK Code: A4_Code_Shuffling_02
require(coursekata) # run your code here
CK Code: A4_Code_Review2_01
require(coursekata) # run your code here
CK Code: A4_Code_Review2_02
require(coursekata) # run your code here
CK Code: A4_Code_Review2_03
require(coursekata) # run your code here
CK Code: A4_Code_Review2_04
require(coursekata) # run your code here
CK Code: A4_Code_Review2_05
require(coursekata) # run your code here
CK Code: A4_Code_Review2_06
require(coursekata) # run your code here
CK Code: A4_Code_Review2_07
require(coursekata) # run your code here
CK Code: A4_Code_Review2_08
require(coursekata) # run your code here
CK Code: A4_Code_Review2_09

Responses