# install.packages('pacman')
library(pacman)
p_load('reticulate', 'keras',
'tidyverse', 'lemon', 'knitr', 'kableExtra', 'dplyr', 'plotly', 'randomNames',
'stargazer', 'tidymodels', 'gghighlight', 'gt', 'colorspace', 'patchwork',
'latex2exp', 'skimr', 'modelsummary', 'ggplot2', 'DataExplorer', 'visdat',
'haven', 'probably', 'DALEX', 'DALEXtra', 'fairmodels', 'glmnet', 'naniar',
'rpart', 'rattle', 'magick', 'vip', 'xgboost', 'tm', 'rsample', 'explore',
'ExPanDaR', 'sjlabelled', 'profvis', 'rsconnect', 'whereami', 'DT', 'tidytext')
About
Chapter last updated: 10 Februar 2025
This website serves as slides and script for the GESIS workshop Applied Machine Learning (2024, 2025), for the seminar Künstliche Intelligenz (KI) für die Sozial- und Politikwissenschaften (Universität Freiburg, 2024) as well as for workshops in shorter formats taught by Paul C. Bauer .1 Original material is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license. Where I draw on other authors material (I do so extensively) other licenses may apply. Please note the references in the syllabus as well as the citations and links in the script. For potential future versions of this material see www.paulcbauer.de. If you have feedback or discover errors/dead links please let me know (mail@paulcbauer.de). Before you use the script please make sure to install all the necessary packages (see Section 9) before you use the script.
- Current version: https://paulcbauer.github.io/workshop_applied_machine_learning/
- Interested in a machine learning workshop at your institution/company? Please contact me at mail@paulcbauer.de.
1 Versions: Workshops & Seminars
- Gesis: 4 days (18h in total): This GESIS version lasts 18 ours across 4 days and cover almost all of the content in the script. Different foki are set depending on participants demands.
- Hours: 10:00 - 12:00 and 13:00 - 16:00
- Dates, Contact and Outline: See syllabus (received link via email).
- Seminar at University of Freiburg (18h teaching hours in total): Siehe Syllabus.
2 Instructor: About me
- Lecturer at University of Freiburg (Department of politics), Postdoc at LMU Munich (statistics department, Frauke Kreuter), External fellow at Mannheim Centre for European Social Research
- Previously
- PhD in Social Sciences at the University of Bern
- research/postdoctoral fellow at the Mannheim Centre for European Social Research, the European University Institute (Florence, Italy)
- My research (Google Scholar)
- Substantive: Political sociology.. trust, polarization, social media, fake news
- Methods/data: Causal inference, experiments, text data, data visualization, machine learning, big data
- (some) ML projects: Bauer and Clemm von Hohenberg (2021), Landesvatter and Bauer (2024), Landesvatter, Behnert, and Bauer (2023), Schuette and Bauer (2025)
- Gesis workshops: Applied Data Visualization, Interactive Data Analysis with Shiny, Applied Machine Learning (with R)
- Contact:
mail@paulcbauer.de
; www.paulcbauer.de; Github; LinkedIn
3 Your turn
- Let’s check our the survey results…
- Name?
- Affiliation? Country?
- What are you interests? What do you want to use machine learning for? (research questions? outcomes?)
- What prior knowledge do you have? (seminars?)
4 Contact & Outline & Dates
- See syllabus.
5 Script & material
- Literature: See syllabus.
- Website/script: https://paulcbauer.github.io/workshop_applied_machine_learning/
- Find it: Google “paul bauer applied machine learning”
- Document = slides + script (Zoom in/out with
STRG + mousewheel
) - Code: can all be found in the script
- Data: can usually be downloaded over links in the script. If not we’ll share the files.
- Full screen: F11
- Navigation: TOCs on left and right
- Search document (upper left)
- Lots of information in footnotes!2
- Document generated with quarto
- Motivation: Have a go-to script for participants (and ourselves!)
- Content: Mixture of theory, lab sessions, exercises and pure code examples for discussion
6 Strategy & Goals/Learning outcomes
Strategy: From the simple to the complex, slowly diving into the logic of machine learning using building blocks that we already know
Goals: By the end of the course participants will..
- ..understand key concepts underlying machine learning.
- ..be able to interpret and evaluate machine learning models.
- ..be able to critically assess model performance.
- ..be able to use various machine learning models for predictive and classification purposes.
- ..have learned how to use the tidymodels framework for machine learning in R.
- ..have learned how to evaluate and visualize model performance using packages like ggplot2.
Difficulty: How to choose out of many methods & models!
7 Online vs. offline
- Negative
- Screen fatigue
- Can’t run around to check your code
- Less engaging, less social
- Voice
- Screen sharing & less screen space than classroom
- Positive
- Participation from everywhere
- That’s how we interact more and more
- Rule(s):
- Please keep your camera online!
- Distracting animals/children/partners are a welcome distraction!
- Yawning, leaving, looking bored etc. allowed!
- Use a virtual background if you like!
- Leaving, returning allowed!
- There no stupid questions3
- Please keep your camera online!
8 Recommended readings
- Important: The workshop/seminar does not require any prior reading.
- However, our schedule is primarily based on two textbooks which we generally recommend for further reading (see references on website):
- Kuhn, M., & Silge, J. (2022). Tidy modeling with R. O’Reilly Media, Inc.
- Link: https://www.tmwr.org/
- James, G., Witten, D., Hastie, T., & Tibshirani, R. (2013, corrected printing June 2023). An introduction to statistical learning (Vol. 112, p. 18). New York: springer.
- Mehrabi, N., Morstatter, F., Saxena, N., Lerman, K., & Galstyan, A. (2021). A survey on bias and fairness in machine learning. ACM computing surveys (CSUR), 54(6), 1-35.
- Chollet, F. (2021). Deep learning with Python. Simon and Schuster.
- See R version: Chollet, F., & Allaire, J. J. (2018). Deep learning with R, Second Edition
- Link: https://livebook.manning.com/book/deep-learning-with-r-second-edition/
- Kuhn, M., & Silge, J. (2022). Tidy modeling with R. O’Reilly Media, Inc.
9 Software we will use
- Open-source software! (Q: Why?)
- R (R Core Team 2023)4
- only viable competitor is Python
- Install the necessary packages using the code below.
- Ggplot25 (Wickham 2016)
- Maybe: Plotly6 (Sievert 2020)
- Note: Ideally cite the software you use in your research especially when it is open-source (e.g., run
citation("ggplot2")
)
10 Data we will use
- Can be downloaded here
- European Social Survey (ESS) [Round 10 - 2020. Democracy, Digital social contacts]
- Outcome: Life satisfaction [0-10]
- The ESS contains different outcomes amenable to both classification and regression as well as a lot of variables that could be used as features (~580 variables).
- Research: Shen, Yin, and Jiao (2023); Collins et al. (2015); Kaiser, Otterbach, and Sousa-Poza (2022); Çiftci and Yıldız (2023); Pan and Cutumisu (2023); Prati (2022)
- COMPAS
- Outcome: Reoffense/Recidvism [0,1]
- We will be using the dataset published by propublica (Github) that is described by Angwin et al. (2016; and Lee, Du, and Guerzhoy 2020; James et al. 2013)
- The data is based on the COMPAS risk assessment tools (RAT). RATs are increasingly being used to assess a criminal defendant’s probability of re-offending.
- Datasets were prepared/adapted for the workhop7
- Workshop leaves out tedious data management tasks (recoding, renaming, etc.)
- I added missings on outcomes (life satisfaction, recidvism) to have some unseen data
- The self-learning material introduced those datasets.
Overview of Compas dataset variables
id
: ID of prisoner, numericname
: Name of prisoner, factorcompas_screening_date
: Date of compas screening, datedecile_score
: the decile of the COMPAS score, numericis_recid
: whether somone reoffended/recidivated (=1) or not (=0), numericis_recid_factor
: same but factor variableage
: a continuous variable containing the age (in years) of the person, numericage_cat
: age categorizedpriors_count
: number of prior crimes committed, numericsex
: gender with levels “Female” and “Male”, factorrace
: race of the person, factorjuv_fel_count
: number of juvenile felonies, numericjuv_misd_count
: number of juvenile misdemeanors, numericjuv_other_count
: number of prior juvenile convictions that are not considered either felonies or misdemeanors, numeric
11 Tools & software
11.0.1 R: Why use it?
- Free and open source (think of science in developing countries)
- Good online-documentation
- Lively community of users (forums etc.)
- Pioneering role
- Visualization capabilities
- Intuitiv
- Cooperates with other programs
- Used across wide variety of disciplines
- Object-oriented programming language
- Popularity (See popularity statistics on books, blogs, forums)
- RStudio as powerful integrated development environment (IDE) for R
- Evolves into a scientific work suite optimizing workflow (replication, reproducability etc.)
- Institutions/people (Gary King, Andrew Gelman etc.)
- Economic power (Revolution Analytics, Microsoft R Open)
- Python is only real “competitor”.. can be used from R (e.g. reticulate package!)8
11.0.2 R: Where/how to study?
If you haven’t used R sofar it’s necessary that you learn some basics in R. As a participant of the seminar you get 6 months access to all the courses on DataCamp. DataCamp has become the go-to site for self-studying various data science skills (mostly software).
- Overview of R courses: https://www.datacamp.com/category/r?page=1
- Introduction is free: https://www.datacamp.com/courses/free-introduction-to-r
- Recommended: https://app.datacamp.com/learn/career-tracks/data-analyst-with-r
11.0.3 R: Installation and setup
Below some notes on the installation and setup of R and relevant packages on your own computer:
- Install Rtools for Windows machines from CRAN (https://cran.r-project.org/bin/windows/Rtools/). If you are using OS X, you will need to to install XCode, available for free from the App Store. This will install a compiler (if you don’t have a compiler installed) which will be needed when installing packages from GitHub that require compilation from C++ source code.
- Install the latest version of R from CRAN (https://cran.r-project.org/).
- Install the latest version of RStudio (https://www.rstudio.com/products/RStudio/). RStudio is the editor we’ll rely on, i.e. we’ll write code in RStudio which is subsequently sent to and run within R.
- Start RStudio and install & load the latest versions of various packages that we need (see Section 9 for code on how to install the packages).
- You may also read up on how to create and “knit” Quarto files. Essentially, such files allow you to integrate the analyses you conduct with the text you write which is ideal for reproducability. Here is an intro to the concept and a simple example: https://quarto.org/docs/get-started/hello/rstudio.html.
11.0.4 Datacamp
- Adress: https://www.datacamp.com/
- 6-month access to do all courses you like (Q: Did everyone get the invitation?)
- Provide various tracks
- Q: What is your experience with Datacamp? Do you like it?
- Q: What makes Datacamp as a platform so powerful? Will it replace humans? (Data & memory!)
References
Footnotes
The first iteration of the workshop was taught in May 2020.↩︎
The popup on mousover!↩︎
Please ask no matter if you think the question is stupid or you have missed something in the course. Any question is valuable and any repetition of content is useful!↩︎
Creators: Core contributors and thousands of package authors.↩︎
Creators: https://github.com/tidyverse/ggplot2↩︎
Creators: https://github.com/plotly/plotly.js; https://github.com/ropensci/plotly↩︎
e.g., rename variables, data preparation↩︎
The seminar consists of a mix of theoretical and applied sessions. For the applied session we will rely on the software R. While there are various programs one could use, the reasons mentioned above speak for R (my personal view). The only real contendor for data science is Python. See here for a nice overview of the differences between the two.↩︎