Course Outline

list Items Test Book

Book
  • Items Test Book

ckcode ⌲ chapter-a3-examining-distributions

require(coursekata) # try running this code gf_histogram(~Thumb, data = Fingers) # try running this code gf_histogram(~Thumb, data = Fingers) ex() %>% check_function("gf_histogram") %>% { check_arg(., "object", arg_not_specified_msg = "Make sure to keep ~Thumb") %>% check_equal() check_arg(., "data", arg_not_specified_msg = "Make sure to specify data") %>% check_equal() }
CK Code: ch3-1
require(coursekata) # Modify this code to play around with labeling the y-axis gf_histogram(~ Thumb, data = Fingers) %>% gf_labs(x = "Thumb length (mm)", y = ) gf_histogram(~ Thumb, data = Fingers) %>% gf_labs(x = "Thumb length (mm)", y = "Your Label") ex() %>% { check_function(., "gf_labs") %>% check_arg("x") %>% check_equal(eval = FALSE) check_function(., "gf_labs") %>% check_arg("y") check_function(., "gf_histogram") %>% check_arg("object") %>% check_equal() check_function(., "gf_histogram") %>% check_arg("data") %>% check_equal() }
CK Code: ch3-2
require(coursekata) # This sets up our tiny data frame with our outcome variable outcome <- c(1, 2, 3, 4, 5) tiny_data <- data.frame(outcome) # Write code to create a histogram of outcome outcome <- c(1, 2, 3, 4, 5) tiny_data <- data.frame(outcome) gf_histogram(~outcome, data = tiny_data, fill = "aquamarine", color = "gray") ex() %>% { check_object(., "outcome", undefined_msg = "Make sure to not remove `outcome`") %>% check_equal() check_object(., "tiny_data") %>% check_column("outcome") %>% check_equal(incorrect_msg = "Make sure to not alter `tiny_data`") check_function(., "gf_histogram") %>% { check_arg(., "fill", arg_not_specified_msg = "Remember to use `fill =` with your own choice of color") check_arg(., "color", arg_not_specified_msg = "Remember to use `color =` with your own choice of color") } check_or(., check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal(eval = FALSE, incorrect_msg = "Make sure you specify `~outcome` as the first argument.") check_arg(., "data") %>% check_equal(incorrect_msg = "Did you set `data = tiny_data`?") }, override_solution_code(., '{ outcome <- c(1, 2, 3, 4, 5) tiny_data <- data.frame(outcome) gf_histogram(~tiny_data, fill = "aquamarine", color = "gray") }') %>% check_function("gf_histogram") %>% check_arg("object") %>% check_equal(eval = FALSE) ) }
CK Code: ch3-3
require(coursekata) # This is the same code as before but we added in another outcome value, 3.2 outcome <- c(1, 2, 3, 4, 5, 3.2) tiny_data <- data.frame(outcome) # This makes a histogram with 5 bins gf_histogram(~ outcome, data = tiny_data, fill = "aquamarine", color = "gray", bins = 5) outcome <- c(1, 2, 3, 4, 5, 3.2) tiny_data <- data.frame(outcome) gf_histogram(~ outcome, data = tiny_data, fill = "aquamarine", color = "gray", bins = 5) ex() %>% check_object("outcome", undefined_msg = "Make sure not to delete 'outcome'") %>% check_equal(incorrect_msg = "Make sure not to change the content of 'outcome'") ex() %>% check_object("tiny_data", undefined_msg = "Make sure not to delete 'tiny_data'") %>% check_equal() ex() %>% check_function("gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() } success_msg("You're doing great!")
CK Code: ch3-4
require(coursekata) # add 3.7 to the outcome values, then run this code outcome <- c(1, 2, 3, 4, 5, 3.2) tiny_data <- data.frame(outcome) # this makes a histogram with 5 bins gf_histogram(~ outcome, data = tiny_data, fill = "aquamarine", color = "gray", bins = 5) # add 3.7 to the outcome values, then run this code outcome <- c(1, 2, 3, 4, 5, 3.2, 3.7) tiny_data <- data.frame(outcome) # this makes a histogram with 5 bins gf_histogram(~ outcome, data = tiny_data, fill = "aquamarine", color = "gray", bins = 5) inc_msg = "Don't alter the other code in this exercise -- only the contents of `outcome`." ex() %>% { check_object(., "outcome") %>% check_equal(incorrect_msg = "Did you add 3.7 to the outcome vector?") check_object(., "tiny_data") %>% check_equal(incorrect_msg = inc_msg) check_function(., "gf_histogram") }
CK Code: ch3-5
require(coursekata) # adjust the number of bins to 50 gf_histogram(~ Thumb, data = Fingers, bins = ) # adjust the number of bins to 5 gf_histogram(~ Thumb, data = Fingers) # adjust the bin width to 3 gf_histogram(~ Thumb, data = Fingers, binwidth = ) # adjust the bin width to 10 gf_histogram(~ Thumb, data = Fingers) # adjust the number of bins to 50 gf_histogram(~ Thumb, data = Fingers, bins = 50) # adjust the number of bins to 5 gf_histogram(~ Thumb, data = Fingers, bins = 5) # adjust the bin width to 3 gf_histogram(~ Thumb, data = Fingers, binwidth = 3) # adjust the bin width to 10 gf_histogram(~ Thumb, data = Fingers, binwidth = 10) ex() %>% { check_function(., "gf_histogram", index = 1) %>% check_arg("bins") %>% check_equal(incorrect_msg = "Did you set the number of `bins` to 50?") check_function(., "gf_histogram", index = 2) %>% check_arg("bins", arg_not_specified_msg = "Did you set the number of `bins` to 5?") %>% check_equal(incorrect_msg = "Did you set the number of `bins` to 5?") check_function(., "gf_histogram", index = 3) %>% check_arg("binwidth") %>% check_equal(incorrect_msg = "Did you set the `binwidth` to 3?") check_function(., "gf_histogram", index = 4) %>% check_arg("binwidth", arg_not_specified_msg = "Did you set the `binwidth` to 10?") %>% check_equal(incorrect_msg = "Did you set the `binwidth` to 10?") }
CK Code: ch3-6
require(coursekata) # This will create a relative frequency histogram of Age gf_dhistogram(~ Age, data = MindsetMatters, fill = "coral2") # Add code below to create a frequency histogram of Age gf_dhistogram(~ Age, data = MindsetMatters, fill = "coral2", bins = 20) gf_histogram(~ Age, data = MindsetMatters) ex() %>% check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }
CK Code: ch3-7
require(coursekata) # make a density histogram of Thumb in the Fingers data frame # make a density histogram of Thumb in the Fingers data frame gf_dhistogram(~ Thumb, data = Fingers) ex() %>% check_or( check_function(., "gf_dhistogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_dhistogram(Fingers, ~ Thumb)") %>% check_function("gf_dhistogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_dhistogram(~Fingers$Thumb)") %>% check_function("gf_dhistogram") %>% check_arg("object") %>% check_equal() )
CK Code: ch3-8
require(coursekata) # This creates our model population model_pop <- 1:6 # Write code to create a relative frequency histogram of our model population. # Remember to include bins as an argument. # This creates our model population model_pop <- 1:6 # Write code to create a relative frequency histogram of our model population. # Remember to include bins as an argument. # gf_dhistogram(~ model_pop, color = "black", bins = 6) ex() %>% { check_object(., "model_pop") %>% check_equal() override_solution_code(., 'gf_dhistogram(~ model_pop, color = "black", bins = 6)' ) %>% check_function(., "gf_dhistogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "bins") %>% check_equal() } }
CK Code: ch3-9
require(coursekata); allow_solution_error() model_pop <- 1:6 # This samples the same way 12 times resample(model_pop, 12) # Will this accomplish the same thing? # Do not change the code --- just submit it sample(model_pop, 12) model_pop <- 1:6 # This samples the same way 12 times resample(model_pop, 12) # Will this accomplish the same thing? # Do not change the code --- just submit it sample(model_pop, 12) success_msg("This code is supposed to throw an error. You can read it in the Console tab.")
CK Code: ch3-10
require(coursekata) set.seed(4) model_pop <- 1:6 sample1 <- resample(model_pop, 12) # Write code to create a relative frequency histogram # Remember to put in bins as an argument # Don't use any custom coloring model_pop <- 1:6 sample1 <- resample(model_pop, 12) # you have to uncomment this line for it to work # gf_dhistogram(~ sample1, bins = 6) ex() %>% { override_solution_code(., 'model_pop <- 1:6; sample1 <- resample(model_pop, 12); gf_dhistogram(~ sample1, bins = 6)' ) %>% { check_object(., "sample1") %>% check_equal() check_function(., "gf_dhistogram") %>% { check_arg(., "bins") check_arg(., "object") %>% check_equal(eval = FALSE) } } }
CK Code: ch3-11
require(coursekata) set.seed(5) model_pop <- 1:6 # Modify this code from 12 dice rolls to 24 dice rolls sample2 <- resample(model_pop, 12) # This will create a density histogram gf_dhistogram(~ sample2, color = "darkgray", fill = "springgreen", bins = 6) model_pop <- 1:6 # Modify this code from 12 dice rolls to 24 dice rolls sample2 <- resample(model_pop, 24) # This will create a density histogram (you have to uncomment the line for it to work) # gf_dhistogram(~ sample2, color = "darkgray", fill = "springgreen", bins = 6) ex() %>% check_object("sample2") %>% check_equal()
CK Code: ch3-12
require(coursekata) set.seed(7) model_pop <- 1:6 # create samples #3, #4, #5 of 24 dice rolls sample3 <- sample4 <- sample5 <- # this will create a density histogram of your sample3 # add onto it to include a density plot gf_dhistogram(~ sample3, color = "darkgray", fill = "springgreen", bins = 6) # create density histograms of sample4 and sample5 with density plots model_pop <- 1:6 # create samples sample3 <- resample(model_pop, 24) sample4 <- resample(model_pop, 24) sample5 <- resample(model_pop, 24) # this will create a density histogram of your sample3 # add onto it to include a density plot # gf_dhistogram(~ sample3, color = "darkgray", fill = "springgreen", bins = 6) %>% # gf_density() # create density histograms of sample4 and sample5 with density plots # gf_dhistogram(~ sample4, color = "darkgray", fill = "springgreen", bins = 6) %>% # gf_density() # gf_dhistogram(~ sample5, color = "darkgray", fill = "springgreen", bins = 6) %>% # gf_density() ex() %>% override_solution_code('{ model_pop <- 1:6 sample3 <- resample(model_pop, 24) sample4 <- resample(model_pop, 24) sample5 <- resample(model_pop, 24) gf_dhistogram(~ sample3, color = "darkgray", fill = "springgreen", bins = 6) %>% gf_density() gf_dhistogram(~ sample4, color = "darkgray", fill = "springgreen", bins = 6) %>% gf_density() gf_dhistogram(~ sample5, color = "darkgray", fill = "springgreen", bins = 6) %>% gf_density(); }') %>% { check_object(., "sample3") %>% check_equal() check_object(., "sample4") %>% check_equal() check_object(., "sample5") %>% check_equal() check_function(., "gf_dhistogram", index = 1) %>% check_arg("object") %>% check_equal(eval = FALSE) check_function(., "gf_dhistogram", index = 2) %>% check_arg("object") %>% check_equal(eval = FALSE) check_function(., "gf_dhistogram", index = 3) %>% check_arg("object") %>% check_equal(eval = FALSE) check_function(., "gf_density", index = 1) check_function(., "gf_density", index = 2) check_function(., "gf_density", index = 3) }
CK Code: ch3-13
require(coursekata) set.seed(7) model_pop <- 1:6 # create a sample with 1000 rolls of a die large_sample <- # this will create a density histogram of your large_sample gf_dhistogram(~ large_sample, color = "darkgray", fill = "springgreen", bins = 6) model_pop <- 1:6 # create a sample with 1000 rolls of a die large_sample <- resample(model_pop, 1000) # this will create a density histogram of your large_sample # gf_dhistogram(~ large_sample, color = "darkgray", fill = "springgreen", bins = 6) ex() %>% override_solution_code('{ model_pop <- 1:6 # create a sample with 1000 rolls of a die large_sample <- resample(model_pop, 1000) # this will create a density histogram of your largesample gf_dhistogram(~ large_sample, color="darkgray", fill="springgreen", bins=6) }') %>% { check_object(., "large_sample") %>% check_equal() check_function(., "gf_dhistogram") %>% check_arg("object") %>% check_equal(eval = FALSE) }
CK Code: ch3-14
require(coursekata) set.seed(10) model_pop <- 1:6 w_pop <- c(rep(1,5), 2, rep(3,10), rep(4,10), 5, rep(6,5)) # Create a sample that draws 24 times from w_pop small_sample <- # This will create a density histogram of your small_sample gf_dhistogram(~ small_sample, color = "darkgray", fill = "mistyrose", bins = 6) # Create a sample that draws 24 times from w_pop small_sample <- resample(w_pop, 24) # This will create a density histogram of your small_sample # gf_dhistogram(~ small_sample, color = "darkgray", fill = "mistyrose", bins = 6) ex() %>% override_solution_code('{ # Create a sample that draws 24 times from w_pop small_sample <- resample(w_pop, 24) # This will create a density histogram of your small_sample gf_dhistogram(~ small_sample, color = "darkgray", fill = "mistyrose", bins = 6) }') %>% { check_object(., "small_sample") %>% check_equal() check_function(., "gf_dhistogram") %>% check_arg("object") %>% check_equal() }
CK Code: ch3-15
require(coursekata) set.seed(7) model_pop <- 1:6 w_pop <- c(rep(1,5), 2, rep(3,10), rep(4,10), 5, rep(6,5)) # create a sample that draws 1000 times from w_pop large_sample <- # this will create a density histogram of your large_sample gf_dhistogram(~ large_sample, color = "darkgray", fill = "mistyrose", bins = 6) # create a sample that draws 1000 times from w_pop large_sample <- resample(w_pop, 1000) # this will create a density histogram of your large_sample # gf_dhistogram(~ large_sample, color = "darkgray", fill = "mistyrose", bins = 6) ex() %>% override_solution_code('{ # create a sample that draws 1000 times from w_pop large_sample <- resample(w_pop, 1000) # this will create a density histogram of your large_sample gf_dhistogram(~ large_sample, color = "darkgray", fill = "mistyrose", bins = 6) }') %>% { check_object(., "large_sample") %>% check_equal() check_function(., "gf_dhistogram") %>% check_arg("object") %>% check_equal() }
CK Code: ch3-16
require(coursekata) # Write code to sort Wt from lowest to highest # Solution 1 arrange(MindsetMatters, Wt) # Solution 2 sort(MindsetMatters$Wt) 3 ex() %>% check_or( check_function(., "arrange", not_called_msg = "If you were trying to use `sort`, that's acceptable but your code must have resulted in an error.") %>% check_result() %>% check_equal(), check_function(., "sort") %>% check_result() %>% check_equal() )
CK Code: ch3-17
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) # Modify the code to get favstats for Population of countries in HappyPlanetIndex favstats() favstats(~ Population, data = HappyPlanetIndex) ex() %>% check_function("favstats") %>% check_result() %>% check_equal()
CK Code: ch3-18
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) # make a histogram of Population from HappyPlanetIndex using gf_histogram # make a histogram of Population from HappyPlanetIndex using gf_histogram gf_histogram(~ Population, data = HappyPlanetIndex) ex() %>% check_or( check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }, override_solution(., "gf_histogram(HappyPlanetIndex, ~ Population)") %>% check_function("gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "gformula") %>% check_equal() }, override_solution(., "gf_histogram(~HappyPlanetIndex$Population)") %>% check_function("gf_histogram") %>% { check_arg(., "object") %>% check_equal() }, override_solution(., "gf_histogram(data = HappyPlanetIndex, gformula = ~ Population)") %>% check_function("gf_histogram") %>% { check_arg(., "data") %>% check_equal() check_arg(., "gformula") %>% check_equal() } )
CK Code: ch3-19
require(coursekata) # Based on the numbers from the favstats results above, use R as a calculator to find the range of Wt in MindsetMatters # Based on the numbers from the favstats results above, use R as a calculator to find the range of Wt in MindsetMatters 196 - 90 ex() %>% check_output_expr("196 - 90")
CK Code: ch3-20
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) # Use R as a calculator to find the IQR of Population from the HappyPlanetIndex data set # Use R as a calculator to find the IQR of Population from the HappyPlanetIndex data set 31.225 - 4.455 ex() %>% check_output_expr("31.225 - 4.455")
CK Code: ch3-21
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) # Modify this code to create a boxplot of Population from HappyPlanetIndex gf_boxplot(Wt ~ 1, data = MindsetMatters) # Modify this code to create a boxplot of Population from HappyPlanetIndex gf_boxplot(Population ~ 1, data = HappyPlanetIndex) ex() %>% check_function("gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }
CK Code: ch3-22
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) # this calculates the Q3 + 1.5*IQR upper_boundary <- 31.225 + 1.5*(31.225-4.455) # modify this code to filter in only countries with population sizes less than the upper_boundary SmallerCountries <- # this makes a histogram of the smaller countries' populations gf_histogram(~ Population, data = SmallerCountries, fill = "slateblue4") %>% gf_labs(x = "Population (in millions)", title = "Population of Countries (Excludes Outliers)") upper_boundary <- 31.225 + 1.5*(31.225-4.455) SmallerCountries <- filter(HappyPlanetIndex, Population < upper_boundary) gf_histogram(~ Population, data = SmallerCountries, fill = "slateblue4") %>% gf_labs(x = "Population (in millions)", title = "Population of Countries (Excludes Outliers)") ex() %>% { check_function(., "filter") %>% { check_arg(., ".data") %>% check_equal(incorrect_msg="Don't forget to filter in HappyPlanetIndex") check_arg(., "...") %>% check_equal(incorrect_msg="Did you use `Population < upper_boundary` as the second argument?") check_result(.) %>% check_equal() } check_object(., "SmallerCountries") %>% check_equal check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() } }
CK Code: ch3-23
require(coursekata) HappyPlanetIndex$Region <- recode( HappyPlanetIndex$Region, '1'="Latin America", '2'="Western Nations", '3'="Middle East and North Africa", '4'="Sub-Saharan Africa", '5'="South Asia", '6'="East Asia", '7'="Former Communist Countries" ) pop_stats <- favstats(~ Population, data = HappyPlanetIndex) SmallerCountries <- filter(HappyPlanetIndex, Population < (pop_stats$Q3 + 1.5*(pop_stats$Q3 - pop_stats$Q1))) # Make a boxplot of Population from the SmallerCountries gf_boxplot(Population ~ 1, data = SmallerCountries) # Make a boxplot of Population from the SmallerCountries gf_boxplot(Population ~ 1, data = SmallerCountries) ex() %>% check_function("gf_boxplot") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal() }
CK Code: ch3-24
require(coursekata) # Create a bar graph of RaceEthnic in the Fingers data frame. Use the gf_bar() function # Create a bar graph of RaceEthnic in the Fingers data frame. Use the gf_bar() function gf_bar(~ RaceEthnic, data = Fingers) ex() %>% check_function("gf_bar") %>% { check_arg(., "data") %>% check_equal(incorrect_msg="Don't forget to set `data = Fingers`") check_arg(., "object") %>% check_equal(incorrect_msg = "Did you use `~ RaceEthnic`?") }
CK Code: ch3-25
require(coursekata) # Add arguments color and fill to these bar graphs # Make sure to add color and fill to both of the graphs gf_bar(~ Sex, data = Fingers) gf_bar(~ RaceEthnic, data = Fingers) # the colors below are just examples. Any color is acceptable as long as you use the color and fill arguments gf_bar(~ Sex, data = Fingers, color="darkgray", fill="mistyrose" ) gf_bar(~ RaceEthnic, data = Fingers, color="darkgray", fill="mistyrose") ex() %>% { check_function(., "gf_bar", index = 1) %>% { check_arg(., "color") check_arg(., "fill") } check_function(., "gf_bar", index = 2) %>% { check_arg(., "color") check_arg(., "fill") } }
CK Code: ch3-26
require(coursekata) # This creates a frequency table of Sex # Do not change this code tally(~ Sex, data = Fingers) # Write code to create a frequency table of RaceEthnic tally(~ Sex, data = Fingers) tally(~ RaceEthnic, data = Fingers) ex() %>% check_function(., "tally", index = 2) %>% check_result() %>% check_equal() success_msg("You're a supe-R coder!")
CK Code: ch3-27
require(coursekata) # Add margin and format arguments to the tally() function. Set margins to TRUE and format to proportion tally(~ RaceEthnic, data = Fingers) tally(~ RaceEthnic, data = Fingers, margins = TRUE, format = "proportion") ex() %>% check_or( check_function(., "tally") %>% { check_arg(., "margins") %>% check_equal() check_arg(., "format") %>% check_equal() check_result(.) %>% check_equal() }, check_output_expr(., 'tally(~ RaceEthnic, data = Fingers, margins = TRUE, format = "proportion")') )
CK Code: ch3-28
require(coursekata) # write code to create a frequency table of Thumb tally(~Thumb, data = Fingers) ex() %>% check_function("tally") %>% { check_arg(., "x") %>% check_equal() check_arg(., "data") %>% check_equal() check_result(.) %>% check_equal() }
CK Code: ch3-29
require(coursekata) # run your code here
CK Code: A3_Code_Review2_01
require(coursekata) # run your code here
CK Code: A3_Code_Review2_02
require(coursekata) # run your code here
CK Code: A3_Code_Review2_03
require(coursekata) # run your code here
CK Code: A3_Code_Review2_04
require(coursekata) # run your code here
CK Code: A3_Code_Review2_05
require(coursekata) # run your code here
CK Code: A3_Code_Review2_06
require(coursekata) # run your code here
CK Code: A3_Code_Review2_07
require(coursekata) # run your code here
CK Code: A3_Code_Review2_08
require(coursekata) # run your code here
CK Code: A3_Code_Review2_09
require(coursekata) # run your code here
CK Code: A3_Code_Review2_10
require(coursekata) # run your code here
CK Code: A3_Code_Review2_11

Responses