Time to start loading packages! Since this assignment mostly focuses on preparing data, I’ll use primarily the tools we’ve used in almost every assignment, while also installing the pacman package to streamline things. As I continue through this assignment, I’ll return to this section and add packages as needed (e.g. the ‘zoo’ package to carry forward values to replace the NAs in my data.)
install.packages("pacman")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library("pacman")
pacman::p_load(
tidyverse,
readr,
magrittr,
dplyr,
psych,
ggplot2,
caTools,
caret,
zoo,
lubridate
)
I’ll start by reading in the CSVs that I have - I won’t make any changes at this point, I’m primarily tinkering with how R will interpret them and what I might need to do to preprocess them. Ultimately, I want to identify what my ideal “final” dataset looks like, and then try to reverse engineer that. All of my datasets have strings that need be read as factors (e.g. different levels of trainings, purposes, courses, etc.)
PIDOperations <- read.csv("/cloud/project/Data/PIDOperations.csv", stringsAsFactors = TRUE)
PIDPurposeCount <- read.csv("/cloud/project/Data/PIDPurposeCount.csv", stringsAsFactors = TRUE)
PIDToolsCount <- read.csv("/cloud/project/Data/PIDToolsCount.csv", stringsAsFactors = TRUE)
PIDCourseData <- read.csv("/cloud/project/Data/PIDCourseData.csv", stringsAsFactors = TRUE)
head(PIDOperations, n = 10)
## PID FirstVisit LastVisit TrainingsCount VisitsCount
## 1 704221707 26/03/2023 26/04/2023 1 9
## 2 704277964 10/1/23 19/01/2023 1 3
## 3 707857617 16/03/2023 24/03/2023 1 3
## 4 710237433 7/2/23 9/5/23 2 16
## 5 711002610 21/03/2023 21/03/2023 1 1
## 6 711644898 28/03/2023 28/03/2023 1 1
## 7 711919044 21/03/2023 21/03/2023 1 1
## 8 711957114 4/4/23 4/4/23 2 2
## 9 712425751 26/01/2023 29/03/2023 5 5
## 10 714013347 21/02/2023 25/04/2023 1 9
head(PIDPurposeCount, n = 10)
## PID Purpose.Descr No.Measure.Value
## 1 704221707 Coursework 6
## 2 NA Personal 2
## 3 NA Training 1
## 4 704277964 Personal 2
## 5 NA Training 1
## 6 707857617 Personal 1
## 7 NA Training 2
## 8 710237433 Personal 14
## 9 NA Training 2
## 10 NA Workshop 1
head(PIDToolsCount, n = 10)
## PID TrainingName Count
## 1 704221707 BeAM101 5
## 2 NA Woodshop 4
## 3 704277964 3D Printer (uPrint) 2
## 4 NA BeAM101 1
## 5 707857617 Shaper Origin 1
## 6 NA BeAM101 1
## 7 NA 3DP 1
## 8 710237433 Woodshop 14
## 9 NA ShopBot 1
## 10 NA Serger 1
head(PIDCourseData, n = 10)
## PID CourseID
## 1 730325671 AMST 460H
## 2 730333649 AMST 460H
## 3 730354285 AMST 460H
## 4 730510524 AMST 460H
## 5 730565792 AMST 460H
## 6 730575185 AMST 460H
## 7 730576601 AMST 460H
## 8 730625503 AMST 460H
## 9 704221707 APPL 089
## 10 730467502 APPL 089
I’m noticing a couple of things right off the bat as I view the datasets. First, the PIDPurposeCount and PIDToolsCount have a significant number of ‘NAs’ which I believe is a symptom of the original CSV document sent to me, where an individual PID in the first column might have multiple tools/purposes listed in the second column without repeating the PID (instead, implying it through cell color coding.) To convert a “tall” dataset to a “wide” dataset, I’m going to need to replace those NAs with the correct PID. I looked online for solutions to this problem, and I believe I can use the ‘na.locf’ function from the ‘zoo’ package to replace the NAs with the last listed PID in the column. Let’s try it!
TestPIDPurposeCount <- na.locf(PIDPurposeCount, fromFirst = TRUE)
head(PIDPurposeCount, n = 10)
## PID Purpose.Descr No.Measure.Value
## 1 704221707 Coursework 6
## 2 NA Personal 2
## 3 NA Training 1
## 4 704277964 Personal 2
## 5 NA Training 1
## 6 707857617 Personal 1
## 7 NA Training 2
## 8 710237433 Personal 14
## 9 NA Training 2
## 10 NA Workshop 1
head(TestPIDPurposeCount, n = 10)
## PID Purpose.Descr No.Measure.Value
## 1 704221707 Coursework 6
## 2 704221707 Personal 2
## 3 704221707 Training 1
## 4 704277964 Personal 2
## 5 704277964 Training 1
## 6 707857617 Personal 1
## 7 707857617 Training 2
## 8 710237433 Personal 14
## 9 710237433 Training 2
## 10 710237433 Workshop 1
Looks pretty good when comparing the original to the updated - from a preliminary glance, it seems like it’s replacing the NAs with the correct PID associated with that data. Let’s go ahead and use the na.locf function to update both the PIDPurposeCount and the PIDToolsCount spreadsheets.
PIDPurposeCount <- na.locf(PIDPurposeCount, fromFirst = TRUE)
PIDToolsCount <- na.locf(PIDToolsCount, fromFirst = TRUE)
head(PIDPurposeCount, n = 10)
## PID Purpose.Descr No.Measure.Value
## 1 704221707 Coursework 6
## 2 704221707 Personal 2
## 3 704221707 Training 1
## 4 704277964 Personal 2
## 5 704277964 Training 1
## 6 707857617 Personal 1
## 7 707857617 Training 2
## 8 710237433 Personal 14
## 9 710237433 Training 2
## 10 710237433 Workshop 1
head(PIDToolsCount, n = 10)
## PID TrainingName Count
## 1 704221707 BeAM101 5
## 2 704221707 Woodshop 4
## 3 704277964 3D Printer (uPrint) 2
## 4 704277964 BeAM101 1
## 5 707857617 Shaper Origin 1
## 6 707857617 BeAM101 1
## 7 707857617 3DP 1
## 8 710237433 Woodshop 14
## 9 710237433 ShopBot 1
## 10 710237433 Serger 1
The ‘PIDOperations’ looks like the cleanest data frame, with one observation per PID (aka an individual student’s identification number.) Based on the other datasets we’ve used for assignments in this class, I think that’s where I’d like to end up - with each student having only one line of features. Contrast that with the PIDPurposeCount, PIDToolsCount, and PIDCourseData - all of these have instances where a single student might have multiple repeating rows.
From what I remember from my other classes, this is an example of a “tall” dataset that I need to convert to a “wide” dataset. I can do that easily for the Purpose and Tools datasets by using functions from the tidyr package. I can also replaced any ‘NAs’ (which represent that a student has not checked in for that tool or purpose) with ‘0’ values.
PIDPurpose_wide <- spread(PIDPurposeCount, Purpose.Descr, No.Measure.Value) %>%
mutate_at(c(2:9), ~replace_na(.,0))
PIDTools_wide <- spread(PIDToolsCount, TrainingName, Count) %>%
mutate_at(c(2:18), ~replace_na(.,0))
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## ℹ The deprecated feature was likely used in the tidyr package.
## Please report the issue at <https://github.com/tidyverse/tidyr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
head(PIDPurpose_wide, n = 10)
## PID Consultation Coursework Personal Product/business development
## 1 704221707 0 6 2 0
## 2 704277964 0 0 2 0
## 3 707857617 0 0 1 0
## 4 710237433 0 0 14 0
## 5 711002610 0 0 0 0
## 6 711644898 0 0 1 0
## 7 711919044 0 0 0 0
## 8 711957114 1 0 0 0
## 9 712425751 0 0 0 0
## 10 714013347 0 8 0 0
## Research Staff after-hours Training Workshop
## 1 0 0 1 0
## 2 0 0 1 0
## 3 0 0 2 0
## 4 0 0 2 1
## 5 0 0 1 0
## 6 0 0 1 1
## 7 0 0 1 0
## 8 0 0 1 0
## 9 0 0 5 0
## 10 0 0 1 0
head(PIDTools_wide, n = 10)
## PID V1 3D Printer (uPrint) 3D Prt (Form 3B,Flex filmnt,etc.) 3DP
## 1 704221707 0 0 0 0
## 2 704277964 0 2 0 0
## 3 707857617 0 0 0 1
## 4 710237433 0 0 0 0
## 5 711002610 0 0 0 0
## 6 711644898 0 0 0 1
## 7 711919044 0 0 0 0
## 8 711957114 1 0 0 1
## 9 712425751 0 0 0 0
## 10 714013347 0 0 0 0
## BeAM101 Desktop Embroidery Electronics Embroidery Laser Metal Shop Serger
## 1 5 0 0 0 0 0 0
## 2 1 0 0 0 0 0 0
## 3 1 0 0 0 0 0 0
## 4 0 0 0 0 0 0 1
## 5 1 0 0 0 0 0 0
## 6 1 0 0 0 0 0 0
## 7 1 0 0 0 0 0 0
## 8 1 0 0 0 0 0 0
## 9 1 0 0 1 1 0 0
## 10 8 0 0 3 1 0 0
## Sewing Shaper Origin ShopBot Vinyl Vinyl Cutter (t-shirt) Woodshop
## 1 0 0 0 0 0 4
## 2 0 0 0 0 0 0
## 3 0 1 0 0 0 0
## 4 0 0 1 0 0 14
## 5 0 0 0 0 0 0
## 6 0 0 0 0 0 0
## 7 0 0 0 0 0 0
## 8 0 0 0 0 0 0
## 9 0 0 0 1 0 1
## 10 0 0 0 0 0 0
The CourseData is a little trickier because I don’t have a measure or count yet - only the courses that students said that they were taking, with each course listed on a different line next to the PID it’s associated with. Ideally, I want a “wide” dataset with all of the courses listed and a binary ‘yes/no’ on whether that student is associated with that course. It will be EXTREMELY wide, as there’s 50+ courses listed in the dataset, but I can curate it down from there in the next steps.
PIDCourseData_wide <- PIDCourseData %>%
count(PID, CourseID) %>%
spread(CourseID, n, fill = 0)
head(PIDCourseData_wide, n = 10)
## PID AMST 460H APPL 089 APPL 101 APPL 110 APPL 240 APPL 260 APPL 265
## 1 704221707 0 1 0 0 0 0 0
## 2 714013347 0 0 0 0 0 0 0
## 3 720126788 0 0 0 0 0 0 0
## 4 720240491 0 0 0 0 0 0 0
## 5 720536612 0 0 0 0 0 0 0
## 6 730117742 0 0 0 0 0 0 0
## 7 730246989 0 0 0 0 0 0 0
## 8 730254910 0 0 0 0 0 0 0
## 9 730273242 0 0 0 0 0 0 0
## 10 730311905 0 0 0 1 0 0 0
## APPL 285 APPL 390 APPL 412 APPL 430 APPL 463 ARTS 363 BMME 298 BMME 698
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 1
## 4 0 1 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 0 0 0 0 0 0 0
## CHEM 089 CLAS 059 CLAS 133H CMPL 246 COMP 089 DENT 205 DENT 212 DRAM 766
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 1
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## 7 0 0 0 1 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 1
## 10 0 0 0 0 0 0 0 0
## ENEC 468 ENGL 143 ENVR 089 GEOG 115 GEOG 228 IDST 101 INLS 690 INLS 737
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 1 0 0 0 0
## 6 0 0 0 0 0 0 0 1
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 0 0 0 0 0 0 0
## KOR 346 MASC 053 MATH 069 MEJO 595 MTSC 710 MUSC 051 NSCI 395 NSCI 405
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 1 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 0 0 0 0 0 0 0
## NSCI 424 PHYS 051 PHYS 055 PHYS 100 PHYS 231 PSYC 330 RELI 105 SPAN 321
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 0 0 0 0 0 0 0
Finally, if I want to do anything with the dates that are in the PIDOperations spreadsheet, I need to make sure they’re in a consistent format that’s readable by R. I can use the parse_data function in readr to help with this.
#some of the dates are inconsistent, so I'm using lubridate to streamline the process
mdy <- mdy(PIDOperations$FirstVisit)
## Warning: 452 failed to parse.
dmy <- dmy(PIDOperations$FirstVisit)
dmy[is.na(dmy)] <- mdy[is.na(dmy)]
PIDOperations$FirstVisit <- dmy
mdy <- mdy(PIDOperations$LastVisit)
## Warning: 456 failed to parse.
dmy <- dmy(PIDOperations$LastVisit)
dmy[is.na(dmy)] <- mdy[is.na(dmy)]
PIDOperations$LastVisit <- dmy
head(PIDOperations, n = 10)
## PID FirstVisit LastVisit TrainingsCount VisitsCount
## 1 704221707 2023-03-26 2023-04-26 1 9
## 2 704277964 2023-01-10 2023-01-19 1 3
## 3 707857617 2023-03-16 2023-03-24 1 3
## 4 710237433 2023-02-07 2023-05-09 2 16
## 5 711002610 2023-03-21 2023-03-21 1 1
## 6 711644898 2023-03-28 2023-03-28 1 1
## 7 711919044 2023-03-21 2023-03-21 1 1
## 8 711957114 2023-04-04 2023-04-04 2 2
## 9 712425751 2023-01-26 2023-03-29 5 5
## 10 714013347 2023-02-21 2023-04-25 1 9
Okay, now I’ve got four data frames that have one observation for each unique PID. NAs were all reliably connected to formatting or intake mechanism, in a consistent pattern that made them easy to apply the same fix across the board. All of these data frames have WAY more information that I’m ultimately going to need, so I’m going to spend some time thinking about what I want to keep when joining into a large megadataset.
It helps to start with the research question. What am I trying to answer, and what data might be relevant to that question? At the end of this ‘Preparing Data’ assignment, I want to try to use logistic regression to predict whether someone returned to the makerspace the following year. I like that as a question, because this data is from Spring 2023 - which means that I can ask our database admin to pull the operations data again for Fall 2023, and see how accurate my model was!
Let’s go through each dataset and determine what factors might be most relevant in answering that question/making that prediction. That will guide me in selecting and creating features in the next section.
PIDCourseData_wide: This contains information about what courses each student checked in under - i.e., they said “I’m here to work on a project for APPL110.” Our student staff specifically ask them if they’re here for a course/what course when checking them in, so although this information is primarily self-reported, I consider it reliable. Skimming the data frame, I think it’s clear that I don’t want 50+ columns, but I’m not sure if I want to lose the granularity of whether certain courses have more influence on number of visits/rate of return than others.
I could go two different ways: I can create a feature that’s essentially “Number of courses this student is associated with” which would be a course count for each PID. I could also count the number of students associated with each course, and then only keep courses that had more than 1-2 students visiting the space. Usually if only one student from a course is using the makerspace for their projects, it’s not because of anything the course overtly did to encourage them (i.e. the final project is a “design and making” project.) Instead, they’re usually someone who loves design and making outside of the course, and has figured out a way to apply those skills to their coursework irregardless of the course.
PIDPurpose_wide: This data frame contains information about what purpose (i.e. coursework, research, etc.) a student checked in under. I’ll probably do the same thing that I proposed for the CourseData - I’ll count the number of students associated with each purpose, and then only keep purposes that had more than 5 students visiting the space.
PIDTools_wide: This data frame contains information about what tool (i.e. 3D Printer, etc.) a student checked in under. There are a number of tools that we no longer offer, or that are not available to the general public, so I’ll only keep the tools that I know are widely available (Orientation, 3D Printer - Ultimaker, Laser Cutter, Sewing, Vinyl Cutter, Wood Shop, Industrial Embroidery.)
PIDOperations: This data frame contains general information about someone’s check-in (e.g. dates, etc.) I definitely want to keep the count of their certifications, and a count of their visits. I also think it might be interesting to create a new feature that calculates the time between their first visit and their last visit. If those dates are close together, it implies to me that they might not have a longterm association with the makerspace.
Great, now I’ve got an idea of where I’m going. Let’s start by reducing down and joining these datasets together into one large dataset, before creating any new features. First, I need to generate those counts for Course Data and Purpose, which will guide me in selecting features for inclusion in the larger dataset.
PIDCourseData %>% count(CourseID, sort = TRUE)
## CourseID n
## 1 APPL 110 220
## 2 GEOG 115 33
## 3 APPL 089 25
## 4 MASC 053 24
## 5 PSYC 330 14
## 6 KOR 346 13
## 7 PHYS 100 12
## 8 CMPL 246 9
## 9 AMST 460H 8
## 10 DRAM 766 5
## 11 APPL 101 4
## 12 APPL 240 4
## 13 BMME 698 4
## 14 APPL 260 3
## 15 APPL 412 3
## 16 APPL 430 3
## 17 CHEM 089 3
## 18 SPAN 321 3
## 19 APPL 285 2
## 20 INLS 737 2
## 21 NSCI 424 2
## 22 APPL 265 1
## 23 APPL 390 1
## 24 APPL 463 1
## 25 ARTS 363 1
## 26 BMME 298 1
## 27 CLAS 059 1
## 28 CLAS 133H 1
## 29 COMP 089 1
## 30 DENT 205 1
## 31 DENT 212 1
## 32 ENEC 468 1
## 33 ENGL 143 1
## 34 ENVR 089 1
## 35 GEOG 228 1
## 36 IDST 101 1
## 37 INLS 690 1
## 38 MATH 069 1
## 39 MEJO 595 1
## 40 MTSC 710 1
## 41 MUSC 051 1
## 42 NSCI 395 1
## 43 NSCI 405 1
## 44 PHYS 051 1
## 45 PHYS 055 1
## 46 PHYS 231 1
## 47 RELI 105 1
PIDPurposeCount %>% count(Purpose.Descr, sort = TRUE)
## Purpose.Descr n
## 1 Training 689
## 2 Personal 363
## 3 Coursework 251
## 4 Workshop 109
## 5 Research 16
## 6 Consultation 11
## 7 Staff after-hours 3
## 8 Product/business development 1
Based on those results, I’ll keep the following:
From PIDCourseData: I’ll keep all of the classes that had 5+ students: APPL 110, GEOG 115, APPL 089, MASC 053, PSYC 330, KOR 346, PHYS 100, CMPL 246, AMST 460H, DRAM 766.
From PIDPurposeCount: I’ll keep any purpose that had more than 20 check-ins: Training, Personal project, Class assignment, Workshop.
Now that I know what I’m going to be selecting, it’s time to join those features together into one large data frame that I can use going forward.
PIDCourseData_FINAL <- PIDCourseData_wide %>%
select(PID, `APPL 089`, `APPL 110`, `GEOG 115`, `MASC 053`,
`PSYC 330`, `KOR 346`, `PHYS 100`, `CMPL 246`, `AMST 460H`, `DRAM 766`)
PIDPurposeCount_FINAL <- PIDPurpose_wide %>%
select(PID, Training, Personal, Coursework, Workshop)
PIDToolsCount_FINAL <- PIDTools_wide %>%
select(PID, BeAM101, `3DP`, Laser, Woodshop, Vinyl, Sewing, Embroidery)
list_df = list(PIDToolsCount_FINAL, PIDPurposeCount_FINAL, PIDCourseData_FINAL, PIDOperations)
MegaPIDData <- list_df %>% reduce(left_join, by='PID') %>%
mutate_at(c(2:26), ~replace_na(.,0))
head(MegaPIDData, n = 10)
## PID BeAM101 3DP Laser Woodshop Vinyl Sewing Embroidery Training
## 1 704221707 5 0 0 4 0 0 0 1
## 2 704277964 1 0 0 0 0 0 0 1
## 3 707857617 1 1 0 0 0 0 0 2
## 4 710237433 0 0 0 14 0 0 0 2
## 5 711002610 1 0 0 0 0 0 0 1
## 6 711644898 1 1 0 0 0 0 0 1
## 7 711919044 1 0 0 0 0 0 0 1
## 8 711957114 1 1 0 0 0 0 0 1
## 9 712425751 1 0 1 1 1 0 1 5
## 10 714013347 8 0 1 0 0 0 3 1
## Personal Coursework Workshop APPL 089 APPL 110 GEOG 115 MASC 053 PSYC 330
## 1 2 6 0 1 0 0 0 0
## 2 2 0 0 0 0 0 0 0
## 3 1 0 0 0 0 0 0 0
## 4 14 0 1 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 1 0 1 0 0 0 0 0
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 8 0 0 0 0 0 0
## KOR 346 PHYS 100 CMPL 246 AMST 460H DRAM 766 FirstVisit LastVisit
## 1 0 0 0 0 0 2023-03-26 2023-04-26
## 2 0 0 0 0 0 2023-01-10 2023-01-19
## 3 0 0 0 0 0 2023-03-16 2023-03-24
## 4 0 0 0 0 0 2023-02-07 2023-05-09
## 5 0 0 0 0 0 2023-03-21 2023-03-21
## 6 0 0 0 0 0 2023-03-28 2023-03-28
## 7 0 0 0 0 0 2023-03-21 2023-03-21
## 8 0 0 0 0 0 2023-04-04 2023-04-04
## 9 0 0 0 0 0 2023-01-26 2023-03-29
## 10 0 0 0 0 1 2023-02-21 2023-04-25
## TrainingsCount VisitsCount
## 1 1 9
## 2 1 3
## 3 1 3
## 4 2 16
## 5 1 1
## 6 1 1
## 7 1 1
## 8 2 2
## 9 5 5
## 10 1 9
It’s looking pretty good! There were some NAs in the course columns as I joined anything, which implies that those PIDs were not associated with any of the courses that I kept from the PIDCourseData, so I went ahead with replacing the NAs with a value of ‘0’.
Let’s try creating those two additional features - ‘CoursesTaken’ as an indicator of how many courses a student was associated with, and ‘Timespan’ as a calculation in days across the two dates (first visit and last visit.) For ‘CourseTaken,’ I want an accurate count of all of the courses that an individual student is associated with, so I’ll need to use the original ‘PIDCourseData’ sheet which has all known courses in it.
#CoursesTaken
CoursesTakenFeature <- PIDCourseData_wide %>%
mutate(CoursesTaken = rowSums(select(., -PID))) %>%
select(PID, CoursesTaken)
MegaPIDData <- left_join(MegaPIDData, CoursesTakenFeature, "PID") %>%
mutate_at(c("CoursesTaken"), ~replace_na(.,0))
#Timespan
MegaPIDData <- MegaPIDData %>%
mutate(Timespan = difftime(LastVisit,FirstVisit,units="days"))
MegaPIDData$Timespan <- as.numeric(MegaPIDData$Timespan)
MegaPIDData <- MegaPIDData[,c(1, 23:28, 2:22)]
head(MegaPIDData, n = 10)
## PID FirstVisit LastVisit TrainingsCount VisitsCount CoursesTaken
## 1 704221707 2023-03-26 2023-04-26 1 9 1
## 2 704277964 2023-01-10 2023-01-19 1 3 0
## 3 707857617 2023-03-16 2023-03-24 1 3 0
## 4 710237433 2023-02-07 2023-05-09 2 16 0
## 5 711002610 2023-03-21 2023-03-21 1 1 0
## 6 711644898 2023-03-28 2023-03-28 1 1 0
## 7 711919044 2023-03-21 2023-03-21 1 1 0
## 8 711957114 2023-04-04 2023-04-04 2 2 0
## 9 712425751 2023-01-26 2023-03-29 5 5 0
## 10 714013347 2023-02-21 2023-04-25 1 9 1
## Timespan BeAM101 3DP Laser Woodshop Vinyl Sewing Embroidery Training
## 1 31 5 0 0 4 0 0 0 1
## 2 9 1 0 0 0 0 0 0 1
## 3 8 1 1 0 0 0 0 0 2
## 4 91 0 0 0 14 0 0 0 2
## 5 0 1 0 0 0 0 0 0 1
## 6 0 1 1 0 0 0 0 0 1
## 7 0 1 0 0 0 0 0 0 1
## 8 0 1 1 0 0 0 0 0 1
## 9 62 1 0 1 1 1 0 1 5
## 10 63 8 0 1 0 0 0 3 1
## Personal Coursework Workshop APPL 089 APPL 110 GEOG 115 MASC 053 PSYC 330
## 1 2 6 0 1 0 0 0 0
## 2 2 0 0 0 0 0 0 0
## 3 1 0 0 0 0 0 0 0
## 4 14 0 1 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 1 0 1 0 0 0 0 0
## 7 0 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 0
## 10 0 8 0 0 0 0 0 0
## KOR 346 PHYS 100 CMPL 246 AMST 460H DRAM 766
## 1 0 0 0 0 0
## 2 0 0 0 0 0
## 3 0 0 0 0 0
## 4 0 0 0 0 0
## 5 0 0 0 0 0
## 6 0 0 0 0 0
## 7 0 0 0 0 0
## 8 0 0 0 0 0
## 9 0 0 0 0 0
## 10 0 0 0 0 1
Last thing before we move on to exploring the data: I want to rename some of the columns to make it a little clearer where they originally came from. In particular, I want to put a ‘P’ in front of the features that came from the Purpose dataset, and a ‘T’ in front of the features that came from the Tools dataset. I think that will make it a little easier to read going forward.
MegaPIDData <- MegaPIDData %>% rename("T_BeAM101" = "BeAM101",
"T_3DP" = "3DP",
"T_Laser" = "Laser",
"T_Woodshop" = "Woodshop",
"T_Vinyl" = "Vinyl",
"T_Sewing" = "Sewing",
"T_Embroidery" = "Embroidery",
"P_Training" = "Training",
"P_Personal" = "Personal",
"P_Coursework" = "Coursework",
"P_Workshop" = "Workshop")
head(MegaPIDData, n = 10)
## PID FirstVisit LastVisit TrainingsCount VisitsCount CoursesTaken
## 1 704221707 2023-03-26 2023-04-26 1 9 1
## 2 704277964 2023-01-10 2023-01-19 1 3 0
## 3 707857617 2023-03-16 2023-03-24 1 3 0
## 4 710237433 2023-02-07 2023-05-09 2 16 0
## 5 711002610 2023-03-21 2023-03-21 1 1 0
## 6 711644898 2023-03-28 2023-03-28 1 1 0
## 7 711919044 2023-03-21 2023-03-21 1 1 0
## 8 711957114 2023-04-04 2023-04-04 2 2 0
## 9 712425751 2023-01-26 2023-03-29 5 5 0
## 10 714013347 2023-02-21 2023-04-25 1 9 1
## Timespan T_BeAM101 T_3DP T_Laser T_Woodshop T_Vinyl T_Sewing T_Embroidery
## 1 31 5 0 0 4 0 0 0
## 2 9 1 0 0 0 0 0 0
## 3 8 1 1 0 0 0 0 0
## 4 91 0 0 0 14 0 0 0
## 5 0 1 0 0 0 0 0 0
## 6 0 1 1 0 0 0 0 0
## 7 0 1 0 0 0 0 0 0
## 8 0 1 1 0 0 0 0 0
## 9 62 1 0 1 1 1 0 1
## 10 63 8 0 1 0 0 0 3
## P_Training P_Personal P_Coursework P_Workshop APPL 089 APPL 110 GEOG 115
## 1 1 2 6 0 1 0 0
## 2 1 2 0 0 0 0 0
## 3 2 1 0 0 0 0 0
## 4 2 14 0 1 0 0 0
## 5 1 0 0 0 0 0 0
## 6 1 1 0 1 0 0 0
## 7 1 0 0 0 0 0 0
## 8 1 0 0 0 0 0 0
## 9 5 0 0 0 0 0 0
## 10 1 0 8 0 0 0 0
## MASC 053 PSYC 330 KOR 346 PHYS 100 CMPL 246 AMST 460H DRAM 766
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## 7 0 0 0 0 0 0 0
## 8 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0
## 10 0 0 0 0 0 0 1
Okay, time to explore the dataset that we’ve created. Let’s start with the structure, and then we can look at summary statistics for two potential dependent variables (VisitsCount and Timespan.)
str(MegaPIDData)
## 'data.frame': 689 obs. of 28 variables:
## $ PID : int 704221707 704277964 707857617 710237433 711002610 711644898 711919044 711957114 712425751 714013347 ...
## $ FirstVisit : Date, format: "2023-03-26" "2023-01-10" ...
## $ LastVisit : Date, format: "2023-04-26" "2023-01-19" ...
## $ TrainingsCount: int 1 1 1 2 1 1 1 2 5 1 ...
## $ VisitsCount : int 9 3 3 16 1 1 1 2 5 9 ...
## $ CoursesTaken : num 1 0 0 0 0 0 0 0 0 1 ...
## $ Timespan : num 31 9 8 91 0 0 0 0 62 63 ...
## $ T_BeAM101 : int 5 1 1 0 1 1 1 1 1 8 ...
## $ T_3DP : int 0 0 1 0 0 1 0 1 0 0 ...
## $ T_Laser : int 0 0 0 0 0 0 0 0 1 1 ...
## $ T_Woodshop : int 4 0 0 14 0 0 0 0 1 0 ...
## $ T_Vinyl : int 0 0 0 0 0 0 0 0 1 0 ...
## $ T_Sewing : int 0 0 0 0 0 0 0 0 0 0 ...
## $ T_Embroidery : int 0 0 0 0 0 0 0 0 1 3 ...
## $ P_Training : int 1 1 2 2 1 1 1 1 5 1 ...
## $ P_Personal : int 2 2 1 14 0 1 0 0 0 0 ...
## $ P_Coursework : int 6 0 0 0 0 0 0 0 0 8 ...
## $ P_Workshop : int 0 0 0 1 0 1 0 0 0 0 ...
## $ APPL 089 : num 1 0 0 0 0 0 0 0 0 0 ...
## $ APPL 110 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ GEOG 115 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ MASC 053 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ PSYC 330 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ KOR 346 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ PHYS 100 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CMPL 246 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ AMST 460H : num 0 0 0 0 0 0 0 0 0 0 ...
## $ DRAM 766 : num 0 0 0 0 0 0 0 0 0 1 ...
summary(MegaPIDData$VisitsCount)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.000 3.000 5.662 9.000 32.000
hist(MegaPIDData$VisitsCount)
summary(MegaPIDData$Timespan)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 0.00 35.00 45.36 89.00 118.00
hist(MegaPIDData$Timespan)
The mean number of visits for this dataset is , while the mean number of days between first and last visits is . Those histograms in particular are interesting to me. For the VisitsCount histogram, it shows a positively skewed distribution, which I interpret to mean that most people are only visiting the makerspace 1-5 times in a semester.
The Timespan histogram, however, shows that most people are visiting the makerspace for a short window of time within the overall semester. If I had to guess, these are the students that are required to complete a training for their class, but are not required to continue using the makerspace for their class project. If you exclude the observations that have a Timespan of less than 2 weeks, though, the distribution becomes much more ambiguous:
hist(MegaPIDData$Timespan, xlim = c(14, 120), ylim = c(0, 100))
In this histogram, ~40 people had a Timespan between First and Last Visit of 20-30 days, while 80 people had a Timespan of 100-110 days. I don’t quite know what do with this yet, but it’s given me food for thought - e.g. what’s the correlation between individual courses and Timespan? What about other relationships between features? We can take a look by creating a correlation matrix.
cor(MegaPIDData[c("VisitsCount", "Timespan", "CoursesTaken", "TrainingsCount")])
## VisitsCount Timespan CoursesTaken TrainingsCount
## VisitsCount 1.0000000 0.7182441 0.3405765 0.5885127
## Timespan 0.7182441 1.0000000 0.4071375 0.5411242
## CoursesTaken 0.3405765 0.4071375 1.0000000 0.1345758
## TrainingsCount 0.5885127 0.5411242 0.1345758 1.0000000
At a glance, it looks like there is a strong positive correlation between the number of visits and the timespan between first/last visit, which makes sense (the more you’re visiting would generally create a longer period of time over which you’re visiting.) There is a relatively weak relationship between TrainingsCount and CoursesTaken, which I found somewhat surprising - I think that might imply that people are going to take trainings based on their interests, rather than solely based on the number of courses they’re in.
Alright, let’s try to explore some basic algorithms. I think I’ll use TrainingsCount as my dependent variable, and see which purpose types might influence that variable. For example, let’s focus on TrainingsCount, P_Coursework, and P_Personal. Do the types of projects you work on in the makerspace influence the number of trainings that you take in a semester?
set.seed(777)
trainingscount_modelPersonal <- lm(TrainingsCount
~ P_Personal,
data = MegaPIDData)
summary(trainingscount_modelPersonal)
##
## Call:
## lm(formula = TrainingsCount ~ P_Personal, data = MegaPIDData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.4724 -0.6115 -0.6115 0.3885 4.3885
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.61149 0.04708 34.230 <2e-16 ***
## P_Personal 0.12406 0.01368 9.066 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.046 on 687 degrees of freedom
## Multiple R-squared: 0.1069, Adjusted R-squared: 0.1056
## F-statistic: 82.19 on 1 and 687 DF, p-value: < 2.2e-16
plot(trainingscount_modelPersonal)
trainingscount_modelCoursework <- lm(TrainingsCount
~ P_Coursework,
data = MegaPIDData)
summary(trainingscount_modelCoursework)
##
## Call:
## lm(formula = TrainingsCount ~ P_Coursework, data = MegaPIDData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1020 -0.5845 -0.5845 0.4155 4.4155
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.58447 0.04349 36.44 <2e-16 ***
## P_Coursework 0.13250 0.01083 12.24 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.003 on 687 degrees of freedom
## Multiple R-squared: 0.179, Adjusted R-squared: 0.1778
## F-statistic: 149.8 on 1 and 687 DF, p-value: < 2.2e-16
plot(trainingscount_modelCoursework)
My first impression is that purpose has a similar relationship with training counts, whether it’s coursework or personal. For every visit that a person checks in to work on a personal project, the number of trainings that they complete in a semester increases by .12406. We can re-scale this to be more meaningful - for roughly every additional 8 visits, a person completes an additional training. Compare that with coursework - for every visit a person checks in to work on a course project, the number of trainings that they complete in a semester increases by .1325. Scaled up, it takes slightly fewer visits (between 7 and 8) to result in an additional training.
The p-values for both model are extremely small (<2e-16), which indicates that it would be extremely unlikely for P_Coursework or P_Personal to NOT HAVE a relationship with TrainingsCount. The residuals are positive for the 3rd/4th quartiles and negative for the 1st/2nd quartiles, but seem to fall more towards the extremes (e.g. the distance between 1Q and 3Q is less than 1, while Min to 1Q and 3Q to Max are both higher than 2.)
We can see more of this problem by looking at the adjusted r-square values, which are very low. The value for the trainingscount_modelPersonal is .1056, which means that this model only explains 10% of the dependent variable (yikes!) The value for the trainingscount_modelCoursework is only slightly better at .1778 (17% explained.) I wouldn’t use either of these models going forward - I would continue to experiment with other features and models.