## Course Outline

• segmentLearnosity
• segmentCKCode

### list Items Test Book

Book
• Items Test Book

## ckcode ⌲ chapter-d2-multivariate-comparisons

require(coursekata) # this fits the Neighborhood model Neighborhood_model <- lm(PriceK ~ Neighborhood, data=Smallville) # this saves the residuals PriceK_N_resid <- resid(Neighborhood_model) # square and sum the residuals # this fits the Neighborhood model Neighborhood_model <- lm(PriceK ~ Neighborhood, data=Smallville) # this saves the residuals PriceK_N_resid <- resid(Neighborhood_model) # square and sum the residuals sum(PriceK_N_resid ^ 2) ex() %>% check_output_expr("sum(PriceK_N_resid ^ 2)")
CK Code: D2_Code_SumSqu_01
require(coursekata) # this fits the Neighborhood model Neighborhood_model <- lm(PriceK ~ Neighborhood, data=Smallville) # find the ANOVA table # this fits the Neighborhood model Neighborhood_model <- lm(PriceK ~ Neighborhood, data=Smallville) # find the ANOVA table supernova(Neighborhood_model) ex() %>% check_function("supernova") %>% check_result() %>% check_equal()
CK Code: D2_Code_SumSqu_02
require(coursekata) # this saves the multivariate model multi_model <- lm(PriceK~ Neighborhood + HomeSizeK, data = Smallville) # write code to generate the model comparisons # this saves the multivariate model multi_model <- lm(PriceK~ Neighborhood + HomeSizeK, data = Smallville) # write code to generate the model comparisons generate_models(multi_model) ex() %>% check_function("generate_models") %>% check_result() %>% check_equal()
CK Code: D2_Code_PREF_01
require(coursekata) # this fits and saves the neighborhood model Neighborhood_model <- lm(PriceK~ Neighborhood, data = Smallville) # write code to save the residuals of the neighborhood model PriceK_N_resid <- # this will print a few of these residuals head(PriceK_N_resid) # this fits and saves the neighborhood model Neighborhood_model <- lm(PriceK~ Neighborhood, data = Smallville) # write code to save the residuals of the neighborhood model PriceK_N_resid <- resid(Neighborhood_model) # this will print a few of these residuals head(PriceK_N_resid) ex() %>% check_object("PriceK_N_resid") %>% check_equal()
CK Code: D2_Code_Shuffle_01
require(coursekata) Neighborhood_model <- lm(PriceK~ Neighborhood, data = Smallville) # this saves the residuals of the neighborhood model PriceK_N_resid <- resid(Neighborhood_model) # write code to fit the multivariate model again, this time # using residuals from the neighborhood model as the outcome variable PriceK_N_resid_multi_model <- lm() # this will print the ANOVA table for this new model supernova(PriceK_N_resid_multi_model) # this saves the residuals of the neighborhood model PriceK_N_resid <- resid(Neighborhood_model) # write code to fit the multivariate model again, this time # using residuals from the neighborhood model as the outcome variable PriceK_N_resid_multi_model <- lm(PriceK_N_resid ~ Neighborhood + HomeSizeK, data = Smallville) # this will print the ANOVA table for this new model supernova(PriceK_N_resid_multi_model) ex() %>% check_object("PriceK_N_resid_multi_model") %>% check_equal()
CK Code: D2_Code_Shuffle_02
require(coursekata) # code to fit neighborhood model and save residuals Neighborhood_model <- lm(PriceK ~ Neighborhood, data = Smallville) Smallville$PriceK_N_resids <- resid(Neighborhood_model) # this code prints sample F for HomeSizeK f(PriceK_N_resids ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # modify the code below to produce the F when residuals are shuffled f(PriceK_N_resids ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # this code prints sample F for HomeSizeK f(PriceK_N_resids ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # modify the code below to produce the F when residuals are shuffled f(shuffle(PriceK_N_resids) ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) ex() %>% check_function("f", index = 2) %>% { check_arg(., 1) %>% check_equal() check_arg(., "data") %>% check_equal() check_arg(., "predictor") %>% check_equal() } CK Code: D2_Code_Targeted_01 require(coursekata) Neighborhood_model <- lm(PriceK~ Neighborhood, data = Smallville) Smallville$PriceK_N_resids <- resid(Neighborhood_model) set.seed(runif(1, min = 1, max = 10000)) # This code generates one shuffled HomeSizeK F # Modify it to make a sampling distribution of 1000 shuffled Fs # Save them in a data frame called HomeSizeK_sdof f(shuffle(PriceK_N_resids) ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # This code will put these Fs into a histogram gf_histogram(~ f, data = HomeSizeK_sdof) %>% gf_labs(title = "shuffled HomeSizeK Fs") # This code generates one shuffled HomeSizeK F # Modify it to make a sampling distribution of 1000 shuffled Fs # Save them in a data frame called HomeSizeK_sdof HomeSizeK_sdof <- do(1000) * f(shuffle(PriceK_N_resids) ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # This code will put these Fs into a histogram gf_histogram(~ f, data = HomeSizeK_sdof) %>% gf_labs(title = "shuffled HomeSizeK Fs") ex() %>% check_correct( check_object(., "HomeSizeK_sdof") %>% check_equal(), check_function(., "do") %>% { check_arg(., 1) %>% check_equal() check_operator(., "*") } )
CK Code: D2_Code_Targeted_02
require(coursekata) Neighborhood_model <- lm(PriceK~ Neighborhood, data = Smallville) Smallville$PriceK_N_resids <- resid(Neighborhood_model) # This saves the sample HomeSizeK F HomeSizeK_f <- f(PriceK_N_resids ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # This code generates a sampling distribution of shuffled HomeSizeK Fs HomeSizeK_sdof <- do(1000) * f(shuffle(PriceK_N_resids) ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # Paste in the code for tallying the p-value for HomeSizeK # Modify the code below to generate an ANOVA table from the multivariate model lm(PriceK ~ Neighborhood + HomeSizeK, data = Smallville) # This saves the sample HomeSizeK F HomeSizeK_f <- f(PriceK_N_resids ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # This code generates a sampling distribution of shuffled HomeSizeK Fs HomeSizeK_sdof <- do(1000) * f(shuffle(PriceK_N_resids) ~ Neighborhood + HomeSizeK, data = Smallville, predictor = ~HomeSizeK) # Paste in the code for tallying the p-value for HomeSizeK tally(~ f > HomeSizeK_f, data=HomeSizeK_sdof, format="proportion") # Modify the code below to generate an ANOVA table from the multivariate model supernova(lm(PriceK ~ Neighborhood + HomeSizeK, data = Smallville)) ex() %>% { check_function(., "tally") %>% check_result() %>% check_equal() check_function(., "supernova") %>% check_result() %>% check_equal() } CK Code: D2_Code_Targeted_03 require(coursekata) # code to make ANOVA table for multivariate model supernova(lm(PriceK ~ HomeSizeK + HasFireplace, data = Smallville)) # add code to get ANOVA tables for each of the two single-predictor models # code to make ANOVA table for multivariate model supernova(lm(PriceK ~ HomeSizeK + HasFireplace, data = Smallville)) # add code to get ANOVA tables for each of the two single-predictor models supernova(lm(PriceK ~ HasFireplace, data = Smallville)) supernova(lm(PriceK ~ HomeSizeK, data = Smallville)) ex() %>% { check_function(., "supernova", index = 2) %>% check_result() %>% check_equal() check_function(., "supernova", index = 3) %>% check_result() %>% check_equal() } CK Code: D2_Code_Deciding_01 require(coursekata) # find the best-fitting parameter estimates # find the best-fitting parameter estimates lm(tip_percent ~ condition + gender, data = tip_exp) ex() %>% check_function("lm") %>% check_result() %>% check_equal() CK Code: D2_Code_Categorical_01 require(coursekata) # write code to save this model as multi_model lm(formula = tip_percent ~ condition + gender, data = tip_exp) # this makes a faceted jitter plot of our data # write code to overlay the multi_model onto it gf_jitter(tip_percent ~ condition, color = ~condition, data = tip_exp, width = .1) %>% gf_facet_grid(. ~ gender) # write code to save this model as multi_model multi_model <- lm(formula = tip_percent ~ condition + gender, data = tip_exp) # this makes a faceted jitter plot of our data # write code to overlay the multi_model onto it gf_jitter(tip_percent ~ condition, color = ~condition, data = tip_exp, width = .1) %>% gf_facet_grid(. ~ gender) %>% gf_model(multi_model) ex() %>% { check_object(., "multi_model") %>% check_equal() check_function(., "gf_model") %>% { check_arg(., "object") %>% check_equal() check_arg(., "model") %>% check_equal() } } CK Code: D2_Code_Categorical_02 require(coursekata) # here is the code to find the best-fitting model # modify this to generate the ANOVA table for this model lm(tip_percent ~ condition + gender, data = tip_exp) # here is the code to find the best-fitting model # modify this to generate the ANOVA table for this model supernova(lm(tip_percent ~ condition + gender, data = tip_exp)) ex() %>% check_output_expr("supernova(lm(tip_percent ~ condition + gender, data = tip_exp))") CK Code: D2_Code_Error_01 require(coursekata) # add the color argument to this scatterplot gf_point(FEV ~ HEIGHT, data = fevdata) # add the color argument to this scatterplot gf_point(FEV ~ HEIGHT, data = fevdata, color = ~AGE) ex() %>% check_function("gf_point") %>% check_arg("color") %>% check_equal() CK Code: D2_Code_Multiple_01 require(coursekata) # find the best-fitting parameter estimates # find the best-fitting parameter estimates lm(FEV ~ HEIGHT + AGE, data = fevdata) ex() %>% check_function("lm") %>% check_result() %>% check_equal() CK Code: D2_Code_Multiple_02 require(coursekata) # this saves the multivariate model multi_model <- lm(FEV ~ HEIGHT + AGE, data = fevdata) # add the multi_model to this plot gf_point(FEV ~ HEIGHT, data = fevdata, color = ~AGE, alpha = .2) # this saves the multivariate model multi_model <- lm(FEV ~ HEIGHT + AGE, data = fevdata) # add the multi_model to this plot gf_point(FEV ~ HEIGHT, data = fevdata, color = ~AGE, alpha = .2) %>% gf_model(multi_model) ex() %>% check_function("gf_model") %>% { check_arg(., "object") %>% check_equal() check_arg(., "model") %>% check_equal() } CK Code: D2_Code_Multiple_03 require(coursekata) # preload plotly to prevent the startup messages require(plotly) # this loads plotly library(plotly) # this saves the predictions of the multivariate model fevdata$prediction <- predict(lm(FEV ~ HEIGHT + AGE, data = fevdata)) # this makes the 3d plot of model plot_ly(fevdata, x = ~AGE, y = ~HEIGHT, z = ~prediction, type = "scatter3d", mode = "markers", color = ~AGE, size = 1) # this loads plotly library(plotly) # this saves the predictions of the multivariate model fevdata\$prediction <- predict(lm(FEV ~ HEIGHT + AGE, data = fevdata)) # this makes the 3d plot of model plot_ly(fevdata, x = ~AGE, y = ~HEIGHT, z = ~prediction, type = "scatter3d", mode = "markers", color = ~AGE, size = 1) ex() %>% check_error()
CK Code: D2_Code_Multiple_04
require(coursekata) # saves the multivariate model multi_model <- lm(FEV ~ HEIGHT + AGE, data = fevdata) # write code to produce the ANOVA table # saves the multivariate model multi_model <- lm(FEV ~ HEIGHT + AGE, data = fevdata) # write code to produce the ANOVA table supernova(multi_model) ex() %>% check_function("supernova") %>% check_result() %>% check_equal()
CK Code: D2_Code_Unpacking_01