library(learnr) knitr::opts_chunk$set(echo = FALSE) knitr::opts_chunk$set(error = TRUE)
Suppose you are a high school counselor trying to predict student GPAs as a function of other student characteristics.
We will use the High School Longitudinal Data to answer these questions.
First, load the data and packages. You will need ds, tidyverse, yardstick, and the hsls_clean data.
library(ds) library(tidyverse) library(yardstick) data("hsls_clean")
If the only information we had about students was their GPA, our best guess for a given student's GPA would be the school's average GPA.
Calculate the average GPA, use the mutate command to make a new variable for the prediction:
#Fill in the blanks with the functions to make a new variable that is the mean of GPA hsls_clean<-hsls_clean%>% _____(mean_gpa_uncond=_____(gpa,na.rm=TRUE)) #add the ls function to see that the new variable has been created ls(hsls_clean)
#Solution hsls_clean<-hsls_clean%>% mutate(mean_gpa_uncond=mean(gpa,na.rm=TRUE)) ls(hsls_clean)
How far off on average is the mean from a given student's actual GPA? (Note: I've already copied the code from the prior exercise, add your new code after)
hsls_clean<-hsls_clean%>% mutate(mean_gpa_uncond=mean(gpa,na.rm=TRUE)) #Add code here:
hsls_clean<-hsls_clean%>% mutate(mean_gpa_uncond=mean(gpa,na.rm=TRUE)) #Add code here: #Calculate the RMSE
hsls_clean<-hsls_clean%>% mutate(mean_gpa_uncond=mean(gpa,na.rm=TRUE)) #Use the RMSE function rmse_uncond<-hsls_clean%>%____(gpa,mean_gpa_uncond) rmse_uncond
#Solution hsls_clean<-hsls_clean%>% mutate(mean_gpa_uncond=mean(gpa,na.rm=TRUE)) rmse_uncond<-hsls_clean%>%rmse(gpa,mean_gpa_uncond) rmse_uncond
We think that there may be a relationship between a student's GPA and if they have taken AP or IB classes. Maybe these students have lower GPAs because these classes are graded more rigorously. Or maybe these students have higher GPAs because students are only allowed to take AP/IB if they have a high GPA to begin with. Generate a table of the conditional mean of the outcome variable by levels of the predictor variable.
#Use the group_by, summarize, and mean functions #GPA is the outcome, AP/IB is the predictor variable
#Use the group_by, summarize, and mean functions #GPA is the outcome, AP/IB is the predictor variable hsls_clean%>% _____(ap_ib_credit)%>% _____(mean_gpa_ap=_____(gpa,na.rm=TRUE))%>% arrange(ap_ib_credit)
#Solution hsls_clean%>% group_by(ap_ib_credit)%>% summarize(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% arrange(ap_ib_credit)
Is the conditional mean a better predictor of GPA than the unconditional mean? First, make a prediction for GPA at each level of AP/IB and add that to your dataset.
#Group variables by AP/IB credit levels and use the mutate function
#Group variables by AP/IB credit levels and use the mutate function hsls_clean<-hsls_clean%>% _____(ap_ib_credit)%>% _____(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() #add the list function to see that the new variable has been created ls(hsls_clean)
#Solution hsls_clean<-hsls_clean%>% group_by(ap_ib_credit)%>% mutate(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() ls(hsls_clean)
Second, compare your RMSEs. (Note: I've already copied the code from the prior exercise, add your new code after)
hsls_clean<-hsls_clean%>% group_by(ap_ib_credit)%>% mutate(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() #Add code below
hsls_clean<-hsls_clean%>% group_by(ap_ib_credit)%>% mutate(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() #Add code below #Use the RMSE function
hsls_clean<-hsls_clean%>% group_by(ap_ib_credit)%>% mutate(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() #Add code below #Use the RMSE function rmse_ap<-hsls_clean%>%____(gpa,mean_gpa_ap) rmse_ap
#Solution hsls_clean<-hsls_clean%>% group_by(ap_ib_credit)%>% mutate(mean_gpa_ap=mean(gpa,na.rm=TRUE))%>% ungroup() rmse_ap<-hsls_clean%>%rmse(gpa,mean_gpa_ap) rmse_ap
question("Is the conditional or unconditional mean a better predictor of student GPA in this case?", answer("conditional", correct = TRUE), answer("unconditional") )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.