```{r setup, include=F} #knitr::opts_chunk$set(eval = FALSE) library(tidyverse) library(dplyr) library(ggplot2) library(learnr) library(gradethis) #remotes::install_github("rstudio/gradethis") library(learnrhash) #devtools::install_github("rundel/learnrhash") evals <- read.csv(url("https://raw.githubusercontent.com/IntroToStatNCAT/RA_LinearRegression/main/evals.csv")) #knitr::opts_chunk$set(eval = FALSE) ``` ## Linear Regression If you've never coded before (or even if you have), type `print("Your Name")` in the interactive R chunk below and run it by hitting `crtl+Enter` or `cmd+Enter` for MAC users. ```{r Student-Name, exercise = TRUE} ``` ### In this tutorial, we consider an advanced technique for building a statistical model which can be used to explain phenomena, understand relationships, and predict outcomes. If you think about the inference portion of applied statistics so far, we've discussed how to conduct inference on a single numerical or categorical variable and then moved on to discuss inference on a numerical or categorical variable with an additional grouping variable. In the latter cases, the grouping variable needed to be a categorical variable (gender, automatic or manual transmission, type of diet eaten, etc.) -- the questions we asked and answered were of the form, given observations which differ by the categorical variable $X$, do they differ in another variable of interest $Y$? ### Another way of phrasing this is that our inference applications seek to answer the question, "*within our population of interest, is there an association between the variable $X$ and the variable $Y$?*". If both variables $X$ and $Y$ are numerical, then we develop a new statistical inference technique called linear regression. ## An Introduction to Linear Regression Let's check in with a few short videos from our friends at OpenIntro.org to help develop the notion of linear regression for us. ### ### Simple linear regression uses a single numerical feature (predictor variable) to predict a numerical response. Simple linear regression uses the form of a straight line $y = mx + b$, where $m$ denotes slope of the relationship and $b$ denotes the intercept (the value of $y$ if $x$ is 0). With regression, we are fitting a straight line to data, where noise is present -- that is, the line we fit is not expected to pass through all of the data points. The form for a simple regression model is $$\displaystyle{y = \beta_0 + \beta_1x + \varepsilon}$$ ### Notice that $\beta_0$ is the intercept, $\beta_1$ is the slope, and $\varepsilon$ denotes the unexplained error (noise). We typically do not write $\varepsilon$ as part of the model, since we assume that it is random noise with a mean of $0$ and a constant standard deviation ($\sigma$) -- in our earlier notation, we assume $\varepsilon \sim N\left(\mu = 0, \sigma\right)$. Instead, we often write the regression model as $$\displaystyle{\mathbb{E}\left[y\right] = \beta_0 + \beta_1x}$$ ### We can use regression models to predict an expected average response ($y$) for a given value of the feature $x$. Regression models are only as good as the data they are trained on. Typically we can feel comfortable using a regression model to interpolate predictions (make predictions within the range of observed feature values) but not for extrapolation -- making predictions for values of the predictor variable outside of the range of its observed values. ### Let's see regression in action as we consider an application to understanding biases in course evaluations. ## The Data Many college courses conclude by giving students the opportunity to evaluate the course and the instructor anonymously. However, the use of these student evaluations as an indicator of course quality and teaching effectiveness is often criticized because these measures may reflect the influence of non-teaching related characteristics, such as the physical appearance of the instructor. The article titled, "Beauty in the classroom: instructors' pulchritude and putative pedagogical productivity" (Hamermesh and Parker, 2005) found that instructors who are viewed to be better looking receive higher instructional ratings. (Daniel S. Hamermesh, Amy Parker, Beauty in the classroom: instructors pulchritude and putative pedagogical productivity, *Economics of Education Review*, Volume 24, Issue 4, August 2005, Pages 369-376, ISSN 0272-7757, 10.1016/j.econedurev.2004.07.013. [http://www.sciencedirect.com/science/article/pii/S0272775704001165](http://www.sciencedirect.com/science/article/pii/S0272775704001165).) In this lab we will analyze the data from this study in order to learn what goes into a positive professor evaluation. ### The data were gathered from end of semester student evaluations for a large sample of professors from the University of Texas at Austin. In addition, six students rated the professors' physical appearance. (This is a slightly modified version of the original data set that was released as part of the replication data for *Data Analysis Using Regression and Multilevel/Hierarchical Models* (Gelman and Hill, 2007).) The result is a data frame where each row contains a different course and columns represent variables about the courses and professors. variable | description ---------------- | -------------------------------------- `score` | average professor evaluation score: (1) very unsatisfactory - (5) excellent. `rank` | rank of professor: teaching, tenure track, tenured. `ethnicity` | ethnicity of professor: not minority, minority. `gender` | gender of professor: female, male. `language` | language of school where professor received education: english or non-english. `age` | age of professor. `cls_perc_eval` | percent of students in class who completed evaluation. `cls_did_eval` | number of students in class who completed evaluation. `cls_students` | total number of students in class. `cls_level` | class level: lower, upper. `cls_profs` | number of professors teaching sections in course in sample: single, multiple. `cls_credits` | number of credits of class: one credit (lab, PE, etc.), multi credit. `bty_f1lower` | beauty rating of professor from lower level female: (1) lowest - (10) highest. `bty_f1upper` | beauty rating of professor from upper level female: (1) lowest - (10) highest. `bty_f2upper` | beauty rating of professor from second upper level female: (1) lowest - (10) highest. `bty_m1lower` | beauty rating of professor from lower level male: (1) lowest - (10) highest. `bty_m1upper` | beauty rating of professor from upper level male: (1) lowest - (10) highest. `bty_m2upper` | beauty rating of professor from second upper level male: (1) lowest - (10) highest. `bty_avg` | average beauty rating of professor. `pic_outfit` | outfit of professor in picture: not formal, formal. `pic_color` | color of professor's picture: color, black & white. ### ```{r understand-study, echo = FALSE} quiz( question_radio( "What is the difference between an observational study and an experiment?", answer("An experiment includes randomization and a manipulated condition to be tested while an observational study lacks manipulation.", correct = TRUE), answer("An experiment must be conducted in a lab"), answer("Participants in an experiment must sign a waiver"), answer("An experiment includes much more data"), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "Is this an observational study or an experiment?", answer("an observational study", correct = TRUE), answer("an experiment"), allow_retry = TRUE ), question_radio( "The original research question posed in the paper is whether beauty leads directly to the differences in course evaluations. Given the study design, is it possible to answer this question as it is phrased?", answer("Yes, it is possible to answer the question as phrased."), answer("No. The original question invokes a causal relationship -- answering the question as it was originally phrased requires data collected via an experiment.", correct = TRUE), answer("No, the study did not include enough data in order to answer the question as it was originally phrased."), answer("No. The question posed is not a *data* question."), allow_retry = TRUE, random_answer_order = TRUE ) ) ``` ### Use the code block below to draw a histogram of the distribution of the `score` variable in the `evals` data frame. Use your histogram to answer the questions that follow. ```{r hist-scores, exercise = TRUE} ``` ```{r describe-scores, echo = FALSE} quiz( question_radio( "Describe the distribution of `score`.", answer("The distribution is approximately normal."), answer("The distribution is approximately uniform."), answer("The distribution is bimodal."), answer("The distribution is skewed left.", correct = TRUE), answer("The distribution is skewed right."), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "What does this tell you about how students typically rate courses?", answer("Students give high scores more often than low scores.", correct = TRUE), answer("Students give low scores more often than high scores."), answer("Students typically give moderate scores with fewer students giving very high or very low scores."), allow_retry = TRUE, random_answer_order = TRUE ) ) ``` ## Simple Linear Regression: The fundamental phenomenon suggested by the Gelman and Hill study is that better looking teachers are evaluated more favorably. Use the code block below to create a scatterplot to see if this appears to be the case. The dataset is called `evals`. Put `bty_avg` along the horizontal axis and `score` along the vertical axis. The code should follow the setup as shown below: ```{r bty-score-setup, eval = F, exercise = TRUE} dataset%>% ggplot(aes(x = _______, y = _______))+ geom_type() ``` ```{r bty-score, exercise = TRUE} ``` ### ```{r bty-score-solution} evals%>% ggplot(aes(x = bty_avg, y = score))+ geom_point() ``` ```{r bty-score-check} grade_code() ``` ### Before we draw conclusions about the trend, compare the number of observations in the data frame with the approximate number of points on the scatterplot. Is anything awry? Use the code block below to replot the scatterplot, but this time use `geom_jitter()` instead of `geom_point()`. ```{r bty-score-jitter, exercise = TRUE} ``` ```{r bty-score-jitter-solution} evals%>% ggplot(aes(x = bty_avg, y = score))+ geom_jitter() ``` ```{r bty-score-jitter-check} grade_code() ``` ### Let's see if the apparent trend in the plot is something more than natural variation. Use the code block below to fit a linear model called `m_bty` to predict average professor score by average beauty rating. In R, we use the `lm()` function to build a linear regression model and then the `summary()` function to view it. The code is provided -- you can just run it. ```{r simple-reg-model, exercise = TRUE, exercise.eval = FALSE} m_bty <- lm(score ~ bty_avg, data = evals) summary(m_bty) ``` ### ```{r interp-reg, echo = FALSE} quiz( question_radio( "Is average beauty score a *statistically* significant predictor of overall instructor evaluation score?", answer("Yes, the $p$-value associated with `bty_avg` is below 0.05.", correct = TRUE), answer("Yes, the coefficient estimate is not 0."), answer("No, the R-squared values are quite small."), answer("No, the $p$-value associated with the intercept is smaller than the $p$-value associated with `bty_avg`."), answer("No, the $p$-value associated with `bty_avg` is above 0.05."), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "What is the approximate value of the `intercept` associated with the linear model we created?", answer("0"), answer("0.067"), answer("3.88", correct = TRUE), answer("2e-16"), answer("0.076"), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "What is the *slope* of the regression model with respect to average beauty rating?", answer("0.067", correct = TRUE), answer("3.88"), answer("5.05e-05"), answer("2e-16"), answer("0.5348"), allow_retry = TRUE ) ) ``` ### We add a line to your plot by adding a layer with `geom_abline()` using the appropriate `slope` and `intercept` arguments. ```{r add-abline} evals%>% ggplot(aes(x = bty_avg, y = score))+ geom_point()+ geom_abline(intercept = 3.88, slope = 0.067) ``` ### ```{r practical-predictor, echo = FALSE} question_radio( "From the plot, doeas it seem that average beauty score is a *practically* significant predictor of overall instructor evaluation score?", answer("No. We should not expect that the model using `bty_avg` to predict overall evaluation score will yield accurate pedictions of these scores.", correct = TRUE), answer("Yes. There is no difference between statistical and practical significance."), answer("Yes. Students give better scores to instructors who are better looking."), answer("Yes. Students give worse scores to instructors who are better looking."), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sig-not-pract, echo = FALSE} question_radio( "What does it mean that average beauty rating is a significant predictor of overall course rating score but not a practical predictor?", answer("The regression model suggests that there is implicit bias in the course evaluation scores depending on the attractiveness of the instructor.", correct = TRUE), answer("The regression model resulted in a Type I error, claiming a significant relationship where there was none."), answer("The significant relationship between beauty rating and overall evaluation score is a result of noise in the sample data."), answer("The attractiveness of an instructor is a driving force in the way students rate their courses."), allow_retry = TRUE, random_answer_order = TRUE ) ``` ## Submit ```{r context="server"} learnrhash::encoder_logic(strip_output = TRUE) ``` ```{r encode, echo=FALSE} learnrhash::encoder_ui( ui_before = shiny::div( "If you have completed this tutorial and are happy with all of your", "solutions, please click the button below to generate your hash and", "submit it using the corresponding tutorial assignment tab on Blackboard", shiny::tags$br() ), ui_after = shiny::tags$a(href = "https://blackboard.ncat.edu/", "NCAT Blackboard") ) ```