Course Outline
-
segmentLearnosity
-
segmentCKCode
-
ckcode-chapter-b1-simple-model
list Items Test Book
Book
ckcode ⌲ chapter-b1-simple-model
require(coursekata)
# Modify this line to save the numbers to outcome
outcome <- c()
# This will give you the favstats for outcome
favstats(outcome)
outcome <- c(5, 5, 5, 10, 20)
favstats(outcome)
ex() %>% {
check_object(., "outcome") %>% check_equal()
check_function(., "favstats") %>% check_result() %>% check_equal()
}
CK Code: B1_Code_Median_01
require(coursekata)
# modify this code to make a histogram of GradePredict
# the second line adds more tick marks to the x-axis
gf_histogram(~ , data = Fingers, color = "forestgreen") +
scale_x_continuous(breaks = seq(2.0, 4.0, by = 0.1))
# modify this code to get the favstats for GradePredict
favstats(~ GradePredict, data = )
# modify this code to make a histogram of GradePredict
# the second line adds more tick marks to the x-axis
gf_histogram(~ GradePredict, data = Fingers, color = "forestgreen") +
scale_x_continuous(breaks = seq(2.0, 4.0, by = 0.1))
# modify this code to get the favstats for GradePredict
favstats(~ GradePredict, data = Fingers)
ex() %>% {
check_or(.,
check_function(., "gf_histogram") %>% {
check_arg(., "object") %>% check_equal()
check_arg(., "data") %>% check_equal()
},
override_solution(., "gf_histogram(Fingers, ~ GradePredict)") %>%
check_function("gf_histogram") %>% {
check_arg(., "object") %>% check_equal()
check_arg(., "gformula") %>% check_equal()
},
override_solution(., "gf_histogram(~ Fingers$GradePredict)") %>%
check_function("gf_histogram") %>%
check_arg(., "object") %>%
check_equal()
)
check_function(., "favstats") %>%
check_result() %>%
check_equal()
}
CK Code: B1_Code_Median_02
require(coursekata)
# modify this code to make a histogram of Thumb
gf_histogram()
# get the favstats for Thumb
# modify this code to make a histogram of Thumb
gf_histogram(~ Thumb, data = Fingers)
# get the favstats for Thumb
favstats(~ Thumb, data = Fingers)
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()
},
override_solution(., "gf_histogram(~ Fingers$Thumb)") %>%
check_function("gf_histogram") %>%
check_arg(., "object") %>%
check_equal()
)
check_function(., "favstats") %>%
check_result() %>%
check_equal()
}
CK Code: B1_Code_Median_03
require(coursekata)
# make a histogram of Age in the MindsetMatters data frame
# set the fill = "red"
# get the favstats for Age
# make a histogram of Age in the MindsetMatters data frame
# set the fill = "red"
gf_histogram(~ Age, data = MindsetMatters, fill = "red")
# get the favstats for Age
favstats(~ Age, data = MindsetMatters)
ex() %>% {
check_or(.,
check_function(., "gf_histogram") %>% {
check_arg(., "object") %>% check_equal()
check_arg(., "data") %>% check_equal()
},
override_solution(., "gf_histogram(MindsetMatters, ~ Age)") %>%
check_function("gf_histogram") %>% {
check_arg(., "object") %>% check_equal()
check_arg(., "gformula") %>% check_equal()
},
override_solution(., "gf_histogram(~ MindsetMatters$Age)") %>%
check_function("gf_histogram") %>%
check_arg(., "object") %>%
check_equal()
)
check_function(., "gf_histogram") %>%
check_arg("fill") %>%
check_equal()
check_function(., "favstats") %>%
check_result() %>%
check_equal()
}
CK Code: B1_Code_Median_04
require(coursekata)
outcome <- c(5, 5, 5, 10, 20)
# Modify this code to draw a vline representing the median in "purple"
gf_histogram(~outcome) %>%
gf_vline(xintercept = 9, color = "blue")
# Modify this code to draw a vline representing the median in "purple"
gf_histogram(~outcome) %>%
gf_vline(xintercept = 5, color = "purple")
ex() %>% {
check_function(., "gf_histogram") %>%
check_arg("object") %>%
check_equal()
check_function(., "gf_vline") %>% {
check_arg(., "xintercept") %>% check_equal()
check_arg(., "color") %>% check_equal()
}
}
CK Code: B1_Code_Median_05
require(coursekata)
# modify this to fit the empty model of Thumb
empty_model <-
# print out the model estimates
empty_model <- lm(Thumb ~ NULL, data = Fingers)
empty_model
ex() %>% {
check_object(., "empty_model") %>% check_equal()
}
CK Code: B1_Code_Empty_01
require(coursekata)
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# saves the best-fitting empty model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# add gf_model to this scatterplot
gf_point(Thumb ~ Height, data = Fingers)
# add gf_model to this jitter plot
gf_jitter(Thumb ~ Sex, data = Fingers, width = .1)
# saves the best-fitting empty model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# add gf_model to this scatterplot
gf_point(Thumb ~ Height, data = Fingers) %>%
gf_model(empty_model)
# add gf_model to this jitter plot
gf_jitter(Thumb ~ Sex, data = Fingers, width = .1) %>%
gf_model(empty_model)
ex() %>% {
check_function(., "gf_model", index = 1) %>%
check_arg("object") %>%
check_equal()
check_or(.,
check_function(., "gf_model", index = 1) %>%
check_arg("model") %>%
check_equal(),
override_solution(., "gf_point(Thumb ~ Height, data = Fingers) %>% gf_model(Thumb ~ NULL)") %>%
check_function("gf_model", index = 1) %>%
check_arg("model") %>%
check_equal()
)
check_function(., "gf_model", index = 2) %>%
check_arg("object") %>%
check_equal()
check_or(.,
check_function(., "gf_model", index = 2) %>%
check_arg("model") %>%
check_equal(),
override_solution(., "gf_model(empty_model); gf_point(Thumb ~ Height, data = Fingers) %>% gf_model(Thumb ~ NULL)") %>%
check_function("gf_model", index = 2) %>%
check_arg("model") %>%
check_equal()
)
}
CK Code: B1_Code_Empty_02
require(coursekata)
# saves the empty model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# write code to generate predictions of the empty model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
predict(empty_model)
ex() %>% check_object("empty_model") %>% check_equal()
CK Code: B1_Code_Predictions_01
require(coursekata)
# this saves the empty_model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# modify this to save the predictions from the empty_model in a new variable
Fingers$Predict <-
# prints out selected variables from Fingers
head(select(Fingers, Thumb, Predict), 10)
# this makes a scatterplot of Thumb by Height and overlays the empty model's predictions as open blue circles
gf_point(Thumb ~ Height, data = Fingers, width = .1) %>%
gf_point(Predict ~ Height, color = "blue", shape = 1, height = 0)
# this saves the empty_model
empty_model <- lm(Thumb ~ NULL, data = Fingers)
# modify this to save the predictions from the empty_model in a new variable
Fingers$Predict <- predict(empty_model)
# prints out selected variables from Fingers
head(select(Fingers, Thumb, Predict), 10)
# this makes a scatterplot of Thumb by Height and overlays the empty model's predictions as open blue circles
gf_point(Thumb ~ Height, data = Fingers, alpha = .2) %>%
gf_point(Predict ~ Height, color = "blue", shape = 1)
ex() %>%
check_object("Fingers") %>%
check_column("Predict") %>%
check_equal()
CK Code: B1_Code_Predictions_02
require(coursekata)
Fingers$TinySet <- c(1,1,1,0,0,0,1,0,0,1, rep(0,147))
Fingers$TinySet[142] <- 1
Fingers <- arrange(arrange(Fingers, Height), desc(TinySet))
empty_model <- lm(Thumb ~ NULL, data = Fingers)
Fingers <- Fingers %>% mutate(
Predict = predict(empty_model),
Resid = Thumb - Predict
)
# modify this to save the residuals from the empty_model
Fingers$Resid <-
# this prints selected variables from Fingers
select(Fingers, Thumb, Predict, Resid)
Fingers$Resid <- Fingers$Thumb - Fingers$Predict
ex() %>% check_object("Fingers") %>%
check_column("Resid") %>% check_equal()
CK Code: B1_Code_Thinking_01
require(coursekata)
Fingers$TinySet <- c(1,1,1,0,0,0,1,0,0,1, rep(0,147))
Fingers$TinySet[142] <- 1
Fingers <- arrange(arrange(Fingers, Height), desc(TinySet))
empty_model <- lm(Thumb ~ NULL, data = Fingers)
Fingers <- Fingers %>% mutate(
Predict = predict(empty_model),
Resid = Thumb - Predict
)
# calculate the residuals from empty_model the easy way
# and save them in the Fingers data frame
Fingers$EasyResid <-
# this prints select variables from Fingers
head(select(Fingers, Thumb, Predict, Resid, EasyResid))
Fingers$EasyResid <- resid(empty_model)
Fingers
ex() %>% check_object("Fingers") %>%
check_column("EasyResid") %>% check_equal()
CK Code: Code_Thinking_02
require(coursekata)
empty_model <- lm(Thumb ~ NULL, data = Fingers)
Fingers <- Fingers %>% mutate(
Predict = predict(empty_model),
Resid = resid(empty_model)
)
# assume Fingers data frame already has the variable Resid saved in it
sum(Fingers$Resid)
ex() %>% {
check_output_expr(., "sum(Fingers$Resid)")
}
CK Code: Code_Thinking_03
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_01
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_02
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_03
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_04
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_05
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_06
require(coursekata)
# run your code here
CK Code: B1_Code_Review2_07