-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
138 lines (95 loc) · 4.19 KB
/
README.Rmd
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
---
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```
# Machine Learning foR Psychologists
[![](https://img.shields.io/badge/Open%20Educational%20Resources-Compatable-brightgreen)](https://creativecommons.org/about/program-areas/education-oer/)
[![](https://img.shields.io/badge/CC-BY--NC%204.0-lightgray)](http://creativecommons.org/licenses/by-nc/4.0/)
[![](https://img.shields.io/badge/Language-R-blue)](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 [`caret`](https://topepo.github.io/caret/)**. (Materials developed with Yael Bar-Shachar.)
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
```{r, echo=FALSE}
extract_pkgs <- function(fl) {
if (length(fl) == 1) {
txt <- read.delim(fl, header = FALSE)[[1]] |>
paste0(collapse = "\n")
pkg_lib <- stringr::str_extract_all(txt, pattern = "(?<=library\\().{1,}(?=\\))")
pkg_req <- stringr::str_extract_all(txt, pattern = "(?<=require\\().{1,}(?=\\))")
pkg_name <- stringr::str_extract_all(txt, pattern = "[a-z|A-Z|0-9]{1,}(?=\\:\\:)")
pkgs <- c(pkg_lib, pkg_req, pkg_name)
} else if (length(fl) > 1) {
pkgs <- sapply(fl, extract_pkgs)
}
pkgs |>
unlist(recursive = TRUE) |>
unique()
}
make_pkg_table <- function(pkgs) {
pkgs <- pkgs[sapply(pkgs, function(x) length(x) > 0)]
ps <- sapply(pkgs, function(x){
paste0(
glue::glue("[`{x}`](https://CRAN.R-project.org/package={x})"),
collapse = ", "
)
})
glue::glue("|[{folder}](/{folder})|{ps}|\n\n",
folder = names(pkgs)) |>
c("|Lesson|Packages|\n|----|----|\n", i2 = _) |> # header
paste0(collapse = "")
}
```
You will need:
1. A fresh installation of [**`R`**](https://cran.r-project.org/) (preferably version 4.2 or above).
2. [RStudio IDE](https://www.rstudio.com/products/rstudio/download/) (optional, but recommended).
3. The following packages, listed by lesson:
```{r, echo=FALSE, message=FALSE, warning=FALSE}
r_list <- list.files(pattern = ".(R|r)$", recursive = TRUE, full.names = TRUE)
r_list <- r_list[!stringr::str_detect(r_list, pattern = "(SOLUTION|logo)")]
r_list <- r_list[stringr::str_detect(r_list, pattern = "^./[0-9]")]
lesson_names <- stringr::str_extract(r_list, pattern = "(?<=(/)).{1,}(?=(/))")
r_list <- split(r_list, lesson_names)
pkgs <- lapply(r_list, extract_pkgs)
print_pkgs <- make_pkg_table(pkgs)
```
`r print_pkgs`
You can install all the packages used by running:
```{r echo=FALSE, comment = "", warning=FALSE}
pkgs <- pkgs |>
unlist(recursive = TRUE) |>
unique() |> sort()
cat("# in alphabetical order:")
capture.output(cat("pkgs <-", capture.output(dput(pkgs)), fill = 80)) |>
styler::style_text()
cat("install.packages(pkgs, dependencies = TRUE)")
```
<details>
<summary><i>Package Versions</i></summary>
The package versions used here:
```{r, echo=FALSE}
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)
}
V <- packinfo[pkgs,"Version"]
src <- sapply(pkgs, get_src)
# setNames(paste0(V, " (", src,")"), pkgs)
v_info <- paste0(glue::glue(" - `{pkgs}` {V} (*{src}*)"), collapse = "\n")
```
`r v_info`
</details>