-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.qmd
More file actions
165 lines (123 loc) · 4.67 KB
/
README.qmd
File metadata and controls
165 lines (123 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
---
format: gfm
execute:
echo: false
warning: false
---
```{r}
#| include: false
library(tidyverse)
library(glue)
extract_pkgs <- function(fl) {
if (length(fl) == 1) {
txt <- read.delim(fl, header = FALSE)[[1]]
pkgs <- str_extract_all(
string = txt,
pattern = "((?<=(library|require)\\()[a-zA-Z0-9\\.]{1,}(?=\\))|[a-zA-Z0-9\\.]{1,}(?=\\:{2}))"
)
pkgs <- unlist(pkgs)
} else if (length(fl) > 1) {
pkgs <- sapply(fl, extract_pkgs)
}
pkgs |>
unlist(recursive = TRUE) |>
unique()
}
```
# Machine Learning foR Psychologists
[](https://creativecommons.org/about/program-areas/education-oer/)
[](http://creativecommons.org/licenses/by-nc/4.0/)
[](http://cran.r-project.org/)
<sub>*Last updated `r Sys.Date()`.*</sub>
This Github repo contains all lesson files for *Machine Learning in R*.
The goal is to impart students with the basic tools to construct, evaluate and compare various **machine learning models, using [`tidymodels`](https://www.tidymodels.org/)**,
based on [_An Introduction to Statistical Learning: with applications in R_](https://www.statlearning.com/).
These topics were taught in the graduate-level course ***Machine Learning for Psychologists*** (Psych Dep., Ben-Gurion University of the Negev; Psych Dep., Tel-Aviv University). This course assumes basic competence in R (importing, regression modeling, plotting, etc.), along the lines of [*Practical Applications in R for Psychologists*](https://github.com/mattansb/Practical-Applications-in-R-for-Psychologists).
**Notes:**
- This repo contains only materials relating to *Practical Applications in R*, and does not contain any theoretical or introductory materials.
- Please note that some code does not work *on purpose*, to force students to learn to debug.
## Setup
You will need:
1. A fresh installation of [**`R`**](https://cran.r-project.org/) (preferably version 4.5.0 or above).
2. [RStudio IDE](https://www.rstudio.com/products/rstudio/download/) or [Positron](https://positron.posit.co/download) (optional, but recommended).
3. The following packages, listed by lesson:
```{r}
r_files <- list.files(pattern = ".R$", recursive = TRUE, full.names = TRUE)
r_files <- r_files[str_detect(r_files, "^\\./\\d{2}")]
r_lesson_names <- str_extract(r_files, pattern = "(?<=(/)).{1,}(?=(/))")
r_list <- split(r_files, r_lesson_names)
r_pkgs <- lapply(r_list, extract_pkgs)
r_df <- tibble(
Lesson = names(r_pkgs),
Packages = r_pkgs
)
r_df |>
mutate(
Lesson = glue("[{Lesson}]({Lesson}//)"),
Packages = sapply(Packages, \(x) glue('`{x}`') |> glue_collapse(sep = ", "))
) |>
knitr::kable()
```
<details><summary><i>Installing R Packages</i></summary>
You can install all the R packages used by running:
```{r}
r_pkgs <- r_pkgs |>
unlist(recursive = TRUE) |>
unique() |>
setdiff(tidyverse::tidyverse_packages()) |>
setdiff(tidymodels::tidymodels_packages()) |>
setdiff(easystats::easystats_packages()) |>
c("tidyverse", "tidymodels", "easystats") |>
sort()
packinfo <- installed.packages(fields = c("Package", "Version"))
get_src <- function(pkg) {
pd <- packageDescription(pkg)
if (is.null(src <- pd$Repository)) {
if (!is.null(src <- pd$GithubRepo)) {
src <- paste0("Github: ", pd$GithubUsername, "/", src)
} else {
src <- "Dev"
}
}
return(src)
}
r_pkg_info <- tibble(
pkg = r_pkgs,
source = sapply(r_pkgs, get_src),
version = packinfo[r_pkgs, "Version"]
) |>
filter(version != packageVersion("base")) |>
mutate(
pak_src = case_when(
source == "Dev" ~ NA,
source == "CRAN" ~ "cran",
str_detect(source, "Github") ~ "github",
str_detect(source, "Bioconductor") ~ "bioc"
),
pak = case_match(
pak_src,
"cran" ~ glue("cran::{pkg}"),
"bioc" ~ glue("bioc::{pkg}"),
"github" ~ glue("github::{str_remove_all(source, 'Github: ')}"),
.default = pkg
)
) |>
arrange(pkg)
cat("# in alphabetical order:\n\n")
cat("pak::pak(\n c(")
r_pkg_info$comma <- rep(",", nrow(r_pkg_info))
r_pkg_info$comma[nrow(r_pkg_info)] <- ""
cat(
glue_data(r_pkg_info, ' "{pak}"{comma} # {version}') |>
glue_collapse(sep = "\n")
)
cat(" )\n)")
```
</details>
---
### Additional Versions
- Prior to 2025, this course was based on the `{caret}` package - this version can still be found [here](https://github.com/mattansb/Machine-Learning-foR-Psychologists/tree/caret).
- Partial parallel `python` lessons can be found in the [`py` folder](py/).
---
### Acknowledgements
Materials developed with Yael Bar-Shachar.