Welcome to your first LASER badge! This LASER Orientation Badge is really a warm-up activitiy to introduce you to R Markdown and the coding case studies that we will be using in the machine learning, network analysis, and text mining labs. It is a chance to become familiar with how RStudio and R Markdown works.
You may have used R before-or you may not have! Either is fine as this task will be designed with the assumption that you have not used R before. It includes “reaches” for anyone who may want to do a bit more.
In the context of doing so, we’ll focus on the following tasks:
You may be wondering what these bolded terms above refer to; what’s so special about preparing, wrangling, exploring, and modeling data - and communicating results? We’re using these terms as a part of a framework, or model, for what we mean by doing learning in STEM education research.
The particular framework we are using comes from the work of Krumm et al.’s Learning Analytics Goes to School. You can check that out, but don’t feel any need to dive deep for now - we’ll be spending more time on this in first day of the summer institute. For now, know that this document is organized around three of the five components of what we’re referring to as the LASER cycle.
Click the green arrow to the right of the “code chunk” below to view the image (more on that process of clicking the green arrow and what it does, too, in a moment)!
knitr::include_graphics("img/laser-cycle.png")
This is an R Markdown file as indicated by the .rmd extension at the end of the file name. R Markdown documents are fully reproducible and use a productive notebook interface to combine narrative text and “chunks” of code to produce a range of formatted outputs including: formats including HTML, PDF, MS Word, Beamer, HTML5 slides, Tufte-style handouts, books, dashboards, shiny applications, scientific articles, websites, and more.
There are two keys to your use of R Markdown for this activity:
Let’s get started! We are glad you are here and to begin this exciting (and challenging) journey together.
By preparing, we refer to developing a question or purpose for the analysis, which you likely know from your research can be difficult! This part of the process also involves developing an understanding of the data and what you may need to analyze the data. This often involves looking at the data and its documentation. For now, we’ll focus on just a few parts of this process, diving in much more deeply over the coming weeks.
R uses “packages,” add-ons that enhance its functionality. One package that we’ll be using is the tidyverse. To load the tidyverse, click the green arrow in the right corner of the block-or “chunk”-of code that follows.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.1.2 ✓ dplyr 1.0.6
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Please do not worry if you saw a number of messages: those probably mean that the tidyverse loaded just fine. If you see an error, though, try to interpret or search via your search engine the contents of the error, or reach out to us for assistance.
Next, we’ll load data-specifically, a CSV file, the kind that you can export from Microsoft Excel or Google Sheets - into R, using the read_csv() function in the next chunk.
Clicking the green arrow runs the code; do that next to read the sci-online-classes.csv file stored in your data folder into your R environment:
d <- read_csv("data/sci-online-classes.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double(),
## course_id = col_character(),
## subject = col_character(),
## semester = col_character(),
## section = col_character(),
## Gradebook_Item = col_character(),
## Grade_Category = col_logical(),
## Gender = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
Nice work! You should now see a new data “object” named d saved in your Environment pane. Try clicking on it and see what happens.
Now let’s learn another way to inspect our data. Run the next chunk and look at the results, tabbing left or right with the arrows, or scanning through the rows by clicking the numbers at the bottom of the pane with the print-out of the data you loaded:
d
## # A tibble: 603 x 30
## student_id course_id total_points_possi… total_points_ear… percentage_earn…
## <dbl> <chr> <dbl> <dbl> <dbl>
## 1 43146 FrScA-S216… 3280 2220 0.677
## 2 44638 OcnA-S116-… 3531 2672 0.757
## 3 47448 FrScA-S216… 2870 1897 0.661
## 4 47979 OcnA-S216-… 4562 3090 0.677
## 5 48797 PhysA-S116… 2207 1910 0.865
## 6 51943 FrScA-S216… 4208 3596 0.855
## 7 52326 AnPhA-S216… 4325 2255 0.521
## 8 52446 PhysA-S116… 2086 1719 0.824
## 9 53447 FrScA-S116… 4655 3149 0.676
## 10 53475 FrScA-S116… 1710 1402 0.820
## # … with 593 more rows, and 25 more variables: subject <chr>, semester <chr>,
## # section <chr>, Gradebook_Item <chr>, Grade_Category <lgl>,
## # FinalGradeCEMS <dbl>, Points_Possible <dbl>, Points_Earned <dbl>,
## # Gender <chr>, q1 <dbl>, q2 <dbl>, q3 <dbl>, q4 <dbl>, q5 <dbl>, q6 <dbl>,
## # q7 <dbl>, q8 <dbl>, q9 <dbl>, q10 <dbl>, TimeSpent <dbl>,
## # TimeSpent_hours <dbl>, TimeSpent_std <dbl>, int <dbl>, pc <dbl>, uv <dbl>
str(d)
## spec_tbl_df [603 × 30] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ student_id : num [1:603] 43146 44638 47448 47979 48797 ...
## $ course_id : chr [1:603] "FrScA-S216-02" "OcnA-S116-01" "FrScA-S216-01" "OcnA-S216-01" ...
## $ total_points_possible: num [1:603] 3280 3531 2870 4562 2207 ...
## $ total_points_earned : num [1:603] 2220 2672 1897 3090 1910 ...
## $ percentage_earned : num [1:603] 0.677 0.757 0.661 0.677 0.865 ...
## $ subject : chr [1:603] "FrScA" "OcnA" "FrScA" "OcnA" ...
## $ semester : chr [1:603] "S216" "S116" "S216" "S216" ...
## $ section : chr [1:603] "02" "01" "01" "01" ...
## $ Gradebook_Item : chr [1:603] "POINTS EARNED & TOTAL COURSE POINTS" "ATTEMPTED" "POINTS EARNED & TOTAL COURSE POINTS" "POINTS EARNED & TOTAL COURSE POINTS" ...
## $ Grade_Category : logi [1:603] NA NA NA NA NA NA ...
## $ FinalGradeCEMS : num [1:603] 93.5 81.7 88.5 81.9 84 ...
## $ Points_Possible : num [1:603] 5 10 10 5 438 5 10 10 443 5 ...
## $ Points_Earned : num [1:603] NA 10 NA 4 399 NA NA 10 425 2.5 ...
## $ Gender : chr [1:603] "M" "F" "M" "M" ...
## $ q1 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q2 : num [1:603] 4 4 4 5 3 NA 5 3 3 NA ...
## $ q3 : num [1:603] 4 3 4 3 3 NA 3 3 3 NA ...
## $ q4 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q5 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q6 : num [1:603] 5 4 4 5 4 NA 5 4 3 NA ...
## $ q7 : num [1:603] 5 4 4 4 4 NA 4 3 3 NA ...
## $ q8 : num [1:603] 5 5 5 5 4 NA 5 3 4 NA ...
## $ q9 : num [1:603] 4 4 3 5 NA NA 5 3 2 NA ...
## $ q10 : num [1:603] 5 4 5 5 3 NA 5 3 5 NA ...
## $ TimeSpent : num [1:603] 1555 1383 860 1599 1482 ...
## $ TimeSpent_hours : num [1:603] 25.9 23 14.3 26.6 24.7 ...
## $ TimeSpent_std : num [1:603] -0.181 -0.308 -0.693 -0.148 -0.235 ...
## $ int : num [1:603] 5 4.2 5 5 3.8 4.6 5 3 4.2 NA ...
## $ pc : num [1:603] 4.5 3.5 4 3.5 3.5 4 3.5 3 3 NA ...
## $ uv : num [1:603] 4.33 4 3.67 5 3.5 ...
## - attr(*, "spec")=
## .. cols(
## .. student_id = col_double(),
## .. course_id = col_character(),
## .. total_points_possible = col_double(),
## .. total_points_earned = col_double(),
## .. percentage_earned = col_double(),
## .. subject = col_character(),
## .. semester = col_character(),
## .. section = col_character(),
## .. Gradebook_Item = col_character(),
## .. Grade_Category = col_logical(),
## .. FinalGradeCEMS = col_double(),
## .. Points_Possible = col_double(),
## .. Points_Earned = col_double(),
## .. Gender = col_character(),
## .. q1 = col_double(),
## .. q2 = col_double(),
## .. q3 = col_double(),
## .. q4 = col_double(),
## .. q5 = col_double(),
## .. q6 = col_double(),
## .. q7 = col_double(),
## .. q8 = col_double(),
## .. q9 = col_double(),
## .. q10 = col_double(),
## .. TimeSpent = col_double(),
## .. TimeSpent_hours = col_double(),
## .. TimeSpent_std = col_double(),
## .. int = col_double(),
## .. pc = col_double(),
## .. uv = col_double()
## .. )
What do you notice about this data set? What do you wonder? Add one-two thoughts following the dashes next (you can add additional dashes if you like!):
There are other ways to inspect your data; the glimpse() function provides one such way. Run the code below to take a glimpse at your data.
glimpse(d)
## Rows: 603
## Columns: 30
## $ student_id <dbl> 43146, 44638, 47448, 47979, 48797, 51943, 52326,…
## $ course_id <chr> "FrScA-S216-02", "OcnA-S116-01", "FrScA-S216-01"…
## $ total_points_possible <dbl> 3280, 3531, 2870, 4562, 2207, 4208, 4325, 2086, …
## $ total_points_earned <dbl> 2220, 2672, 1897, 3090, 1910, 3596, 2255, 1719, …
## $ percentage_earned <dbl> 0.6768293, 0.7567261, 0.6609756, 0.6773345, 0.86…
## $ subject <chr> "FrScA", "OcnA", "FrScA", "OcnA", "PhysA", "FrSc…
## $ semester <chr> "S216", "S116", "S216", "S216", "S116", "S216", …
## $ section <chr> "02", "01", "01", "01", "01", "03", "01", "01", …
## $ Gradebook_Item <chr> "POINTS EARNED & TOTAL COURSE POINTS", "ATTEMPTE…
## $ Grade_Category <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ FinalGradeCEMS <dbl> 93.45372, 81.70184, 88.48758, 81.85260, 84.00000…
## $ Points_Possible <dbl> 5, 10, 10, 5, 438, 5, 10, 10, 443, 5, 12, 10, 5,…
## $ Points_Earned <dbl> NA, 10.00, NA, 4.00, 399.00, NA, NA, 10.00, 425.…
## $ Gender <chr> "M", "F", "M", "M", "F", "F", "M", "F", "F", "M"…
## $ q1 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 4, 3, 5, NA,…
## $ q2 <dbl> 4, 4, 4, 5, 3, NA, 5, 3, 3, NA, NA, 5, 3, 3, NA,…
## $ q3 <dbl> 4, 3, 4, 3, 3, NA, 3, 3, 3, NA, NA, 3, 3, 5, NA,…
## $ q4 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 5, 3, 5, NA,…
## $ q5 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 5, 4, 5, NA,…
## $ q6 <dbl> 5, 4, 4, 5, 4, NA, 5, 4, 3, NA, NA, 5, 3, 5, NA,…
## $ q7 <dbl> 5, 4, 4, 4, 4, NA, 4, 3, 3, NA, NA, 5, 3, 5, NA,…
## $ q8 <dbl> 5, 5, 5, 5, 4, NA, 5, 3, 4, NA, NA, 4, 3, 5, NA,…
## $ q9 <dbl> 4, 4, 3, 5, NA, NA, 5, 3, 2, NA, NA, 5, 2, 2, NA…
## $ q10 <dbl> 5, 4, 5, 5, 3, NA, 5, 3, 5, NA, NA, 4, 4, 5, NA,…
## $ TimeSpent <dbl> 1555.1667, 1382.7001, 860.4335, 1598.6166, 1481.…
## $ TimeSpent_hours <dbl> 25.91944500, 23.04500167, 14.34055833, 26.643610…
## $ TimeSpent_std <dbl> -0.18051496, -0.30780313, -0.69325954, -0.148446…
## $ int <dbl> 5.0, 4.2, 5.0, 5.0, 3.8, 4.6, 5.0, 3.0, 4.2, NA,…
## $ pc <dbl> 4.50, 3.50, 4.00, 3.50, 3.50, 4.00, 3.50, 3.00, …
## $ uv <dbl> 4.333333, 4.000000, 3.666667, 5.000000, 3.500000…
We have one more question to pose to you: What do rows and columns typically represent in your area of work and/or research?
Generally, rows typically represent “cases,” the units that we measure, or the units on which we collect data. This is not a trick question! What counts as a “case” (and therefore what is represented as a row) varies by (and within) fields. There may be multiple types or levels of units studied in your field; listing more than one is fine! Also, please consider what columns - which usually represent variables - represent in your area of work and/or research.
What rows typically (or you think may) represent:
What columns typically (or you think may) represent:
Next, we’ll use a few functions that are handy for preparing data in table form.
By wrangle, we refer to the process of cleaning and processing data, and, in cases, merging (or joining) data from multiple sources. Often, this part of the process is very (surprisingly) time-intensive. Wrangling your data into shape can itself be an important accomplishment! There are great tools in R to do this, especially through the use of the {dplyr} R package.
Let’s select only a few variables.
library(dplyr)
d %>%
select(student_id, total_points_possible, total_points_earned)
## # A tibble: 603 x 3
## student_id total_points_possible total_points_earned
## <dbl> <dbl> <dbl>
## 1 43146 3280 2220
## 2 44638 3531 2672
## 3 47448 2870 1897
## 4 47979 4562 3090
## 5 48797 2207 1910
## 6 51943 4208 3596
## 7 52326 4325 2255
## 8 52446 2086 1719
## 9 53447 4655 3149
## 10 53475 1710 1402
## # … with 593 more rows
Notice how the number of columns (variables) is now different.
Let’s include one additional variable in your select function.
First, we need to figure out what variables exist in our dataset (or be reminded of this - it’s very common in R to be continually checking and inspecting your data)!
You can use a function named glimpse() to do this.
glimpse(d)
## Rows: 603
## Columns: 30
## $ student_id <dbl> 43146, 44638, 47448, 47979, 48797, 51943, 52326,…
## $ course_id <chr> "FrScA-S216-02", "OcnA-S116-01", "FrScA-S216-01"…
## $ total_points_possible <dbl> 3280, 3531, 2870, 4562, 2207, 4208, 4325, 2086, …
## $ total_points_earned <dbl> 2220, 2672, 1897, 3090, 1910, 3596, 2255, 1719, …
## $ percentage_earned <dbl> 0.6768293, 0.7567261, 0.6609756, 0.6773345, 0.86…
## $ subject <chr> "FrScA", "OcnA", "FrScA", "OcnA", "PhysA", "FrSc…
## $ semester <chr> "S216", "S116", "S216", "S216", "S116", "S216", …
## $ section <chr> "02", "01", "01", "01", "01", "03", "01", "01", …
## $ Gradebook_Item <chr> "POINTS EARNED & TOTAL COURSE POINTS", "ATTEMPTE…
## $ Grade_Category <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ FinalGradeCEMS <dbl> 93.45372, 81.70184, 88.48758, 81.85260, 84.00000…
## $ Points_Possible <dbl> 5, 10, 10, 5, 438, 5, 10, 10, 443, 5, 12, 10, 5,…
## $ Points_Earned <dbl> NA, 10.00, NA, 4.00, 399.00, NA, NA, 10.00, 425.…
## $ Gender <chr> "M", "F", "M", "M", "F", "F", "M", "F", "F", "M"…
## $ q1 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 4, 3, 5, NA,…
## $ q2 <dbl> 4, 4, 4, 5, 3, NA, 5, 3, 3, NA, NA, 5, 3, 3, NA,…
## $ q3 <dbl> 4, 3, 4, 3, 3, NA, 3, 3, 3, NA, NA, 3, 3, 5, NA,…
## $ q4 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 5, 3, 5, NA,…
## $ q5 <dbl> 5, 4, 5, 5, 4, NA, 5, 3, 4, NA, NA, 5, 4, 5, NA,…
## $ q6 <dbl> 5, 4, 4, 5, 4, NA, 5, 4, 3, NA, NA, 5, 3, 5, NA,…
## $ q7 <dbl> 5, 4, 4, 4, 4, NA, 4, 3, 3, NA, NA, 5, 3, 5, NA,…
## $ q8 <dbl> 5, 5, 5, 5, 4, NA, 5, 3, 4, NA, NA, 4, 3, 5, NA,…
## $ q9 <dbl> 4, 4, 3, 5, NA, NA, 5, 3, 2, NA, NA, 5, 2, 2, NA…
## $ q10 <dbl> 5, 4, 5, 5, 3, NA, 5, 3, 5, NA, NA, 4, 4, 5, NA,…
## $ TimeSpent <dbl> 1555.1667, 1382.7001, 860.4335, 1598.6166, 1481.…
## $ TimeSpent_hours <dbl> 25.91944500, 23.04500167, 14.34055833, 26.643610…
## $ TimeSpent_std <dbl> -0.18051496, -0.30780313, -0.69325954, -0.148446…
## $ int <dbl> 5.0, 4.2, 5.0, 5.0, 3.8, 4.6, 5.0, 3.0, 4.2, NA,…
## $ pc <dbl> 4.50, 3.50, 4.00, 3.50, 3.50, 4.00, 3.50, 3.00, …
## $ uv <dbl> 4.333333, 4.000000, 3.666667, 5.000000, 3.500000…
In the code chunk below, add a new variable to the code below, being careful to type the new variable name as it appears in the data. We’ve added some code to get you started. Consider how the names of the other variables are separated as you think about how to add an additional variable to this code.
d %>%
select(student_id, total_points_possible, total_points_earned, pc)
## # A tibble: 603 x 4
## student_id total_points_possible total_points_earned pc
## <dbl> <dbl> <dbl> <dbl>
## 1 43146 3280 2220 4.5
## 2 44638 3531 2672 3.5
## 3 47448 2870 1897 4
## 4 47979 4562 3090 3.5
## 5 48797 2207 1910 3.5
## 6 51943 4208 3596 4
## 7 52326 4325 2255 3.5
## 8 52446 2086 1719 3
## 9 53447 4655 3149 3
## 10 53475 1710 1402 NA
## # … with 593 more rows
Once added, the output should be different than in the code above - there should now be an additional variable included in the print-out.
Next, let’s explore filtering variables. Check out and run the next chunk of code, imagining that we wish to filter our data to view only the rows associated with students who earned a final grade (as a percentage) of 70 - 70% - or higher.
d %>%
filter(FinalGradeCEMS > 70)
## # A tibble: 438 x 30
## student_id course_id total_points_possi… total_points_ear… percentage_earn…
## <dbl> <chr> <dbl> <dbl> <dbl>
## 1 43146 FrScA-S216… 3280 2220 0.677
## 2 44638 OcnA-S116-… 3531 2672 0.757
## 3 47448 FrScA-S216… 2870 1897 0.661
## 4 47979 OcnA-S216-… 4562 3090 0.677
## 5 48797 PhysA-S116… 2207 1910 0.865
## 6 52326 AnPhA-S216… 4325 2255 0.521
## 7 52446 PhysA-S116… 2086 1719 0.824
## 8 53447 FrScA-S116… 4655 3149 0.676
## 9 53475 FrScA-S216… 1209 977 0.808
## 10 54066 OcnA-S116-… 4641 3429 0.739
## # … with 428 more rows, and 25 more variables: subject <chr>, semester <chr>,
## # section <chr>, Gradebook_Item <chr>, Grade_Category <lgl>,
## # FinalGradeCEMS <dbl>, Points_Possible <dbl>, Points_Earned <dbl>,
## # Gender <chr>, q1 <dbl>, q2 <dbl>, q3 <dbl>, q4 <dbl>, q5 <dbl>, q6 <dbl>,
## # q7 <dbl>, q8 <dbl>, q9 <dbl>, q10 <dbl>, TimeSpent <dbl>,
## # TimeSpent_hours <dbl>, TimeSpent_std <dbl>, int <dbl>, pc <dbl>, uv <dbl>
In the next code chunk, change the cut-off from 70% to some other value - larger or smaller (maybe much larger or smaller - feel free to play around with the code a bit!).
d %>%
filter(FinalGradeCEMS <= 20)
## # A tibble: 30 x 30
## student_id course_id total_points_possi… total_points_ear… percentage_earn…
## <dbl> <chr> <dbl> <dbl> <dbl>
## 1 71415 FrScA-S216… 3284 2574 0.784
## 2 85323 FrScA-S116… 4000 3136 0.784
## 3 85355 OcnA-S116-… 3173 2687 0.847
## 4 85390 PhysA-S116… 5246 4318 0.823
## 5 85889 BioA-S116-… 3838 2248 0.586
## 6 86588 AnPhA-S116… 4024 2812 0.699
## 7 87081 BioA-S116-… 4254 3315 0.779
## 8 87122 OcnA-S116-… 9066 5414 0.597
## 9 88874 FrScA-S116… 3370 2770 0.822
## 10 90995 AnPhA-S116… 3516 2878 0.819
## # … with 20 more rows, and 25 more variables: subject <chr>, semester <chr>,
## # section <chr>, Gradebook_Item <chr>, Grade_Category <lgl>,
## # FinalGradeCEMS <dbl>, Points_Possible <dbl>, Points_Earned <dbl>,
## # Gender <chr>, q1 <dbl>, q2 <dbl>, q3 <dbl>, q4 <dbl>, q5 <dbl>, q6 <dbl>,
## # q7 <dbl>, q8 <dbl>, q9 <dbl>, q10 <dbl>, TimeSpent <dbl>,
## # TimeSpent_hours <dbl>, TimeSpent_std <dbl>, int <dbl>, pc <dbl>, uv <dbl>
What happens when you change the cut-off from 70 to something else? Add a thought (or more):
The last function we’ll use for preparing tables is arrange.
We’ll combine this arrange() function with a function we used already - select(). We do this so we can view only the student ID and their final grade.
d %>%
select(student_id, FinalGradeCEMS) %>%
arrange(FinalGradeCEMS)
## # A tibble: 603 x 2
## student_id FinalGradeCEMS
## <dbl> <dbl>
## 1 90995 0
## 2 92606 0.535
## 3 95684 0.903
## 4 90996 1.80
## 5 94876 2.93
## 6 92633 3.01
## 7 85390 3.06
## 8 94630 3.43
## 9 90995 5.04
## 10 96677 5.2
## # … with 593 more rows
Note that arrange works by sorting values in ascending order (from lowest to highest); you can change this by using the desc() function with arrange, like the following:
d %>%
select(student_id, FinalGradeCEMS) %>%
arrange(desc(FinalGradeCEMS))
## # A tibble: 603 x 2
## student_id FinalGradeCEMS
## <dbl> <dbl>
## 1 85650 100
## 2 91067 99.8
## 3 66740 99.3
## 4 86792 99.1
## 5 78153 99.0
## 6 66689 98.6
## 7 88261 98.6
## 8 92740 98.6
## 9 92726 98.2
## 10 92741 98.2
## # … with 593 more rows
In the code chunk below, replace FinalGradeCEMS that is used with both the select() and arrange() functions with a different variable in the data set. Consider returning to the code chunk above in which you glimpsed at the names of all of the variables.
d %>%
select(student_id, pc) %>%
arrange(desc(pc))
## # A tibble: 603 x 2
## student_id pc
## <dbl> <dbl>
## 1 58168 5
## 2 60186 5
## 3 70068 5
## 4 78000 5
## 5 84645 5
## 6 85482 5
## 7 85702 5
## 8 86002 5
## 9 86251 5
## 10 86365 5
## # … with 593 more rows
Can you compose a series of functions that include the select(), filter(), and arrange functions? Recall that you can “pipe” the output from one function to the next as when we used select() and arrange() together in the code chunk above.
This reach is not required/necessary to complete; it’s just for those who wish to do a bit more with these functions at this time (we’ll do more in class, too!)
...
## Error in eval(expr, envir, enclos): '...' used in an incorrect context
Exploratory data analysis, or exploring your data, involves processes of describing your data (such as by calculating the means and standard deviations of numeric variables, or counting the frequency of categorical variables) and, often, visualizing your data prior. In this section, we’ll create a few plots to explore our data.
The code below creates a histogram, or a distribution of the values, in this case for students’ final grades.
ggplot(d, aes(x = FinalGradeCEMS)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 30 rows containing non-finite values (stat_bin).
You can change the color of the histogram bars by specifying a color as follows:
ggplot(d, aes(x = FinalGradeCEMS)) +
geom_histogram(fill = "blue")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 30 rows containing non-finite values (stat_bin).
In the code chunk below, change the color to one of your choosing; consider this list of valid color names here: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
ggplot(d, aes(x = FinalGradeCEMS)) +
geom_histogram(fill = "dodgerblue")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 30 rows containing non-finite values (stat_bin).
Finally, we’ll make one more change; visualize the distribution of another variable in the data - one other than FinalGradeCEMS. You can do so by swapping out the name for another variable with FinalGradeCEMS. Also, change the color to one other than blue.
ggplot(d, aes(x = pc)) +
geom_histogram(fill = "green")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 75 rows containing non-finite values (stat_bin).
Completed the above? Nice job! Try for a “reach” by creating a scatter plot for the relationship between two variables. You will need to pass the names of two variables to the code below for what is now simply XXX (a placeholder).
str(d)
## spec_tbl_df [603 × 30] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ student_id : num [1:603] 43146 44638 47448 47979 48797 ...
## $ course_id : chr [1:603] "FrScA-S216-02" "OcnA-S116-01" "FrScA-S216-01" "OcnA-S216-01" ...
## $ total_points_possible: num [1:603] 3280 3531 2870 4562 2207 ...
## $ total_points_earned : num [1:603] 2220 2672 1897 3090 1910 ...
## $ percentage_earned : num [1:603] 0.677 0.757 0.661 0.677 0.865 ...
## $ subject : chr [1:603] "FrScA" "OcnA" "FrScA" "OcnA" ...
## $ semester : chr [1:603] "S216" "S116" "S216" "S216" ...
## $ section : chr [1:603] "02" "01" "01" "01" ...
## $ Gradebook_Item : chr [1:603] "POINTS EARNED & TOTAL COURSE POINTS" "ATTEMPTED" "POINTS EARNED & TOTAL COURSE POINTS" "POINTS EARNED & TOTAL COURSE POINTS" ...
## $ Grade_Category : logi [1:603] NA NA NA NA NA NA ...
## $ FinalGradeCEMS : num [1:603] 93.5 81.7 88.5 81.9 84 ...
## $ Points_Possible : num [1:603] 5 10 10 5 438 5 10 10 443 5 ...
## $ Points_Earned : num [1:603] NA 10 NA 4 399 NA NA 10 425 2.5 ...
## $ Gender : chr [1:603] "M" "F" "M" "M" ...
## $ q1 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q2 : num [1:603] 4 4 4 5 3 NA 5 3 3 NA ...
## $ q3 : num [1:603] 4 3 4 3 3 NA 3 3 3 NA ...
## $ q4 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q5 : num [1:603] 5 4 5 5 4 NA 5 3 4 NA ...
## $ q6 : num [1:603] 5 4 4 5 4 NA 5 4 3 NA ...
## $ q7 : num [1:603] 5 4 4 4 4 NA 4 3 3 NA ...
## $ q8 : num [1:603] 5 5 5 5 4 NA 5 3 4 NA ...
## $ q9 : num [1:603] 4 4 3 5 NA NA 5 3 2 NA ...
## $ q10 : num [1:603] 5 4 5 5 3 NA 5 3 5 NA ...
## $ TimeSpent : num [1:603] 1555 1383 860 1599 1482 ...
## $ TimeSpent_hours : num [1:603] 25.9 23 14.3 26.6 24.7 ...
## $ TimeSpent_std : num [1:603] -0.181 -0.308 -0.693 -0.148 -0.235 ...
## $ int : num [1:603] 5 4.2 5 5 3.8 4.6 5 3 4.2 NA ...
## $ pc : num [1:603] 4.5 3.5 4 3.5 3.5 4 3.5 3 3 NA ...
## $ uv : num [1:603] 4.33 4 3.67 5 3.5 ...
## - attr(*, "spec")=
## .. cols(
## .. student_id = col_double(),
## .. course_id = col_character(),
## .. total_points_possible = col_double(),
## .. total_points_earned = col_double(),
## .. percentage_earned = col_double(),
## .. subject = col_character(),
## .. semester = col_character(),
## .. section = col_character(),
## .. Gradebook_Item = col_character(),
## .. Grade_Category = col_logical(),
## .. FinalGradeCEMS = col_double(),
## .. Points_Possible = col_double(),
## .. Points_Earned = col_double(),
## .. Gender = col_character(),
## .. q1 = col_double(),
## .. q2 = col_double(),
## .. q3 = col_double(),
## .. q4 = col_double(),
## .. q5 = col_double(),
## .. q6 = col_double(),
## .. q7 = col_double(),
## .. q8 = col_double(),
## .. q9 = col_double(),
## .. q10 = col_double(),
## .. TimeSpent = col_double(),
## .. TimeSpent_hours = col_double(),
## .. TimeSpent_std = col_double(),
## .. int = col_double(),
## .. pc = col_double(),
## .. uv = col_double()
## .. )
ggplot(d, aes(x = pc, y = total_points_earned)) +
geom_point()
## Warning: Removed 75 rows containing missing values (geom_point).
“Model” is one of those terms that has many different meanings. For our purpose, we refer to the process of simplifying and summarizing our data. Thus, models can take many forms; calculating means represents a legitimate form of modeling data, as does estimating more complex models, including linear regressions, and models and algorithms associated with machine learning tasks. For now, we’ll run a linear regression to predict students’ final grades.
Below, we predict students’ final grades (FinaGradeCEMS, which is on a 0-100 point scale) on the basis of the time they spent on the course (measured through their learning management system in minutes, TimeSpent, and the subject (one of five) of their specific course.
m1 <- lm(FinalGradeCEMS ~ TimeSpent + subject, data = d)
summary(m1)
##
## Call:
## lm(formula = FinalGradeCEMS ~ TimeSpent + subject, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -70.378 -8.836 4.816 12.855 36.047
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 57.3931739 2.3382193 24.546 < 2e-16 ***
## TimeSpent 0.0071098 0.0006516 10.912 < 2e-16 ***
## subjectBioA -1.5596482 3.6053075 -0.433 0.665
## subjectFrScA 11.7306546 2.2143847 5.297 1.68e-07 ***
## subjectOcnA 1.0974545 2.5771474 0.426 0.670
## subjectPhysA 16.0357213 3.0712923 5.221 2.50e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 19.8 on 567 degrees of freedom
## (30 observations deleted due to missingness)
## Multiple R-squared: 0.213, Adjusted R-squared: 0.2061
## F-statistic: 30.69 on 5 and 567 DF, p-value: < 2.2e-16
Notice how above the variables are separated by a + symbol. Below, add another - a third - variable to the regression model. Specifically, add a variable students’ initial, self-reported interest in science, int - and any other variable(s) you like! What do you notice about the results? We’re going to dive into this much more: if you have many questions now, you’re in the right spot!
m2 <- lm(FinalGradeCEMS ~ TimeSpent + subject + pc, data = d)
summary(m2)
##
## Call:
## lm(formula = FinalGradeCEMS ~ TimeSpent + subject + pc, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -67.789 -9.282 4.481 13.214 42.651
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 66.8806101 5.7837037 11.564 < 2e-16 ***
## TimeSpent 0.0072313 0.0006955 10.398 < 2e-16 ***
## subjectBioA -1.3543995 3.9057303 -0.347 0.7289
## subjectFrScA 13.5446736 2.3958673 5.653 2.66e-08 ***
## subjectOcnA 3.8936648 2.7337716 1.424 0.1550
## subjectPhysA 17.9305330 3.2420930 5.531 5.17e-08 ***
## pc -3.2286969 1.3876604 -2.327 0.0204 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 19.82 on 497 degrees of freedom
## (99 observations deleted due to missingness)
## Multiple R-squared: 0.2368, Adjusted R-squared: 0.2276
## F-statistic: 25.7 on 6 and 497 DF, p-value: < 2.2e-16
Great job! Once you’ve finished your work, Upon doing so, you should see a new laser-orientation-badge.html.
Congratulations, you’ve completed your Models & Inference Badge! Complete the following steps to submit your work for review by
Change the name of the author: in the YAML header at the very top of this document to your name. As noted in Reproducible Research in R, The YAML header controls the style and feel for knitted document but doesn’t actually display in the final output.
Click the yarn icon above to “knit” your data product to a HTML file that will be saved in your R Project folder.
Commit your changes in GitHub Desktop and push them to your online GitHub repository.
Publish your HTML page the web using one of the following publishing methods:
Publish on RPubs by clicking the “Publish” button located in the Viewer Pane when you knit your document. Note, you will need to quickly create a RPubs account.
Publishing on GitHub using either GitHub Pages or the HTML previewer.
Post a new discussion on GitHub to our Foundations Badges forum. In your post, include a link to your published web page and a short reflection highlighting one thing you learned from this lab and one thing you’d like to explore further.